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