The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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\***********************************************************************************/
12#include <GaudiKernel/Guards.h>
20#include <GaudiKernel/Service.h>
22
23using std::string;
24
25#define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
26#define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
27
29 if ( m_svcManager ) m_svcManager->removeService( this ).ignore();
30}
31
32// IService::sysInitialize
34 std::call_once( m_initFlag, &Service::sysInitialize_imp, this );
35 return m_initSC;
36}
37
39
40 try {
43 // check if we want to audit the initialize
44 ( m_auditorInitialize ) ? auditorSvc().get() : nullptr,
46
47 // initialize messaging (except for MessageSvc)
48 if ( name() != "MessageSvc" ) {
49 // this initializes the messaging, in case property update handlers need to print
50 // and update the property value bypassing the update handler
51 m_outputLevel.value() = setUpMessaging();
52 }
53
54 bindPropertiesTo( serviceLocator()->getOptsSvc() );
55
57 error() << "Property AutoRetrieveTools must be set TRUE if CheckToolDeps is True" << endmsg;
59 return;
60 }
61
62 m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
63
64 // Check for data dependencies in AlgTools
65 // visit all sub-algs and tools, build full set. First initialize ToolHandles if needed
66 if ( m_autoRetrieveTools ) {
67 try {
69 } catch ( const GaudiException& Exception ) {
70 error() << "Service failed to initilize ToolHandles : " << Exception << endmsg;
72 return;
73 }
75 if ( m_checkToolDeps ) {
76 for ( auto& itool : m_tools ) {
77 info() << " AlgTool: " << itool->name() << endmsg;
78 IDataHandleHolder* idh = dynamic_cast<IDataHandleHolder*>( itool );
79 if ( idh == 0 ) {
80 error() << "dcast to IDataHandleHolder failed" << endmsg;
81 continue;
82 }
83 if ( idh->inputDataObjs().size() != 0 ) {
84 error() << "Service " << name() << " holds AlgTool " << itool->name()
85 << " which holds at least one ReadDataHandle" << endmsg;
86 for ( auto& obj : idh->inputDataObjs() ) { error() << " -> InputHandle: " << obj << endmsg; }
88 }
89 if ( idh->outputDataObjs().size() != 0 ) {
90 error() << "Service " << name() << " holds AlgTool " << itool->name()
91 << " which holds at least one WriteDataHandle" << endmsg;
92 for ( auto& obj : idh->outputDataObjs() ) { error() << " -> OutputHandle: " << obj << endmsg; }
94 }
95 }
96 }
97 }
98
100 return;
101 } catch ( const GaudiException& Exception ) {
102 fatal() << "in sysInitialize(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
103 error() << Exception << endmsg;
104 // Stat stat( chronoSvc() , Exception.tag() );
105 } catch ( const std::exception& Exception ) {
106 fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
107 error() << Exception.what() << endmsg;
108 // Stat stat( chronoSvc() , "*std::exception*" );
109 } catch ( ... ) {
110 fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
111 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
112 }
113
115}
116
117//--- IService::initialize
119 ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
121 return StatusCode::SUCCESS;
122}
123
124// IService::sysStart
126 StatusCode sc;
127
128 try {
131 // check if we want to audit the initialize
132 ( m_auditorStart ) ? auditorSvc().get() : nullptr, Gaudi::IAuditor::Start );
133 sc = start();
134 if ( sc.isSuccess() ) m_state = m_targetState;
135 return sc;
136 } catch ( const GaudiException& Exception ) {
137 fatal() << "in sysStart(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
138 error() << Exception << endmsg;
139 // Stat stat( chronoSvc() , Exception.tag() );
140 } catch ( const std::exception& Exception ) {
141 fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
142 fatal() << Exception.what() << endmsg;
143 // Stat stat( chronoSvc() , "*std::exception*" );
144 } catch ( ... ) {
145 fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
146 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
147 }
148
149 return StatusCode::FAILURE;
150}
151
152// IService::sysStop
154 StatusCode sc;
155
156 try {
159 // check if we want to audit the initialize
160 ( m_auditorStop ) ? auditorSvc().get() : nullptr, Gaudi::IAuditor::Stop );
161 sc = stop();
162 if ( sc.isSuccess() ) m_state = m_targetState;
163 return sc;
164 } catch ( const GaudiException& Exception ) {
165 fatal() << "in sysStop(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
166 error() << Exception << endmsg;
167 // Stat stat( chronoSvc() , Exception.tag() );
168 } catch ( const std::exception& Exception ) {
169 fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
170 error() << Exception.what() << endmsg;
171 // Stat stat( chronoSvc() , "*std::exception*" );
172 } catch ( ... ) {
173 fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
174 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
175 }
176
177 return StatusCode::FAILURE;
178}
179
180//--- IService::stop
182 // stub implementation
183 return StatusCode::SUCCESS;
184}
185
186//--- IService::start
188 // stub implementation
189 return StatusCode::SUCCESS;
190}
191
192//--- IService::sysFinalize
194
196
197 try {
200 // check if we want to audit the initialize
201 ( m_auditorFinalize ) ? auditorSvc().get() : nullptr,
203 sc = finalize();
204 if ( sc.isSuccess() ) m_state = m_targetState;
205 } catch ( const GaudiException& Exception ) {
206 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
207 error() << Exception << endmsg;
208 // Stat stat( chronoSvc() , Exception.tag() ) ;
209 } catch ( const std::exception& Exception ) {
210 fatal() << " Standard std::exception is caught " << endmsg;
211 error() << Exception.what() << endmsg;
212 // Stat stat( chronoSvc() , "*std::exception*" ) ;
213 } catch ( ... ) {
214 fatal() << "UNKNOWN Exception is caught " << endmsg;
215 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
216 }
217
218 m_pAuditorSvc = nullptr;
219 return sc;
220}
221
222//--- IService::finalize
224 // m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
225 return StatusCode::SUCCESS;
226}
227
228//--- IService::sysReinitialize
230
231 StatusCode sc;
232
233 // Check that the current status is the correct one.
235 error() << "sysReinitialize(): cannot reinitialize service not initialized" << endmsg;
236 return StatusCode::FAILURE;
237 }
238
239 try {
240
242 // check if we want to audit the initialize
243 ( m_auditorReinitialize ) ? auditorSvc().get() : nullptr,
245 sc = reinitialize();
246 return sc;
247 } catch ( const GaudiException& Exception ) {
248 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
249 error() << Exception << endmsg;
250 // Stat stat( chronoSvc() , Exception.tag() ) ;
251 } catch ( const std::exception& Exception ) {
252 fatal() << " Standard std::exception is caught " << endmsg;
253 error() << Exception.what() << endmsg;
254 // Stat stat( chronoSvc() , "*std::exception*" ) ;
255 } catch ( ... ) {
256 fatal() << "UNKNOWN Exception is caught " << endmsg;
257 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
258 }
259 return StatusCode::FAILURE;
260}
261
262//--- IService::sysRestart
264
265 StatusCode sc;
266
267 // Check that the current status is the correct one.
269 error() << "sysRestart(): cannot restart service in state " << FSMState() << " -- must be RUNNING " << endmsg;
270 return StatusCode::FAILURE;
271 }
272
273 try {
274
276 // check if we want to audit the initialize
277 ( m_auditorRestart ) ? auditorSvc().get() : nullptr, Gaudi::IAuditor::ReStart );
278 sc = restart();
279 return sc;
280 } catch ( const GaudiException& Exception ) {
281 fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
282 error() << Exception << endmsg;
283 // Stat stat( chronoSvc() , Exception.tag() ) ;
284 } catch ( const std::exception& Exception ) {
285 fatal() << " Standard std::exception is caught " << endmsg;
286 error() << Exception.what() << endmsg;
287 // Stat stat( chronoSvc() , "*std::exception*" ) ;
288 } catch ( ... ) {
289 fatal() << "UNKNOWN Exception is caught " << endmsg;
290 // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
291 }
292 return StatusCode::FAILURE;
293}
294
295//--- IService::reinitialize
297 /* @TODO
298 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
299 * is causing too many problems
300 *
301 // Default implementation is finalize+initialize
302 StatusCode sc = finalize();
303 if (sc.isFailure()) {
304 error() << "reinitialize(): cannot be finalized" << endmsg;
305 return sc;
306 }
307 sc = initialize();
308 if (sc.isFailure()) {
309 error() << "reinitialize(): cannot be initialized" << endmsg;
310 return sc;
311 }
312 */
313 return StatusCode::SUCCESS;
314}
315
316//--- IService::restart
318 // Default implementation is stop+start
319 StatusCode sc = stop();
320 if ( sc.isFailure() ) {
321 error() << "restart(): cannot be stopped" << endmsg;
322 return sc;
323 }
324 sc = start();
325 if ( sc.isFailure() ) {
326 error() << "restart(): cannot be started" << endmsg;
327 return sc;
328 }
329 return StatusCode::SUCCESS;
330}
331
332//--- IService::getServiceName
333const std::string& Service::name() const { return m_name; }
334
335//--- Retrieve pointer to service locator
337
338//--- Local methods
339// Standard Constructor
340Service::Service( std::string name, ISvcLocator* svcloc ) : m_name( std::move( name ) ), m_svcLocator( svcloc ) {
341 if ( m_name != "MessageSvc" ) { // the MessageSvc should not notify itself
342 m_outputLevel.declareUpdateHandler(
344 }
345
346 // Initialize the default value from ApplicationMgr AuditAlgorithms
347 Gaudi::Property<bool> audit( "AuditServices", false );
348 if ( auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" ) ) {
349 appMgr->getProperty( &audit ).ignore();
350 }
351
352 m_auditorInitialize = audit;
353 m_auditorStart = audit;
354 m_auditorStop = audit;
355 m_auditorFinalize = audit;
356 m_auditorReinitialize = audit;
357 m_auditorRestart = audit;
358}
359
361 if ( !m_pAuditorSvc ) {
362 m_pAuditorSvc = serviceLocator()->service( "AuditorSvc" );
363 if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
364 }
365 return m_pAuditorSvc;
366}
367
369
371 auto init_one = [&]( BaseToolHandle* th ) {
372 if ( !th->isEnabled() ) {
373 if ( msgLevel( MSG::DEBUG ) && !th->typeAndName().empty() )
374 debug() << "ToolHandle " << th->typeAndName() << " not used" << endmsg;
375 return;
376 }
377 if ( !th->get() ) {
378 auto sc = th->retrieve();
379 if ( sc.isFailure() ) {
380 throw GaudiException( "Failed to retrieve tool " + th->typeAndName(), this->name(), StatusCode::FAILURE );
381 }
382 }
383 auto* tool = th->get();
384 if ( msgLevel( MSG::DEBUG ) )
385 debug() << "Adding " << ( th->isPublic() ? "public" : "private" ) << " ToolHandle tool " << tool->name() << " ("
386 << tool->type() << ")" << endmsg;
387 m_tools.push_back( tool );
388 };
389
390 for ( auto thArr : m_toolHandleArrays ) {
391 if ( msgLevel( MSG::DEBUG ) )
392 debug() << "Registering all Tools in ToolHandleArray " << thArr->propertyName() << endmsg;
393 // Iterate over its tools:
394 for ( auto toolHandle : thArr->getBaseArray() ) {
395 // Try to cast it into a BaseToolHandle pointer:
396 BaseToolHandle* bth = dynamic_cast<BaseToolHandle*>( toolHandle );
397 if ( bth ) {
398 init_one( bth );
399 } else {
400 error() << "Error retrieving Tool " << toolHandle->typeAndName() << " in ToolHandleArray "
401 << thArr->propertyName() << ". Not registered" << endmsg;
402 }
403 }
404 }
405
406 for ( BaseToolHandle* th : m_toolHandles ) init_one( th );
407
408 m_toolHandlesInit = true;
409}
410
411const std::vector<IAlgTool*>& Service::tools() const {
413
414 return m_tools;
415}
416
417std::vector<IAlgTool*>& Service::tools() {
419
420 return m_tools;
421}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define ON_DEBUG
Non-templated base class for actual ToolHandle<T>.
Definition ToolHandle.h:78
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition Guards.h:202
static const std::string Stop
Definition IAuditor.h:52
static const std::string Initialize
Definition IAuditor.h:47
static const std::string Finalize
Definition IAuditor.h:53
static const std::string Start
Definition IAuditor.h:49
static const std::string ReInitialize
Definition IAuditor.h:48
static const std::string ReStart
Definition IAuditor.h:50
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
virtual const DataObjIDColl & inputDataObjs() const =0
virtual const DataObjIDColl & outputDataObjs() const =0
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition ISvcManager.h:30
Gaudi::StateMachine::State FSMState() const override
Definition Service.h:55
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
Gaudi::Property< bool > m_auditorStart
Definition Service.h:186
StatusCode sysFinalize() override
Finalize Service.
Definition Service.cpp:193
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition Service.cpp:360
Gaudi::Property< bool > m_auditorFinalize
Definition Service.h:188
StatusCode sysInitialize() override
Initialize Service.
Definition Service.cpp:33
void sysInitialize_imp()
Definition Service.cpp:38
StatusCode finalize() override
Definition Service.cpp:223
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition Service.h:178
SmartIF< ISvcManager > m_svcManager
Definition Service.h:171
std::once_flag m_initFlag
Definition Service.h:165
Gaudi::Property< int > m_outputLevel
flag indicating whether ToolHandle tools have been added to m_tools
Definition Service.h:184
Gaudi::Property< bool > m_auditorStop
Definition Service.h:187
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode stop() override
Definition Service.cpp:181
Gaudi::StateMachine::State m_targetState
Service state.
Definition Service.h:157
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition Service.cpp:229
StatusCode reinitialize() override
Definition Service.cpp:296
std::vector< IAlgTool * > m_tools
Definition Service.h:176
Gaudi::Property< bool > m_auditorRestart
Definition Service.h:190
std::vector< BaseToolHandle * > m_toolHandles
Definition Service.h:177
StatusCode sysStart() override
Initialize Service.
Definition Service.cpp:125
Gaudi::StateMachine::State m_state
Service state.
Definition Service.h:155
StatusCode restart() override
Definition Service.cpp:317
Gaudi::Property< bool > m_checkToolDeps
Definition Service.h:194
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition Service.h:198
void setServiceManager(ISvcManager *ism) override
Definition Service.cpp:368
bool m_toolHandlesInit
Definition Service.h:179
Gaudi::Property< bool > m_autoRetrieveTools
Definition Service.h:192
StatusCode m_initSC
Definition Service.h:164
StatusCode sysStop() override
Initialize Service.
Definition Service.cpp:153
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition Service.h:170
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
StatusCode start() override
Definition Service.cpp:187
Gaudi::Property< bool > m_auditorInitialize
Definition Service.h:185
~Service() override
Definition Service.cpp:28
Gaudi::Property< bool > m_auditorReinitialize
Definition Service.h:189
StatusCode sysRestart() override
Re-initialize the Service.
Definition Service.cpp:263
std::string m_name
Service Name.
Definition Service.h:168
StatusCode initialize() override
Definition Service.cpp:118
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition Service.cpp:340
const std::vector< IAlgTool * > & tools() const
Definition Service.cpp:411
void initToolHandles() const
Definition Service.cpp:370
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
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...
@ DEBUG
Definition IMessageSvc.h:22
STL namespace.