Service.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/Service.h"
3 #include "GaudiKernel/PropertyMgr.h"
4 #include "GaudiKernel/IMessageSvc.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/ISvcLocator.h"
7 #include "GaudiKernel/ISvcManager.h"
8 #include "GaudiKernel/IAuditorSvc.h"
9 #include "GaudiKernel/IJobOptionsSvc.h"
10 #include "GaudiKernel/GaudiException.h"
11 #include "GaudiKernel/ServiceLocatorHelper.h"
12 #include "GaudiKernel/ThreadGaudi.h"
13 #include "GaudiKernel/Guards.h"
14 
15 using std::string;
16 
17 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
18 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
19 
22 }
23 
24 // IService::sysInitialize
26  StatusCode sc;
27 
28  try {
30  Gaudi::Guards::AuditorGuard guard(this,
31  // check if we want to audit the initialize
32  (m_auditorInitialize) ? auditorSvc().get() : 0,
34  if ((name() != "MessageSvc") && msgSvc()) // pre-set the outputLevel from the MessageSvc value
36  sc = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
37  if (sc.isSuccess())
39  return sc;
40  }
41  catch ( const GaudiException& Exception ) {
42  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag()
43  << " is caught" << endmsg;
44  error() << Exception << endmsg;
45  // Stat stat( chronoSvc() , Exception.tag() );
46  }
47  catch( const std::exception& Exception ) {
48  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
49  error() << Exception.what() << endmsg;
50  // Stat stat( chronoSvc() , "*std::exception*" );
51  }
52  catch(...) {
53  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
54  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
55  }
56 
57  return StatusCode::FAILURE;
58 }
59 
60 
61 //--- IService::initialize
63  // Set the Algorithm's properties
65  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
67  return sc ;
68 }
69 
70 // IService::sysStart
72  StatusCode sc;
73 
74  try {
76  Gaudi::Guards::AuditorGuard guard(this,
77  // check if we want to audit the initialize
78  (m_auditorStart) ? auditorSvc().get() : 0,
80  sc = start();
81  if (sc.isSuccess())
83  return sc;
84  }
85  catch ( const GaudiException& Exception ) {
86  fatal() << "in sysStart(): exception with tag=" << Exception.tag()
87  << " is caught" << endmsg;
88  error() << Exception << endmsg;
89  // Stat stat( chronoSvc() , Exception.tag() );
90  }
91  catch( const std::exception& Exception ) {
92  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
93  fatal() << Exception.what() << endmsg;
94  // Stat stat( chronoSvc() , "*std::exception*" );
95  }
96  catch(...) {
97  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
98  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
99  }
100 
101  return StatusCode::FAILURE;
102 }
103 
104 // IService::sysStop
106  StatusCode sc;
107 
108  try {
110  Gaudi::Guards::AuditorGuard guard(this,
111  // check if we want to audit the initialize
112  (m_auditorStop) ? auditorSvc().get() : 0,
114  sc = stop();
115  if (sc.isSuccess())
117  return sc;
118  }
119  catch ( const GaudiException& Exception ) {
120  fatal() << "in sysStop(): exception with tag=" << Exception.tag()
121  << " is caught" << endmsg;
122  error() << Exception << endmsg;
123  // Stat stat( chronoSvc() , Exception.tag() );
124  }
125  catch( const std::exception& Exception ) {
126  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
127  error() << Exception.what() << endmsg;
128  // Stat stat( chronoSvc() , "*std::exception*" );
129  }
130  catch(...) {
131  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
132  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
133  }
134 
135  return StatusCode::FAILURE;
136 }
137 
138 
139 //--- IService::stop
141  // stub implementation
142  return StatusCode::SUCCESS;
143 }
144 
145 //--- IService::start
147  // stub implementation
148  return StatusCode::SUCCESS;
149 }
150 
151 //--- IService::sysFinalize
153 
155 
156  try {
158  Gaudi::Guards::AuditorGuard guard(this,
159  // check if we want to audit the initialize
160  (m_auditorFinalize) ? auditorSvc().get() : 0,
162  sc = finalize();
163  if (sc.isSuccess())
165  }
166  catch( const GaudiException& Exception ) {
167  fatal() << " Exception with tag=" << Exception.tag()
168  << " is caught " << endmsg;
169  error() << Exception << endmsg;
170  // Stat stat( chronoSvc() , Exception.tag() ) ;
171  }
172  catch( const std::exception& Exception ) {
173  fatal() << " Standard std::exception is caught " << endmsg;
174  error() << Exception.what() << endmsg;
175  // Stat stat( chronoSvc() , "*std::exception*" ) ;
176  }
177  catch( ... ) {
178  fatal() << "UNKNOWN Exception is caught " << endmsg;
179  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
180  }
181 
182  m_pAuditorSvc = 0;
183  return sc;
184 }
185 
186 //--- IService::finalize
188  //MsgStream log(msgSvc(),name());
189  //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
190  return StatusCode::SUCCESS;
191 }
192 
193 //--- IService::sysReinitialize
195 
196  StatusCode sc;
197 
198  // Check that the current status is the correct one.
200  MsgStream log ( msgSvc() , name() );
201  error()
202  << "sysReinitialize(): cannot reinitialize service not initialized"
203  << endmsg;
204  return StatusCode::FAILURE;
205  }
206 
207  try {
208 
209  Gaudi::Guards::AuditorGuard guard(this,
210  // check if we want to audit the initialize
211  (m_auditorReinitialize) ? auditorSvc().get() : 0,
213  sc = reinitialize();
214  return sc;
215  }
216  catch( const GaudiException& Exception ) {
217  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
218  fatal() << " Exception with tag=" << Exception.tag()
219  << " is caught " << endmsg;
220  error() << Exception << endmsg;
221  // Stat stat( chronoSvc() , Exception.tag() ) ;
222  }
223  catch( const std::exception& Exception ) {
224  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
225  fatal() << " Standard std::exception is caught " << endmsg;
226  error() << Exception.what() << endmsg;
227  // Stat stat( chronoSvc() , "*std::exception*" ) ;
228  }
229  catch( ... ) {
230  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
231  fatal() << "UNKNOWN Exception is caught " << endmsg;
232  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
233  }
234  return StatusCode::FAILURE ;
235 
236 }
237 
238 //--- IService::sysRestart
240 
241  StatusCode sc;
242 
243  // Check that the current status is the correct one.
245  MsgStream log ( msgSvc() , name() );
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() : 0,
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 
334 StatusCode Service::setProperty(const std::string& s) {
335  return m_propertyMgr->setProperty(s);
336 }
337 
338 StatusCode Service::setProperty(const std::string& n, const std::string& v) {
339  return m_propertyMgr->setProperty(n,v);
340 }
341 
343  return m_propertyMgr->getProperty(p);
344 }
345 
346 const Property& Service::getProperty(const std::string& n) const {
347  return m_propertyMgr->getProperty(n);
348 }
349 
350 StatusCode Service::getProperty(const std::string& n, std::string& v ) const {
351  return m_propertyMgr->getProperty(n,v);
352 }
353 
354 const std::vector<Property*>& Service::getProperties() const {
355  return m_propertyMgr->getProperties();
356 }
357 
358 bool Service::hasProperty(const std::string& name) const {
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  return StatusCode::SUCCESS;
380 }
381 
382 
383 //--- Local methods
384 // Standard Constructor
385 Service::Service(std::string name, ISvcLocator* svcloc) :
386  m_name( std::move(name) ),
387  m_svcLocator( svcloc ),
388  m_propertyMgr( new PropertyMgr() )
389 {
390  // Declare common Service properties with their defaults
391  if ( (name != "MessageSvc") && msgSvc() ) {
392  // In genconf a service is instantiated without the ApplicationMgr
394  }
395  declareProperty("OutputLevel", m_outputLevel);
397 
398  // Get the default setting for service auditing from the AppMgr
399  declareProperty("AuditServices", m_auditInit = true);
400 
401  bool audit(false);
402  auto appMgr = serviceLocator()->service<IProperty>("ApplicationMgr");
403  if (appMgr) {
404  const Property& prop = appMgr->getProperty("AuditServices");
405  if (m_name != "IncidentSvc") {
406  setProperty(prop).ignore();
407  audit = m_auditInit.value();
408  } else {
409  audit = false;
410  }
411  }
412 
413  declareProperty( "AuditInitialize" , m_auditorInitialize = audit );
414  declareProperty( "AuditStart" , m_auditorStart = audit );
415  declareProperty( "AuditStop" , m_auditorStop = audit );
416  declareProperty( "AuditFinalize" , m_auditorFinalize = audit );
417  declareProperty( "AuditReInitialize" , m_auditorReinitialize = audit );
418  declareProperty( "AuditReStart" , m_auditorRestart = audit );
419 }
420 
421 // Callback to set output level
423  if ( (name() != "MessageSvc") && msgSvc() ) {
425  }
427 }
428 
429 
431  if ( !m_pAuditorSvc ) {
432  m_pAuditorSvc = serviceLocator()->service("AuditorSvc");
433  if( !m_pAuditorSvc ) {
434  throw GaudiException("Service [AuditorSvc] not found", name(), StatusCode::FAILURE);
435  }
436  }
437  return m_pAuditorSvc;
438 }
439 
440 void
442  m_svcManager = ism;
443 }
BooleanProperty m_auditInit
Definition: Service.h:264
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:246
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:256
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:62
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:242
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:265
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:244
StatusCode finalize() override
Definition: Service.cpp:187
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:430
StatusCode setProperty(const Property &p) override
set the property form another property
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode start() override
Definition: Service.cpp:146
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:73
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:239
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
StatusCode sysStart() override
Initialize Service.
Definition: Service.cpp:71
StatusCode setProperty(const Property &p) override
Definition: Service.cpp:330
STL namespace.
std::string m_name
Service Name.
Definition: Service.h:253
GAUDI_API std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:50
Property manager helper class.
Definition: PropertyMgr.h:34
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
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
virtual int outputLevel() const =0
Retrieve the current output level threshold.
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:105
Main interface for the JobOptions service.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
~Service() override
Standard Destructor.
Definition: Service.cpp:20
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:268
StatusCode reinitialize() override
Definition: Service.cpp:282
bool m_auditorReinitialize
Definition: Service.h:269
StatusCode stop() override
Definition: Service.cpp:140
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:152
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
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: Service.h:258
#define ON_DEBUG
Definition: Service.cpp:17
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:255
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:194
bool m_auditorStart
Definition: Service.h:266
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:422
string s
Definition: gaudirun.py:246
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:385
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
virtual void declareUpdateHandler(std::function< void(Property &)> fun)
set new callback for update
Definition: Property.cpp:71
tuple appMgr
Definition: IOTest.py:83
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:210
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:441
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.
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:263
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
bool m_auditorRestart
Definition: Service.h:270
bool m_auditorStop
Definition: Service.h:267
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:25