Service.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/Service.h"
13 #include "GaudiKernel/Guards.h"
14 
15 using std::string;
16 
17 #define ON_DEBUG if (msgLevel(MSG::DEBUG))
18 #define ON_VERBOSE if (msgLevel(MSG::VERBOSE))
19 
20 
23 }
24 
25 // IService::sysInitialize
28  return m_initSC;
29 }
30 
32 
33  try {
35  Gaudi::Guards::AuditorGuard guard(this,
36  // check if we want to audit the initialize
37  (m_auditorInitialize) ? auditorSvc().get() : nullptr,
39  if ((name() != "MessageSvc") && msgSvc()) // pre-set the outputLevel from the MessageSvc value
41  m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
42  if (m_initSC.isSuccess())
44  return;
45  }
46  catch ( const GaudiException& Exception ) {
47  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag()
48  << " is caught" << endmsg;
49  error() << Exception << endmsg;
50  // Stat stat( chronoSvc() , Exception.tag() );
51  }
52  catch( const std::exception& Exception ) {
53  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
54  error() << Exception.what() << endmsg;
55  // Stat stat( chronoSvc() , "*std::exception*" );
56  }
57  catch(...) {
58  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
59  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
60  }
61 
63 
64 }
65 
66 
67 //--- IService::initialize
69  // Set the Algorithm's properties
71  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
73  return sc ;
74 }
75 
76 // IService::sysStart
78  StatusCode sc;
79 
80  try {
82  Gaudi::Guards::AuditorGuard guard(this,
83  // check if we want to audit the initialize
84  (m_auditorStart) ? auditorSvc().get() : nullptr,
86  sc = start();
87  if (sc.isSuccess())
89  return sc;
90  }
91  catch ( const GaudiException& Exception ) {
92  fatal() << "in sysStart(): exception with tag=" << Exception.tag()
93  << " is caught" << endmsg;
94  error() << Exception << endmsg;
95  // Stat stat( chronoSvc() , Exception.tag() );
96  }
97  catch( const std::exception& Exception ) {
98  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
99  fatal() << Exception.what() << endmsg;
100  // Stat stat( chronoSvc() , "*std::exception*" );
101  }
102  catch(...) {
103  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
104  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
105  }
106 
107  return StatusCode::FAILURE;
108 }
109 
110 // IService::sysStop
112  StatusCode sc;
113 
114  try {
116  Gaudi::Guards::AuditorGuard guard(this,
117  // check if we want to audit the initialize
118  (m_auditorStop) ? auditorSvc().get() : nullptr,
120  sc = stop();
121  if (sc.isSuccess())
123  return sc;
124  }
125  catch ( const GaudiException& Exception ) {
126  fatal() << "in sysStop(): exception with tag=" << Exception.tag()
127  << " is caught" << endmsg;
128  error() << Exception << endmsg;
129  // Stat stat( chronoSvc() , Exception.tag() );
130  }
131  catch( const std::exception& Exception ) {
132  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
133  error() << Exception.what() << endmsg;
134  // Stat stat( chronoSvc() , "*std::exception*" );
135  }
136  catch(...) {
137  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
138  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
139  }
140 
141  return StatusCode::FAILURE;
142 }
143 
144 
145 //--- IService::stop
147  // stub implementation
148  return StatusCode::SUCCESS;
149 }
150 
151 //--- IService::start
153  // stub implementation
154  return StatusCode::SUCCESS;
155 }
156 
157 //--- IService::sysFinalize
159 
161 
162  try {
164  Gaudi::Guards::AuditorGuard guard(this,
165  // check if we want to audit the initialize
166  (m_auditorFinalize) ? auditorSvc().get() : nullptr,
168  sc = finalize();
169  if (sc.isSuccess())
171  }
172  catch( const GaudiException& Exception ) {
173  fatal() << " Exception with tag=" << Exception.tag()
174  << " is caught " << endmsg;
175  error() << Exception << endmsg;
176  // Stat stat( chronoSvc() , Exception.tag() ) ;
177  }
178  catch( const std::exception& Exception ) {
179  fatal() << " Standard std::exception is caught " << endmsg;
180  error() << Exception.what() << endmsg;
181  // Stat stat( chronoSvc() , "*std::exception*" ) ;
182  }
183  catch( ... ) {
184  fatal() << "UNKNOWN Exception is caught " << endmsg;
185  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
186  }
187 
188  m_pAuditorSvc = nullptr;
189  return sc;
190 }
191 
192 //--- IService::finalize
194  //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
195  return StatusCode::SUCCESS;
196 }
197 
198 //--- IService::sysReinitialize
200 
201  StatusCode sc;
202 
203  // Check that the current status is the correct one.
205  error()
206  << "sysReinitialize(): cannot reinitialize service not initialized"
207  << endmsg;
208  return StatusCode::FAILURE;
209  }
210 
211  try {
212 
213  Gaudi::Guards::AuditorGuard guard(this,
214  // check if we want to audit the initialize
215  (m_auditorReinitialize) ? auditorSvc().get() : nullptr,
217  sc = reinitialize();
218  return sc;
219  }
220  catch( const GaudiException& Exception ) {
221  fatal() << " Exception with tag=" << Exception.tag()
222  << " is caught " << endmsg;
223  error() << Exception << endmsg;
224  // Stat stat( chronoSvc() , Exception.tag() ) ;
225  }
226  catch( const std::exception& Exception ) {
227  fatal() << " Standard std::exception is caught " << endmsg;
228  error() << Exception.what() << endmsg;
229  // Stat stat( chronoSvc() , "*std::exception*" ) ;
230  }
231  catch( ... ) {
232  fatal() << "UNKNOWN Exception is caught " << endmsg;
233  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
234  }
235  return StatusCode::FAILURE ;
236 
237 }
238 
239 //--- IService::sysRestart
241 
242  StatusCode sc;
243 
244  // Check that the current status is the correct one.
246  error()
247  << "sysRestart(): cannot restart service in state " << FSMState()
248  << " -- must be RUNNING "
249  << endmsg;
250  return StatusCode::FAILURE;
251  }
252 
253  try {
254 
255  Gaudi::Guards::AuditorGuard guard(this,
256  // check if we want to audit the initialize
257  (m_auditorRestart) ? auditorSvc().get() : nullptr,
259  sc = restart();
260  return sc;
261  }
262  catch( const GaudiException& Exception ) {
263  fatal() << " Exception with tag=" << Exception.tag()
264  << " is caught " << endmsg;
265  error() << Exception << endmsg;
266  // Stat stat( chronoSvc() , Exception.tag() ) ;
267  }
268  catch( const std::exception& Exception ) {
269  fatal() << " Standard std::exception is caught " << endmsg;
270  error() << Exception.what() << endmsg;
271  // Stat stat( chronoSvc() , "*std::exception*" ) ;
272  }
273  catch( ... ) {
274  fatal() << "UNKNOWN Exception is caught " << endmsg;
275  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
276  }
277  return StatusCode::FAILURE ;
278 
279 }
280 
281 //--- IService::reinitialize
283  /* @TODO
284  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
285  * is causing too many problems
286  *
287  // Default implementation is finalize+initialize
288  StatusCode sc = finalize();
289  if (sc.isFailure()) {
290  error() << "reinitialize(): cannot be finalized" << endmsg;
291  return sc;
292  }
293  sc = initialize();
294  if (sc.isFailure()) {
295  error() << "reinitialize(): cannot be initialized" << endmsg;
296  return sc;
297  }
298  */
299  return StatusCode::SUCCESS;
300 }
301 
302 //--- IService::restart
304  // Default implementation is stop+start
305  StatusCode sc = stop();
306  if (sc.isFailure()) {
307  error() << "restart(): cannot be stopped" << endmsg;
308  return sc;
309  }
310  sc = start();
311  if (sc.isFailure()) {
312  error() << "restart(): cannot be started" << endmsg;
313  return sc;
314  }
315  return StatusCode::SUCCESS;
316 }
317 
318 //--- IService::getServiceName
319 const std::string& Service::name() const {
320  return m_name;
321 }
322 
323 //--- Retrieve pointer to service locator
325  return m_svcLocator;
326 }
327 
328 // IProperty implementation
329 // Delegate to the Property manager
331  return m_propertyMgr->setProperty(p);
332 }
333 
335  return m_propertyMgr->setProperty(s);
336 }
337 
339  return m_propertyMgr->setProperty(n,v);
340 }
341 
343  return m_propertyMgr->getProperty(p);
344 }
345 
347  return m_propertyMgr->getProperty(n);
348 }
349 
351  return m_propertyMgr->getProperty(n,v);
352 }
353 
355  return m_propertyMgr->getProperties();
356 }
357 
359  return m_propertyMgr->hasProperty(name);
360 }
361 
362 // Use the job options service to set declared properties
364  const bool CREATEIF(true);
365  auto jos = serviceLocator()->service<IJobOptionsSvc>("JobOptionsSvc", CREATEIF);
366  if( !jos ) {
367  throw GaudiException("Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE);
368  }
369  // set first generic Properties
370  StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
371  if( sc.isFailure() ) return sc;
372 
373  // set specific Properties
374  if (isGaudiThreaded(name())) {
375  if (jos->setMyProperties( name(), this ).isFailure()) {
376  return StatusCode::FAILURE;
377  }
378  }
379  if (name() != "MessageSvc") updateMsgStreamOutputLevel( m_outputLevel );
380  return StatusCode::SUCCESS;
381 }
382 
383 
384 //--- Local methods
385 // Standard Constructor
387  m_name( std::move(name) ),
388  m_svcLocator( svcloc ),
389  m_propertyMgr( new PropertyMgr() )
390 {
391  // Declare common Service properties with their defaults
392  if ( (name != "MessageSvc") && msgSvc() ) {
393  // In genconf a service is instantiated without the ApplicationMgr
395  }
396  declareProperty("OutputLevel", m_outputLevel);
398 
399  // Get the default setting for service auditing from the AppMgr
400  declareProperty("AuditServices", m_auditInit = true);
401 
402  bool audit(false);
403  auto appMgr = serviceLocator()->service<IProperty>("ApplicationMgr");
404  if (appMgr) {
405  const Property& prop = appMgr->getProperty("AuditServices");
406  if (m_name != "IncidentSvc") {
407  setProperty(prop).ignore();
408  audit = m_auditInit.value();
409  } else {
410  audit = false;
411  }
412  }
413 
414  declareProperty( "AuditInitialize" , m_auditorInitialize = audit );
415  declareProperty( "AuditStart" , m_auditorStart = audit );
416  declareProperty( "AuditStop" , m_auditorStop = audit );
417  declareProperty( "AuditFinalize" , m_auditorFinalize = audit );
418  declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit );
419  declareProperty( "AuditReStart" , m_auditorRestart = audit );
420 }
421 
422 // Callback to set output level
424  if ( name() != "MessageSvc") updateMsgStreamOutputLevel(m_outputLevel);
425 }
426 
427 
429  if ( !m_pAuditorSvc ) {
430  m_pAuditorSvc = serviceLocator()->service("AuditorSvc");
431  if( !m_pAuditorSvc ) {
432  throw GaudiException("Service [AuditorSvc] not found", name(), StatusCode::FAILURE);
433  }
434  }
435  return m_pAuditorSvc;
436 }
437 
438 void
440  m_svcManager = ism;
441 }
BooleanProperty m_auditInit
Definition: Service.h:336
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:313
StatusCode initialize() override
Definition: Service.cpp:68
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:309
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
bool m_auditorInitialize
Definition: Service.h:337
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:311
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:335
StatusCode finalize() override
Definition: Service.cpp:193
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:57
void sysInitialize_imp()
Definition: Service.cpp:31
virtual Property & declareUpdateHandler(std::function< void(Property &)> fun)
set new callback for update
Definition: Property.cpp:72
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:428
StatusCode setProperty(const Property &p) override
set the property form another property
StatusCode m_initSC
Definition: Service.h:321
StatusCode start() override
Definition: Service.cpp:152
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:240
StatusCode sysStart() override
Initialize Service.
Definition: Service.cpp:77
StatusCode setProperty(const Property &p) override
Definition: Service.cpp:330
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:327
STL namespace.
std::string m_name
Service Name.
Definition: Service.h:325
Property manager helper class.
Definition: PropertyMgr.h:37
T call_once(T...args)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:73
STL class.
StatusCode getProperty(Property *p) const override
get the property
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:319
const std::vector< Property * > & getProperties() const override
Definition: Service.cpp:354
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual int outputLevel() const =0
Retrieve the current output level threshold.
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:111
Main interface for the JobOptions service.
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: Service.h:330
T what(T...args)
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
~Service() override
Standard Destructor.
Definition: Service.cpp:21
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
const std::vector< Property * > & getProperties() const override
get all properties
virtual const std::string & tag() const
name tag for the exception, or exception type
bool m_auditorFinalize
Definition: Service.h:340
StatusCode reinitialize() override
Definition: Service.cpp:282
bool m_auditorReinitialize
Definition: Service.h:341
StatusCode stop() override
Definition: Service.cpp:146
std::once_flag m_initFlag
Definition: Service.h:322
STL class.
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:158
const TYPE & value() const
explicit conversion
Definition: Property.h:341
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:363
#define ON_DEBUG
Definition: Service.cpp:17
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:199
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:8
bool m_auditorStart
Definition: Service.h:338
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool hasProperty(const std::string &name) const override
Definition: Service.cpp:358
StatusCode getProperty(Property *p) const override
Definition: Service.cpp:342
void initOutputLevel(Property &prop)
callback for output level property
Definition: Service.cpp:423
string s
Definition: gaudirun.py:245
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:386
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
tuple appMgr
Definition: IOTest.py:83
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:328
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
void ignore() const
Definition: StatusCode.h:108
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:215
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:439
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:214
StatusCode restart() override
Definition: Service.cpp:303
virtual StatusCode removeService(IService *svc)=0
Remove a service from the "active" list of services of the factory.
bool m_auditorRestart
Definition: Service.h:342
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
bool m_auditorStop
Definition: Service.h:339
GAUDI_API std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:50
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:26