All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
20 
23 }
24 
25 // IService::sysInitialize
27  StatusCode sc;
28 
29  try {
31  Gaudi::Guards::AuditorGuard guard(this,
32  // check if we want to audit the initialize
33  (m_auditorInitialize) ? auditorSvc().get() : nullptr,
35  if ((name() != "MessageSvc") && msgSvc()) // pre-set the outputLevel from the MessageSvc value
37  sc = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
38  if (sc.isSuccess())
40  return sc;
41  }
42  catch ( const GaudiException& Exception ) {
43  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag()
44  << " is caught" << endmsg;
45  error() << Exception << endmsg;
46  // Stat stat( chronoSvc() , Exception.tag() );
47  }
48  catch( const std::exception& Exception ) {
49  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
50  error() << Exception.what() << endmsg;
51  // Stat stat( chronoSvc() , "*std::exception*" );
52  }
53  catch(...) {
54  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
55  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
56  }
57 
58  return StatusCode::FAILURE;
59 }
60 
61 
62 //--- IService::initialize
64  // Set the Algorithm's properties
66  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
68  return sc ;
69 }
70 
71 // IService::sysStart
73  StatusCode sc;
74 
75  try {
77  Gaudi::Guards::AuditorGuard guard(this,
78  // check if we want to audit the initialize
79  (m_auditorStart) ? auditorSvc().get() : nullptr,
81  sc = start();
82  if (sc.isSuccess())
84  return sc;
85  }
86  catch ( const GaudiException& Exception ) {
87  fatal() << "in sysStart(): exception with tag=" << Exception.tag()
88  << " is caught" << endmsg;
89  error() << Exception << endmsg;
90  // Stat stat( chronoSvc() , Exception.tag() );
91  }
92  catch( const std::exception& Exception ) {
93  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
94  fatal() << Exception.what() << endmsg;
95  // Stat stat( chronoSvc() , "*std::exception*" );
96  }
97  catch(...) {
98  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
99  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
100  }
101 
102  return StatusCode::FAILURE;
103 }
104 
105 // IService::sysStop
107  StatusCode sc;
108 
109  try {
111  Gaudi::Guards::AuditorGuard guard(this,
112  // check if we want to audit the initialize
113  (m_auditorStop) ? auditorSvc().get() : nullptr,
115  sc = stop();
116  if (sc.isSuccess())
118  return sc;
119  }
120  catch ( const GaudiException& Exception ) {
121  fatal() << "in sysStop(): exception with tag=" << Exception.tag()
122  << " is caught" << endmsg;
123  error() << Exception << endmsg;
124  // Stat stat( chronoSvc() , Exception.tag() );
125  }
126  catch( const std::exception& Exception ) {
127  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
128  error() << Exception.what() << endmsg;
129  // Stat stat( chronoSvc() , "*std::exception*" );
130  }
131  catch(...) {
132  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
133  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
134  }
135 
136  return StatusCode::FAILURE;
137 }
138 
139 
140 //--- IService::stop
142  // stub implementation
143  return StatusCode::SUCCESS;
144 }
145 
146 //--- IService::start
148  // stub implementation
149  return StatusCode::SUCCESS;
150 }
151 
152 //--- IService::sysFinalize
154 
156 
157  try {
159  Gaudi::Guards::AuditorGuard guard(this,
160  // check if we want to audit the initialize
161  (m_auditorFinalize) ? auditorSvc().get() : nullptr,
163  sc = finalize();
164  if (sc.isSuccess())
166  }
167  catch( const GaudiException& Exception ) {
168  fatal() << " Exception with tag=" << Exception.tag()
169  << " is caught " << endmsg;
170  error() << Exception << endmsg;
171  // Stat stat( chronoSvc() , Exception.tag() ) ;
172  }
173  catch( const std::exception& Exception ) {
174  fatal() << " Standard std::exception is caught " << endmsg;
175  error() << Exception.what() << endmsg;
176  // Stat stat( chronoSvc() , "*std::exception*" ) ;
177  }
178  catch( ... ) {
179  fatal() << "UNKNOWN Exception is caught " << endmsg;
180  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
181  }
182 
183  m_pAuditorSvc = nullptr;
184  return sc;
185 }
186 
187 //--- IService::finalize
189  //MsgStream log(msgSvc(),name());
190  //m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
191  return StatusCode::SUCCESS;
192 }
193 
194 //--- IService::sysReinitialize
196 
197  StatusCode sc;
198 
199  // Check that the current status is the correct one.
201  MsgStream log ( msgSvc() , name() );
202  error()
203  << "sysReinitialize(): cannot reinitialize service not initialized"
204  << endmsg;
205  return StatusCode::FAILURE;
206  }
207 
208  try {
209 
210  Gaudi::Guards::AuditorGuard guard(this,
211  // check if we want to audit the initialize
212  (m_auditorReinitialize) ? auditorSvc().get() : nullptr,
214  sc = reinitialize();
215  return sc;
216  }
217  catch( const GaudiException& Exception ) {
218  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
219  fatal() << " Exception with tag=" << Exception.tag()
220  << " is caught " << endmsg;
221  error() << Exception << endmsg;
222  // Stat stat( chronoSvc() , Exception.tag() ) ;
223  }
224  catch( const std::exception& Exception ) {
225  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
226  fatal() << " Standard std::exception is caught " << endmsg;
227  error() << Exception.what() << endmsg;
228  // Stat stat( chronoSvc() , "*std::exception*" ) ;
229  }
230  catch( ... ) {
231  MsgStream log ( msgSvc() , name() + ".sysReinitialize()" );
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  MsgStream log ( msgSvc() , name() );
247  error()
248  << "sysRestart(): cannot restart service in state " << FSMState()
249  << " -- must be RUNNING "
250  << endmsg;
251  return StatusCode::FAILURE;
252  }
253 
254  try {
255 
256  Gaudi::Guards::AuditorGuard guard(this,
257  // check if we want to audit the initialize
258  (m_auditorRestart) ? auditorSvc().get() : nullptr,
260  sc = restart();
261  return sc;
262  }
263  catch( const GaudiException& Exception ) {
264  fatal() << " Exception with tag=" << Exception.tag()
265  << " is caught " << endmsg;
266  error() << Exception << endmsg;
267  // Stat stat( chronoSvc() , Exception.tag() ) ;
268  }
269  catch( const std::exception& Exception ) {
270  fatal() << " Standard std::exception is caught " << endmsg;
271  error() << Exception.what() << endmsg;
272  // Stat stat( chronoSvc() , "*std::exception*" ) ;
273  }
274  catch( ... ) {
275  fatal() << "UNKNOWN Exception is caught " << endmsg;
276  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
277  }
278  return StatusCode::FAILURE ;
279 
280 }
281 
282 //--- IService::reinitialize
284  /* @TODO
285  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
286  * is causing too many problems
287  *
288  // Default implementation is finalize+initialize
289  StatusCode sc = finalize();
290  if (sc.isFailure()) {
291  error() << "reinitialize(): cannot be finalized" << endmsg;
292  return sc;
293  }
294  sc = initialize();
295  if (sc.isFailure()) {
296  error() << "reinitialize(): cannot be initialized" << endmsg;
297  return sc;
298  }
299  */
300  return StatusCode::SUCCESS;
301 }
302 
303 //--- IService::restart
305  // Default implementation is stop+start
306  StatusCode sc = stop();
307  if (sc.isFailure()) {
308  error() << "restart(): cannot be stopped" << endmsg;
309  return sc;
310  }
311  sc = start();
312  if (sc.isFailure()) {
313  error() << "restart(): cannot be started" << endmsg;
314  return sc;
315  }
316  return StatusCode::SUCCESS;
317 }
318 
319 //--- IService::getServiceName
320 const std::string& Service::name() const {
321  return m_name;
322 }
323 
324 //--- Retrieve pointer to service locator
326  return m_svcLocator;
327 }
328 
329 // IProperty implementation
330 // Delegate to the Property manager
332  return m_propertyMgr->setProperty(p);
333 }
334 
335 StatusCode Service::setProperty(const std::string& s) {
336  return m_propertyMgr->setProperty(s);
337 }
338 
339 StatusCode Service::setProperty(const std::string& n, const std::string& v) {
340  return m_propertyMgr->setProperty(n,v);
341 }
342 
344  return m_propertyMgr->getProperty(p);
345 }
346 
347 const Property& Service::getProperty(const std::string& n) const {
348  return m_propertyMgr->getProperty(n);
349 }
350 
351 StatusCode Service::getProperty(const std::string& n, std::string& v ) const {
352  return m_propertyMgr->getProperty(n,v);
353 }
354 
355 const std::vector<Property*>& Service::getProperties() const {
356  return m_propertyMgr->getProperties();
357 }
358 
359 bool Service::hasProperty(const std::string& name) const {
360  return m_propertyMgr->hasProperty(name);
361 }
362 
363 // Use the job options service to set declared properties
365  const bool CREATEIF(true);
366  auto jos = serviceLocator()->service<IJobOptionsSvc>("JobOptionsSvc", CREATEIF);
367  if( !jos ) {
368  throw GaudiException("Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE);
369  }
370  // set first generic Properties
371  StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName(name()), this );
372  if( sc.isFailure() ) return sc;
373 
374  // set specific Properties
375  if (isGaudiThreaded(name())) {
376  if (jos->setMyProperties( name(), this ).isFailure()) {
377  return StatusCode::FAILURE;
378  }
379  }
380  return StatusCode::SUCCESS;
381 }
382 
383 
384 //--- Local methods
385 // Standard Constructor
386 Service::Service(std::string name, ISvcLocator* svcloc) :
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") && msgSvc() ) {
426  }
428 }
429 
430 
432  if ( !m_pAuditorSvc ) {
433  m_pAuditorSvc = serviceLocator()->service("AuditorSvc");
434  if( !m_pAuditorSvc ) {
435  throw GaudiException("Service [AuditorSvc] not found", name(), StatusCode::FAILURE);
436  }
437  }
438  return m_pAuditorSvc;
439 }
440 
441 void
443  m_svcManager = ism;
444 }
BooleanProperty m_auditInit
Definition: Service.h:329
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:311
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:321
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:307
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:325
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:330
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:309
StatusCode finalize() override
Definition: Service.cpp:188
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:54
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:431
StatusCode setProperty(const Property &p) override
set the property form another property
StatusCode start() override
Definition: Service.cpp:147
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:240
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:72
StatusCode setProperty(const Property &p) override
Definition: Service.cpp:331
STL namespace.
std::string m_name
Service Name.
Definition: Service.h:318
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:35
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:320
const std::vector< Property * > & getProperties() const override
Definition: Service.cpp:355
virtual int outputLevel() const =0
Retrieve the current output level threshold.
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:106
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: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:333
StatusCode reinitialize() override
Definition: Service.cpp:283
bool m_auditorReinitialize
Definition: Service.h:334
StatusCode stop() override
Definition: Service.cpp:141
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:153
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:364
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: Service.h:323
#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:320
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:195
bool m_auditorStart
Definition: Service.h:331
bool hasProperty(const std::string &name) const override
Definition: Service.cpp:359
StatusCode getProperty(Property *p) const override
Definition: Service.cpp:343
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.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
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:212
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:442
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:304
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:328
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
bool m_auditorRestart
Definition: Service.h:335
bool m_auditorStop
Definition: Service.h:332
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:26