The Gaudi Framework  v29r0 (ff2e7097)
Service.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/Service.h"
4 #include "GaudiKernel/Guards.h"
10 #include "GaudiKernel/MsgStream.h"
14 
15 using std::string;
16 
17 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
18 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
19 
21 {
23 }
24 
25 // IService::sysInitialize
27 {
29  return m_initSC;
30 }
31 
33 {
34 
35  try {
37  Gaudi::Guards::AuditorGuard guard( this,
38  // check if we want to audit the initialize
39  ( m_auditorInitialize ) ? auditorSvc().get() : nullptr, IAuditor::Initialize );
40 
42  if ( !m_initSC ) return;
43 
44  m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
46  return;
47  } catch ( const GaudiException& Exception ) {
48  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
49  error() << Exception << endmsg;
50  // Stat stat( chronoSvc() , Exception.tag() );
51  } catch ( const std::exception& Exception ) {
52  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
53  error() << Exception.what() << endmsg;
54  // Stat stat( chronoSvc() , "*std::exception*" );
55  } catch ( ... ) {
56  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
57  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
58  }
59 
61 }
62 
63 //--- IService::initialize
65 {
66  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
68  return StatusCode::SUCCESS;
69 }
70 
71 // IService::sysStart
73 {
74  StatusCode sc;
75 
76  try {
78  Gaudi::Guards::AuditorGuard guard( this,
79  // check if we want to audit the initialize
80  ( m_auditorStart ) ? auditorSvc().get() : nullptr, IAuditor::Start );
81  sc = start();
82  if ( sc.isSuccess() ) m_state = m_targetState;
83  return sc;
84  } catch ( const GaudiException& Exception ) {
85  fatal() << "in sysStart(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
86  error() << Exception << endmsg;
87  // Stat stat( chronoSvc() , Exception.tag() );
88  } catch ( const std::exception& Exception ) {
89  fatal() << "in sysStart(): standard std::exception is caught" << endmsg;
90  fatal() << Exception.what() << endmsg;
91  // Stat stat( chronoSvc() , "*std::exception*" );
92  } catch ( ... ) {
93  fatal() << "in sysStart(): UNKNOWN Exception is caught" << endmsg;
94  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
95  }
96 
97  return StatusCode::FAILURE;
98 }
99 
100 // IService::sysStop
102 {
103  StatusCode sc;
104 
105  try {
107  Gaudi::Guards::AuditorGuard guard( this,
108  // check if we want to audit the initialize
109  ( m_auditorStop ) ? auditorSvc().get() : nullptr, IAuditor::Stop );
110  sc = stop();
111  if ( sc.isSuccess() ) m_state = m_targetState;
112  return sc;
113  } catch ( const GaudiException& Exception ) {
114  fatal() << "in sysStop(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
115  error() << Exception << endmsg;
116  // Stat stat( chronoSvc() , Exception.tag() );
117  } catch ( const std::exception& Exception ) {
118  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
119  error() << Exception.what() << endmsg;
120  // Stat stat( chronoSvc() , "*std::exception*" );
121  } catch ( ... ) {
122  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
123  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
124  }
125 
126  return StatusCode::FAILURE;
127 }
128 
129 //--- IService::stop
131 {
132  // stub implementation
133  return StatusCode::SUCCESS;
134 }
135 
136 //--- IService::start
138 {
139  // stub implementation
140  return StatusCode::SUCCESS;
141 }
142 
143 //--- IService::sysFinalize
145 {
146 
148 
149  try {
151  Gaudi::Guards::AuditorGuard guard( this,
152  // check if we want to audit the initialize
153  ( m_auditorFinalize ) ? auditorSvc().get() : nullptr, IAuditor::Finalize );
154  sc = finalize();
155  if ( sc.isSuccess() ) m_state = m_targetState;
156  } catch ( const GaudiException& Exception ) {
157  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
158  error() << Exception << endmsg;
159  // Stat stat( chronoSvc() , Exception.tag() ) ;
160  } catch ( const std::exception& Exception ) {
161  fatal() << " Standard std::exception is caught " << endmsg;
162  error() << Exception.what() << endmsg;
163  // Stat stat( chronoSvc() , "*std::exception*" ) ;
164  } catch ( ... ) {
165  fatal() << "UNKNOWN Exception is caught " << endmsg;
166  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
167  }
168 
169  m_pAuditorSvc = nullptr;
170  return sc;
171 }
172 
173 //--- IService::finalize
175 {
176  // m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
177  return StatusCode::SUCCESS;
178 }
179 
180 //--- IService::sysReinitialize
182 {
183 
184  StatusCode sc;
185 
186  // Check that the current status is the correct one.
188  error() << "sysReinitialize(): cannot reinitialize service not initialized" << endmsg;
189  return StatusCode::FAILURE;
190  }
191 
192  try {
193 
194  Gaudi::Guards::AuditorGuard guard( this,
195  // check if we want to audit the initialize
196  ( m_auditorReinitialize ) ? auditorSvc().get() : nullptr,
198  sc = reinitialize();
199  return sc;
200  } catch ( const GaudiException& Exception ) {
201  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
202  error() << Exception << endmsg;
203  // Stat stat( chronoSvc() , Exception.tag() ) ;
204  } catch ( const std::exception& Exception ) {
205  fatal() << " Standard std::exception is caught " << endmsg;
206  error() << Exception.what() << endmsg;
207  // Stat stat( chronoSvc() , "*std::exception*" ) ;
208  } catch ( ... ) {
209  fatal() << "UNKNOWN Exception is caught " << endmsg;
210  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
211  }
212  return StatusCode::FAILURE;
213 }
214 
215 //--- IService::sysRestart
217 {
218 
219  StatusCode sc;
220 
221  // Check that the current status is the correct one.
223  error() << "sysRestart(): cannot restart service in state " << FSMState() << " -- must be RUNNING " << endmsg;
224  return StatusCode::FAILURE;
225  }
226 
227  try {
228 
229  Gaudi::Guards::AuditorGuard guard( this,
230  // check if we want to audit the initialize
231  ( m_auditorRestart ) ? auditorSvc().get() : nullptr, IAuditor::ReStart );
232  sc = restart();
233  return sc;
234  } catch ( const GaudiException& Exception ) {
235  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
236  error() << Exception << endmsg;
237  // Stat stat( chronoSvc() , Exception.tag() ) ;
238  } catch ( const std::exception& Exception ) {
239  fatal() << " Standard std::exception is caught " << endmsg;
240  error() << Exception.what() << endmsg;
241  // Stat stat( chronoSvc() , "*std::exception*" ) ;
242  } catch ( ... ) {
243  fatal() << "UNKNOWN Exception is caught " << endmsg;
244  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
245  }
246  return StatusCode::FAILURE;
247 }
248 
249 //--- IService::reinitialize
251 {
252  /* @TODO
253  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
254  * is causing too many problems
255  *
256  // Default implementation is finalize+initialize
257  StatusCode sc = finalize();
258  if (sc.isFailure()) {
259  error() << "reinitialize(): cannot be finalized" << endmsg;
260  return sc;
261  }
262  sc = initialize();
263  if (sc.isFailure()) {
264  error() << "reinitialize(): cannot be initialized" << endmsg;
265  return sc;
266  }
267  */
268  return StatusCode::SUCCESS;
269 }
270 
271 //--- IService::restart
273 {
274  // Default implementation is stop+start
275  StatusCode sc = stop();
276  if ( sc.isFailure() ) {
277  error() << "restart(): cannot be stopped" << endmsg;
278  return sc;
279  }
280  sc = start();
281  if ( sc.isFailure() ) {
282  error() << "restart(): cannot be started" << endmsg;
283  return sc;
284  }
285  return StatusCode::SUCCESS;
286 }
287 
288 //--- IService::getServiceName
289 const std::string& Service::name() const { return m_name; }
290 
291 //--- Retrieve pointer to service locator
293 
294 // Use the job options service to set declared properties
296 {
297  const bool CREATEIF( true );
298  auto jos = serviceLocator()->service<IJobOptionsSvc>( "JobOptionsSvc", CREATEIF );
299  if ( !jos ) {
300  throw GaudiException( "Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE );
301  }
302  // set first generic Properties
303  StatusCode sc = jos->setMyProperties( getGaudiThreadGenericName( name() ), this );
304  if ( sc.isFailure() ) return sc;
305 
306  // set specific Properties
307  if ( isGaudiThreaded( name() ) ) {
308  if ( jos->setMyProperties( name(), this ).isFailure() ) {
309  return StatusCode::FAILURE;
310  }
311  }
312  // initialize output level (except for MessageSvc)
313  if ( name() != "MessageSvc" && msgSvc() ) {
314  if ( m_outputLevel == MSG::NIL ) // if not defined (via options)
315  m_outputLevel = msgLevel(); // set it from MessageSvc
316  else // otherwise notify MessageSvc
318  }
319  return sc;
320 }
321 
322 //--- Local methods
323 // Standard Constructor
324 Service::Service( std::string name, ISvcLocator* svcloc ) : m_name( std::move( name ) ), m_svcLocator( svcloc )
325 {
326  if ( m_name != "MessageSvc" ) { // the MessageSvc should not notify itself
327  m_outputLevel.declareUpdateHandler(
329  }
330 
331  // Initialize the default value from ApplicationMgr AuditAlgorithms
332  Gaudi::Property<bool> audit( false );
333  auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" );
334  if ( appMgr && appMgr->hasProperty( "AuditServices" ) ) {
335  audit.assign( appMgr->getProperty( "AuditServices" ) );
336  }
337  m_auditInit = audit;
338  m_auditorInitialize = audit;
339  m_auditorStart = audit;
340  m_auditorStop = audit;
341  m_auditorFinalize = audit;
342  m_auditorReinitialize = audit;
343  m_auditorRestart = audit;
344 }
345 
347 {
348  if ( !m_pAuditorSvc ) {
349  m_pAuditorSvc = serviceLocator()->service( "AuditorSvc" );
350  if ( !m_pAuditorSvc ) {
351  throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE );
352  }
353  }
354  return m_pAuditorSvc;
355 }
356 
Gaudi::Property< int > m_outputLevel
Definition: Service.h:183
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:162
StatusCode initialize() override
Definition: Service.cpp:64
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
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:289
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:160
Gaudi::Property< bool > m_auditorInitialize
Definition: Service.h:185
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:193
StatusCode finalize() override
Definition: Service.cpp:174
Implementation of property with value of concrete type.
Definition: Property.h:319
void sysInitialize_imp()
Definition: Service.cpp:32
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:346
StatusCode m_initSC
Definition: Service.h:169
StatusCode start() override
Definition: Service.cpp:137
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:75
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:216
StatusCode sysStart() override
Initialize Service.
Definition: Service.cpp:72
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:175
STL namespace.
Gaudi::Property< bool > m_auditInit
Definition: Service.h:184
Gaudi::Property< bool > m_auditorRestart
Definition: Service.h:190
std::string m_name
Service Name.
Definition: Service.h:173
Gaudi::Property< bool > m_auditorReinitialize
Definition: Service.h:189
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:76
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:101
Main interface for the JobOptions service.
T what(T...args)
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:28
Gaudi::Property< bool > m_auditorFinalize
Definition: Service.h:188
virtual const std::string & tag() const
name tag for the exception, or exception type
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
StatusCode reinitialize() override
Definition: Service.cpp:250
StatusCode stop() override
Definition: Service.cpp:130
std::once_flag m_initFlag
Definition: Service.h:170
STL class.
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:144
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:295
#define ON_DEBUG
Definition: Service.cpp:17
StatusCode sysReinitialize() override
Re-initialize the Service.
Definition: Service.cpp:181
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...
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
appMgr
Definition: IOTest.py:94
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:324
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:54
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Gaudi::Property< bool > m_auditorStart
Definition: Service.h:186
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:631
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:176
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
void ignore() const
Definition: StatusCode.h:109
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:357
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:215
StatusCode restart() override
Definition: Service.cpp:272
virtual StatusCode removeService(IService *svc)=0
Remove a service from the "active" list of services of the factory.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
Gaudi::Property< bool > m_auditorStop
Definition: Service.h:187
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
GAUDI_API std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:52
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:26