The Gaudi Framework  v33r0 (d5ea422b)
Service.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // Include Files
12 #include "GaudiKernel/Service.h"
14 #include "GaudiKernel/Guards.h"
20 #include "GaudiKernel/MsgStream.h"
23 
24 using std::string;
25 
26 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
27 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
28 
31 }
32 
33 // IService::sysInitialize
36  return m_initSC;
37 }
38 
40 
41  try {
43  Gaudi::Guards::AuditorGuard guard( this,
44  // check if we want to audit the initialize
46 
48  if ( !m_initSC ) return;
49 
50  m_initSC = initialize(); // This should change the state to Gaudi::StateMachine::CONFIGURED
52  return;
53  } catch ( const GaudiException& Exception ) {
54  fatal() << "in sysInitialize(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
55  error() << Exception << endmsg;
56  // Stat stat( chronoSvc() , Exception.tag() );
57  } catch ( const std::exception& Exception ) {
58  fatal() << "in sysInitialize(): standard std::exception is caught" << endmsg;
59  error() << Exception.what() << endmsg;
60  // Stat stat( chronoSvc() , "*std::exception*" );
61  } catch ( ... ) {
62  fatal() << "in sysInitialize(): UNKNOWN Exception is caught" << endmsg;
63  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
64  }
65 
67 }
68 
69 //--- IService::initialize
71  ON_DEBUG debug() << "Service base class initialized successfully" << endmsg;
73  return StatusCode::SUCCESS;
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, IAuditor::Start );
85  sc = start();
86  if ( sc.isSuccess() ) m_state = m_targetState;
87  return sc;
88  } catch ( const GaudiException& Exception ) {
89  fatal() << "in sysStart(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
90  error() << Exception << endmsg;
91  // Stat stat( chronoSvc() , Exception.tag() );
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  } 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() : nullptr, IAuditor::Stop );
113  sc = stop();
114  if ( sc.isSuccess() ) m_state = m_targetState;
115  return sc;
116  } catch ( const GaudiException& Exception ) {
117  fatal() << "in sysStop(): exception with tag=" << Exception.tag() << " is caught" << endmsg;
118  error() << Exception << endmsg;
119  // Stat stat( chronoSvc() , Exception.tag() );
120  } catch ( const std::exception& Exception ) {
121  fatal() << "in sysStop(): standard std::exception is caught" << endmsg;
122  error() << Exception.what() << endmsg;
123  // Stat stat( chronoSvc() , "*std::exception*" );
124  } catch ( ... ) {
125  fatal() << "in sysStop(): UNKNOWN Exception is caught" << endmsg;
126  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
127  }
128 
129  return StatusCode::FAILURE;
130 }
131 
132 //--- IService::stop
134  // stub implementation
135  return StatusCode::SUCCESS;
136 }
137 
138 //--- IService::start
140  // stub implementation
141  return StatusCode::SUCCESS;
142 }
143 
144 //--- IService::sysFinalize
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  // m_state = Gaudi::StateMachine::ChangeState(Gaudi::StateMachine::TERMINATE,m_state);
176  return StatusCode::SUCCESS;
177 }
178 
179 //--- IService::sysReinitialize
181 
182  StatusCode sc;
183 
184  // Check that the current status is the correct one.
186  error() << "sysReinitialize(): cannot reinitialize service not initialized" << endmsg;
187  return StatusCode::FAILURE;
188  }
189 
190  try {
191 
192  Gaudi::Guards::AuditorGuard guard( this,
193  // check if we want to audit the initialize
194  ( m_auditorReinitialize ) ? auditorSvc().get() : nullptr,
196  sc = reinitialize();
197  return sc;
198  } catch ( const GaudiException& Exception ) {
199  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
200  error() << Exception << endmsg;
201  // Stat stat( chronoSvc() , Exception.tag() ) ;
202  } catch ( const std::exception& Exception ) {
203  fatal() << " Standard std::exception is caught " << endmsg;
204  error() << Exception.what() << endmsg;
205  // Stat stat( chronoSvc() , "*std::exception*" ) ;
206  } catch ( ... ) {
207  fatal() << "UNKNOWN Exception is caught " << endmsg;
208  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
209  }
210  return StatusCode::FAILURE;
211 }
212 
213 //--- IService::sysRestart
215 
216  StatusCode sc;
217 
218  // Check that the current status is the correct one.
220  error() << "sysRestart(): cannot restart service in state " << FSMState() << " -- must be RUNNING " << endmsg;
221  return StatusCode::FAILURE;
222  }
223 
224  try {
225 
226  Gaudi::Guards::AuditorGuard guard( this,
227  // check if we want to audit the initialize
228  ( m_auditorRestart ) ? auditorSvc().get() : nullptr, IAuditor::ReStart );
229  sc = restart();
230  return sc;
231  } catch ( const GaudiException& Exception ) {
232  fatal() << " Exception with tag=" << Exception.tag() << " is caught " << endmsg;
233  error() << Exception << endmsg;
234  // Stat stat( chronoSvc() , Exception.tag() ) ;
235  } catch ( const std::exception& Exception ) {
236  fatal() << " Standard std::exception is caught " << endmsg;
237  error() << Exception.what() << endmsg;
238  // Stat stat( chronoSvc() , "*std::exception*" ) ;
239  } catch ( ... ) {
240  fatal() << "UNKNOWN Exception is caught " << endmsg;
241  // Stat stat( chronoSvc() , "*UNKNOWN Exception*" ) ;
242  }
243  return StatusCode::FAILURE;
244 }
245 
246 //--- IService::reinitialize
248  /* @TODO
249  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
250  * is causing too many problems
251  *
252  // Default implementation is finalize+initialize
253  StatusCode sc = finalize();
254  if (sc.isFailure()) {
255  error() << "reinitialize(): cannot be finalized" << endmsg;
256  return sc;
257  }
258  sc = initialize();
259  if (sc.isFailure()) {
260  error() << "reinitialize(): cannot be initialized" << endmsg;
261  return sc;
262  }
263  */
264  return StatusCode::SUCCESS;
265 }
266 
267 //--- IService::restart
269  // Default implementation is stop+start
270  StatusCode sc = stop();
271  if ( sc.isFailure() ) {
272  error() << "restart(): cannot be stopped" << endmsg;
273  return sc;
274  }
275  sc = start();
276  if ( sc.isFailure() ) {
277  error() << "restart(): cannot be started" << endmsg;
278  return sc;
279  }
280  return StatusCode::SUCCESS;
281 }
282 
283 //--- IService::getServiceName
284 const std::string& Service::name() const { return m_name; }
285 
286 //--- Retrieve pointer to service locator
288 
289 // Use the job options service to set declared properties
291  const bool CREATEIF( true );
292  auto jos = serviceLocator()->service<IJobOptionsSvc>( "JobOptionsSvc", CREATEIF );
293  if ( !jos ) { throw GaudiException( "Service [JobOptionsSvc] not found", name(), StatusCode::FAILURE ); }
294 
295  // initialize messaging (except for MessageSvc)
296  if ( name() != "MessageSvc" ) {
297  // this initializes the messaging, in case property update handlers need to print
298  // and update the property value bypassing the update handler
299  m_outputLevel.value() = setUpMessaging();
300  }
301 
302  return jos->setMyProperties( name(), this );
303 }
304 
305 //--- Local methods
306 // Standard Constructor
307 Service::Service( std::string name, ISvcLocator* svcloc ) : m_name( std::move( name ) ), m_svcLocator( svcloc ) {
308  if ( m_name != "MessageSvc" ) { // the MessageSvc should not notify itself
309  m_outputLevel.declareUpdateHandler(
311  }
312 
313  // Initialize the default value from ApplicationMgr AuditAlgorithms
314  Gaudi::Property<bool> audit( false );
315  auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" );
316  if ( appMgr && appMgr->hasProperty( "AuditServices" ) ) { audit.assign( appMgr->getProperty( "AuditServices" ) ); }
317  m_auditInit = audit;
318  m_auditorInitialize = audit;
319  m_auditorStart = audit;
320  m_auditorStop = audit;
321  m_auditorFinalize = audit;
322  m_auditorReinitialize = audit;
323  m_auditorRestart = audit;
324 }
325 
327  if ( !m_pAuditorSvc ) {
328  m_pAuditorSvc = serviceLocator()->service( "AuditorSvc" );
329  if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
330  }
331  return m_pAuditorSvc;
332 }
333 
Gaudi::Property< int > m_outputLevel
Definition: Service.h:186
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:165
StatusCode initialize() override
Definition: Service.cpp:70
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:287
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:35
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:163
Gaudi::Property< bool > m_auditorInitialize
Definition: Service.h:188
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:196
StatusCode finalize() override
Definition: Service.cpp:174
Implementation of property with value of concrete type.
Definition: Property.h:370
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:62
void sysInitialize_imp()
Definition: Service.cpp:39
StatusCode m_initSC
Definition: Service.h:172
StatusCode start() override
Definition: Service.cpp:139
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:38
StatusCode sysRestart() override
Re-initialize the Service.
Definition: Service.cpp:214
virtual const std::string & tag() const
name tag for the exception, or exception type
StatusCode sysStart() override
Initialize Service.
Definition: Service.cpp:77
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:178
STL namespace.
Gaudi::Property< bool > m_auditInit
Definition: Service.h:187
Gaudi::Property< bool > m_auditorRestart
Definition: Service.h:193
std::string m_name
Service Name.
Definition: Service.h:176
Gaudi::Property< bool > m_auditorReinitialize
Definition: Service.h:192
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
T call_once(T... args)
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:86
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode sysStop() override
Initialize Service.
Definition: Service.cpp:105
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:29
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Gaudi::Property< bool > m_auditorFinalize
Definition: Service.h:191
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
StatusCode reinitialize() override
Definition: Service.cpp:247
StatusCode stop() override
Definition: Service.cpp:133
bool isSuccess() const
Definition: StatusCode.h:361
std::once_flag m_initFlag
Definition: Service.h:173
STL class.
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:145
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:290
#define ON_DEBUG
Definition: Service.cpp:26
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:164
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...
SmartIF< IAuditorSvc > & auditorSvc() const
The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Definition: Service.cpp:326
appMgr
Definition: IOTest.py:103
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition: Service.cpp:307
constexpr static const auto FAILURE
Definition: StatusCode.h:97
Gaudi::Property< bool > m_auditorStart
Definition: Service.h:189
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:687
bool isFailure() const
Definition: StatusCode.h:141
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:179
void setServiceManager(ISvcManager *ism) override
Definition: Service.cpp:334
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:30
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition: Guards.h:213
StatusCode restart() override
Definition: Service.cpp:268
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:190
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:34