The Gaudi Framework  master (42b00024)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Service.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include Files
13 #include <GaudiKernel/Guards.h>
19 #include <GaudiKernel/MsgStream.h>
21 #include <GaudiKernel/Service.h>
23 
24 using std::string;
25 
26 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
27 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
28 
30  if ( m_svcManager ) m_svcManager->removeService( this ).ignore();
31 }
32 
33 // IService::sysInitialize
35  std::call_once( m_initFlag, &Service::sysInitialize_imp, this );
36  return m_initSC;
37 }
38 
40 
41  try {
44  // check if we want to audit the initialize
45  ( m_auditorInitialize ) ? auditorSvc().get() : nullptr,
47 
48  // initialize messaging (except for MessageSvc)
49  if ( name() != "MessageSvc" ) {
50  // this initializes the messaging, in case property update handlers need to print
51  // and update the property value bypassing the update handler
53  }
54 
55  bindPropertiesTo( serviceLocator()->getOptsSvc() );
56 
58  error() << "Property AutoRetrieveTools must be set TRUE if CheckToolDeps is True" << endmsg;
60  return;
61  }
62 
63  m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
64 
65  // Check for data dependencies in AlgTools
66  // visit all sub-algs and tools, build full set. First initialize ToolHandles if needed
67  if ( m_autoRetrieveTools ) {
68  try {
70  } catch ( const GaudiException& Exception ) {
71  error() << "Service failed to initilize ToolHandles : " << Exception << endmsg;
73  return;
74  }
75 
76  if ( m_checkToolDeps ) {
77  for ( auto& itool : m_tools ) {
78  info() << " AlgTool: " << itool->name() << endmsg;
79  IDataHandleHolder* idh = dynamic_cast<IDataHandleHolder*>( itool );
80  if ( idh == 0 ) {
81  error() << "dcast to IDataHandleHolder failed" << endmsg;
82  continue;
83  }
84  if ( idh->inputDataObjs().size() != 0 ) {
85  error() << "Service " << name() << " holds AlgTool " << itool->name()
86  << " which holds at least one ReadDataHandle" << endmsg;
87  for ( auto& obj : idh->inputDataObjs() ) { error() << " -> InputHandle: " << obj << endmsg; }
89  }
90  if ( idh->outputDataObjs().size() != 0 ) {
91  error() << "Service " << name() << " holds AlgTool " << itool->name()
92  << " which holds at least one WriteDataHandle" << endmsg;
93  for ( auto& obj : idh->outputDataObjs() ) { error() << " -> OutputHandle: " << obj << endmsg; }
95  }
96  }
97  }
98  }
99 
101  return;
102  } catch ( const GaudiException& Exception ) {
103  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
104  error() << Exception << endmsg;
105  // Stat stat( chronoSvc() , Exception.tag() );
106  } catch ( const std::exception& Exception ) {
107  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
108  error() << Exception.what() << endmsg;
109  // Stat stat( chronoSvc() , "*std::exception*" );
110  } catch ( ... ) {
111  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
112  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
113  }
114 
116 }
117 
118 //--- IService::initialize
120  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
122  return StatusCode::SUCCESS;
123 }
124 
125 // IService::sysStart
127  StatusCode sc;
128 
129  try {
132  // check if we want to audit the initialize
133  ( m_auditorStart ) ? auditorSvc().get() : nullptr, Gaudi::IAuditor::Start );
134  sc = start();
135  if ( sc.isSuccess() ) m_state = m_targetState;
136  return sc;
137  } catch ( const GaudiException& Exception ) {
138  fatal() << "in sysStart(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
139  error() << Exception << endmsg;
140  // Stat stat( chronoSvc() , Exception.tag() );
141  } catch ( const std::exception& Exception ) {
142  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
143  fatal() << Exception.what() << endmsg;
144  // Stat stat( chronoSvc() , "*std::exception*" );
145  } catch ( ... ) {
146  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
147  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
148  }
149 
150  return StatusCode::FAILURE;
151 }
152 
153 // IService::sysStop
155  StatusCode sc;
156 
157  try {
160  // check if we want to audit the initialize
161  ( m_auditorStop ) ? auditorSvc().get() : nullptr, Gaudi::IAuditor::Stop );
162  sc = stop();
163  if ( sc.isSuccess() ) m_state = m_targetState;
164  return sc;
165  } catch ( const GaudiException& Exception ) {
166  fatal() << "in sysStop(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
167  error() << Exception << endmsg;
168  // Stat stat( chronoSvc() , Exception.tag() );
169  } catch ( const std::exception& Exception ) {
170  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
171  error() << Exception.what() << endmsg;
172  // Stat stat( chronoSvc() , "*std::exception*" );
173  } catch ( ... ) {
174  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
175  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
176  }
177 
178  return StatusCode::FAILURE;
179 }
180 
181 //--- IService::stop
183  // stub implementation
184  return StatusCode::SUCCESS;
185 }
186 
187 //--- IService::start
189  // stub implementation
190  return StatusCode::SUCCESS;
191 }
192 
193 //--- IService::sysFinalize
195 
197 
198  try {
201  // check if we want to audit the initialize
202  ( m_auditorFinalize ) ? auditorSvc().get() : nullptr,
204  sc = finalize();
205  if ( sc.isSuccess() ) m_state = m_targetState;
206  } catch ( const GaudiException& Exception ) {
207  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
208  error() << Exception << endmsg;
209  // Stat stat( chronoSvc() , Exception.tag() ) ;
210  } catch ( const std::exception& Exception ) {
211  fatal() << " Standard std::exception is caught " << endmsg;
212  error() << Exception.what() << endmsg;
213  // Stat stat( chronoSvc() , "*std::exception*" ) ;
214  } catch ( ... ) {
215  fatal() << "UNKNOWN Exception is caught " << endmsg;
216  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
217  }
218 
219  m_pAuditorSvc = nullptr;
220  return sc;
221 }
222 
223 //--- IService::finalize
225  // m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
226  return StatusCode::SUCCESS;
227 }
228 
229 //--- IService::sysReinitialize
231 
232  StatusCode sc;
233 
234  // Check that the current status is the correct one.
236  error() << "sysReinitialize(): cannot reinitialize service not initialized" << endmsg;
237  return StatusCode::FAILURE;
238  }
239 
240  try {
241 
243  // check if we want to audit the initialize
244  ( m_auditorReinitialize ) ? auditorSvc().get() : nullptr,
246  sc = reinitialize();
247  return sc;
248  } catch ( const GaudiException& Exception ) {
249  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
250  error() << Exception << endmsg;
251  // Stat stat( chronoSvc() , Exception.tag() ) ;
252  } catch ( const std::exception& Exception ) {
253  fatal() << " Standard std::exception is caught " << endmsg;
254  error() << Exception.what() << endmsg;
255  // Stat stat( chronoSvc() , "*std::exception*" ) ;
256  } catch ( ... ) {
257  fatal() << "UNKNOWN Exception is caught " << endmsg;
258  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
259  }
260  return StatusCode::FAILURE;
261 }
262 
263 //--- IService::sysRestart
265 
266  StatusCode sc;
267 
268  // Check that the current status is the correct one.
270  error() << "sysRestart(): cannot restart service in state " << FSMState() << " -- must be RUNNING " << endmsg;
271  return StatusCode::FAILURE;
272  }
273 
274  try {
275 
277  // check if we want to audit the initialize
279  sc = restart();
280  return sc;
281  } catch ( const GaudiException& Exception ) {
282  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
283  error() << Exception << endmsg;
284  // Stat stat( chronoSvc() , Exception.tag() ) ;
285  } catch ( const std::exception& Exception ) {
286  fatal() << " Standard std::exception is caught " << endmsg;
287  error() << Exception.what() << endmsg;
288  // Stat stat( chronoSvc() , "*std::exception*" ) ;
289  } catch ( ... ) {
290  fatal() << "UNKNOWN Exception is caught " << endmsg;
291  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
292  }
293  return StatusCode::FAILURE;
294 }
295 
296 //--- IService::reinitialize
298  /* @TODO
299  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
300  * is causing too many problems
301  *
302  // Default implementation is finalize+initialize
303  StatusCode sc = finalize();
304  if (sc.isFailure()) {
305  error() << "reinitialize(): cannot be finalized" << endmsg;
306  return sc;
307  }
308  sc = initialize();
309  if (sc.isFailure()) {
310  error() << "reinitialize(): cannot be initialized" << endmsg;
311  return sc;
312  }
313  */
314  return StatusCode::SUCCESS;
315 }
316 
317 //--- IService::restart
319  // Default implementation is stop+start
320  StatusCode sc = stop();
321  if ( sc.isFailure() ) {
322  error() << "restart(): cannot be stopped" << endmsg;
323  return sc;
324  }
325  sc = start();
326  if ( sc.isFailure() ) {
327  error() << "restart(): cannot be started" << endmsg;
328  return sc;
329  }
330  return StatusCode::SUCCESS;
331 }
332 
333 //--- IService::getServiceName
334 const std::string& Service::name() const { return m_name; }
335 
336 //--- Retrieve pointer to service locator
338 
339 //--- Local methods
340 // Standard Constructor
341 Service::Service( std::string name, ISvcLocator* svcloc ) : m_name( std::move( name ) ), m_svcLocator( svcloc ) {
342  if ( m_name != "MessageSvc" ) { // the MessageSvc should not notify itself
345  }
346 
347  // Initialize the default value from ApplicationMgr AuditAlgorithms
348  Gaudi::Property<bool> audit( "AuditServices", false );
349  if ( auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" ) ) {
350  appMgr->getProperty( &audit ).ignore();
351  }
352 
353  m_auditorInitialize = audit;
354  m_auditorStart = audit;
355  m_auditorStop = audit;
356  m_auditorFinalize = audit;
357  m_auditorReinitialize = audit;
358  m_auditorRestart = audit;
359 }
360 
362  if ( !m_pAuditorSvc ) {
363  m_pAuditorSvc = serviceLocator()->service( "AuditorSvc" );
364  if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
365  }
366  return m_pAuditorSvc;
367 }
368 
370 
372  auto init_one = [&]( BaseToolHandle* th ) {
373  if ( !th->isEnabled() ) {
374  if ( msgLevel( MSG::DEBUG ) && !th->typeAndName().empty() )
375  debug() << "ToolHandle " << th->typeAndName() << " not used" << endmsg;
376  return;
377  }
378  if ( !th->get() ) {
379  auto sc = th->retrieve();
380  if ( sc.isFailure() ) {
381  throw GaudiException( "Failed to retrieve tool " + th->typeAndName(), this->name(), StatusCode::FAILURE );
382  }
383  }
384  auto* tool = th->get();
385  if ( msgLevel( MSG::DEBUG ) )
386  debug() << "Adding " << ( th->isPublic() ? "public" : "private" ) << " ToolHandle tool " << tool->name() << " ("
387  << tool->type() << ")" << endmsg;
388  m_tools.push_back( tool );
389  };
390 
391  for ( auto thArr : m_toolHandleArrays ) {
392  if ( msgLevel( MSG::DEBUG ) )
393  debug() << "Registering all Tools in ToolHandleArray " << thArr->propertyName() << endmsg;
394  // Iterate over its tools:
395  for ( auto toolHandle : thArr->getBaseArray() ) {
396  // Try to cast it into a BaseToolHandle pointer:
397  BaseToolHandle* bth = dynamic_cast<BaseToolHandle*>( toolHandle );
398  if ( bth ) {
399  init_one( bth );
400  } else {
401  error() << "Error retrieving Tool " << toolHandle->typeAndName() << " in ToolHandleArray "
402  << thArr->propertyName() << ". Not registered" << endmsg;
403  }
404  }
405  }
406 
407  for ( BaseToolHandle* th : m_toolHandles ) init_one( th );
408 
409  m_toolHandlesInit = true;
410 }
411 
412 const std::vector<IAlgTool*>& Service::tools() const {
414 
415  return m_tools;
416 }
417 
418 std::vector<IAlgTool*>& Service::tools() {
420 
421  return m_tools;
422 }
IDataHandleHolder
Definition: IDataHandleHolder.h:24
Service::m_auditorStart
Gaudi::Property< bool > m_auditorStart
Definition: Service.h:195
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Service::m_auditorStop
Gaudi::Property< bool > m_auditorStop
Definition: Service.h:196
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service
Definition: Service.h:207
Gaudi::IAuditor::Start
static constexpr std::string Start
Definition: IAuditor.h:49
Service::m_toolHandlesInit
bool m_toolHandlesInit
Definition: Service.h:188
Service::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Service.h:187
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:119
Service::sysInitialize
StatusCode sysInitialize() override
Initialize Service
Definition: Service.cpp:34
Service::sysRestart
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:264
Service::sysStart
StatusCode sysStart() override
Initialize Service
Definition: Service.cpp:126
Service::m_state
Gaudi::StateMachine::State m_state
Service state
Definition: Service.h:164
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:315
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:173
Service::start
StatusCode start() override
Definition: Service.cpp:188
GaudiException.h
Service::sysInitialize_imp
void sysInitialize_imp()
Definition: Service.cpp:39
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:32
Gaudi::IAuditor::Finalize
static constexpr std::string Finalize
Definition: IAuditor.h:53
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
Service::m_auditorInitialize
Gaudi::Property< bool > m_auditorInitialize
Definition: Service.h:194
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference
Definition: Service.h:179
Service::m_auditorReinitialize
Gaudi::Property< bool > m_auditorReinitialize
Definition: Service.h:198
Service::initToolHandles
void initToolHandles() const
Definition: Service.cpp:371
IDataHandleHolder::inputDataObjs
virtual const DataObjIDColl & inputDataObjs() const =0
ISvcManager
Definition: ISvcManager.h:32
Service::sysStop
StatusCode sysStop() override
Initialize Service
Definition: Service.cpp:154
IMessageSvc.h
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:224
IAuditorSvc.h
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:62
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::bindPropertiesTo
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
Definition: PropertyHolder.h:255
Gaudi::Guards::AuditorGuard
Definition: Guards.h:204
Service::m_name
std::string m_name
Service Name
Definition: Service.h:177
Service::tools
const std::vector< IAlgTool * > & tools() const
Definition: Service.cpp:412
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:174
ON_DEBUG
#define ON_DEBUG
Definition: Service.cpp:26
Service::~Service
~Service() override
Standard Destructor
Definition: Service.cpp:29
bug_34121.tool
tool
Definition: bug_34121.py:18
CommonMessaging< implements< IService, IProperty, IStateful > >::setUpMessaging
MSG::Level setUpMessaging() const
Set up local caches.
Definition: CommonMessaging.h:174
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:334
StatusCode
Definition: StatusCode.h:65
Gaudi::IAuditor::ReStart
static constexpr std::string ReStart
Definition: IAuditor.h:50
Service::m_tools
std::vector< IAlgTool * > m_tools
Definition: Service.h:185
Service::m_auditorFinalize
Gaudi::Property< bool > m_auditorFinalize
Definition: Service.h:197
IDataHandleHolder::outputDataObjs
virtual const DataObjIDColl & outputDataObjs() const =0
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:23
Service::m_auditorRestart
Gaudi::Property< bool > m_auditorRestart
Definition: Service.h:199
Gaudi::Property::declareUpdateHandler
Details::PropertyBase & declareUpdateHandler(std::function< void(Details::PropertyBase &)> fun) override
set new callback for update
Definition: Property.h:141
Gaudi::IAuditor::ReInitialize
static constexpr std::string ReInitialize
Definition: IAuditor.h:48
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:235
CommonMessaging< implements< IService, IProperty, IStateful > >::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:185
SmartIF< ISvcLocator >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
Gaudi::IAuditor::Stop
static constexpr std::string Stop
Definition: IAuditor.h:52
Service.h
Gaudi::StateMachine::ChangeState
State GAUDI_API ChangeState(const Transition transition, const State state)
Function to get the new state according to the required transition, checking if the transition is all...
Definition: StateMachine.cpp:19
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Service::stop
StatusCode stop() override
Definition: Service.cpp:182
Service::m_autoRetrieveTools
Gaudi::Property< bool > m_autoRetrieveTools
Definition: Service.h:201
BaseToolHandle
Definition: ToolHandle.h:84
Service::sysFinalize
StatusCode sysFinalize() override
Finalize Service
Definition: Service.cpp:194
IDataHandleHolder.h
ServiceLocatorHelper.h
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
Gaudi::StateMachine::START
@ START
Definition: StateMachine.h:36
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:180
Service::reinitialize
StatusCode reinitialize() override
Definition: Service.cpp:297
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor
Definition: Service.cpp:341
Service::setServiceManager
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:369
Gaudi::IAuditor::Initialize
static constexpr std::string Initialize
Definition: IAuditor.h:47
Service::sysReinitialize
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:230
Service::m_targetState
Gaudi::StateMachine::State m_targetState
Service state
Definition: Service.h:166
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Guards.h
Service::auditorSvc
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:361
ISvcLocator.h
Service::restart
StatusCode restart() override
Definition: Service.cpp:318
PropertyHolder.h
IOTest.appMgr
appMgr
Definition: IOTest.py:105
Service::m_checkToolDeps
Gaudi::Property< bool > m_checkToolDeps
Definition: Service.h:203
Gaudi::StateMachine::CONFIGURE
@ CONFIGURE
Definition: StateMachine.h:34
Service::m_outputLevel
Gaudi::Property< int > m_outputLevel
flag indicating whether ToolHandle tools have been added to m_tools
Definition: Service.h:193
Gaudi::Property< bool >
Gaudi::StateMachine::STOP
@ STOP
Definition: StateMachine.h:37
ISvcManager.h
MsgStream.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:337
Service::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: Service.h:186