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