AuditorSvc.cpp
Go to the documentation of this file.
1 // Include Files
2 #include "GaudiKernel/MsgStream.h"
3 #include "GaudiKernel/ISvcLocator.h"
4 #include "GaudiKernel/IAuditor.h"
5 #include "GaudiKernel/INamedInterface.h"
6 #include "GaudiKernel/Auditor.h"
7 #include "GaudiKernel/TypeNameString.h"
8 #include "GaudiKernel/GaudiException.h"
9 #include "AuditorSvc.h"
10 
11 // Instantiation of a static factory class used by clients to create
12 // instances of this service
14 
15 //
16 // ClassName: AuditorSvc
17 //
18 // Description: This service manages Auditors.
19 //------------------------------------------------------------------
20 
21 //- private helpers ---
22 SmartIF<IAuditor> AuditorSvc::newAuditor_( MsgStream& log, const std::string& name ) {
23  // locate the auditor factory, instantiate a new auditor, initialize it
24  StatusCode sc;
26  SmartIF<IAuditor> aud{ Auditor::Factory::create( item.type(), item.name(), serviceLocator().get() ) };
27  if ( aud ) {
28  if ( m_targetState >= Gaudi::StateMachine::INITIALIZED ) {
29  sc = aud->sysInitialize();
30  if ( sc.isFailure() ) {
31  log << MSG::WARNING << "Failed to initialize Auditor " << name << endmsg;
32  aud.reset();
33  }
34  }
35  } else {
36  log << MSG::WARNING << "Unable to retrieve factory for Auditor " << name << endmsg;
37  }
38  return aud;
39 }
40 
41 SmartIF<IAuditor> AuditorSvc::findAuditor_( const std::string& name ) {
42  // find an auditor by name, return 0 on error
43  const std::string item_name = Gaudi::Utils::TypeNameString(name).name();
44  auto it = std::find_if( std::begin(m_pAudList), std::end(m_pAudList),
45  [&](const IAuditor* i) { return i->name() == item_name; });
46  return SmartIF<IAuditor>{ it != std::end(m_pAudList) ? *it : nullptr };
47 }
48 
50  if ( m_audNameList.size() == m_pAudList.size() )
51  return StatusCode::SUCCESS;
52 
53  MsgStream log( msgSvc(), name() );
54  StatusCode sc;
55 
56 // if ( sc.isFailure() ) {
57 // log << MSG::ERROR << "Unable to locate ObjectManager Service" << endmsg;
58 // return sc;
59 // }
60 
61  // create all declared Auditors that do not yet exist
62  for ( auto& it : m_audNameList ) {
63 
64  // this is clumsy, but the PropertyMgr won't tell us when my property changes right
65  // under my nose, so I'll have to figure this out the hard way
66  if ( !findAuditor_( it ) ) { // if auditor does not yet exist
67  auto aud = newAuditor_( log, it );
68  if ( aud ) {
69  m_pAudList.push_back( std::move(aud) );
70  } else {
71  log << MSG::ERROR << "Error constructing Auditor " << it << endmsg;
73  }
74  }
75  }
76  return sc;
77 }
78 
79 // Standard Constructor.
80 // Input: name String with service name
81 // Input: svc Pointer to service locator interface
82 AuditorSvc::AuditorSvc( const std::string& name, ISvcLocator* svc )
83 : base_class(name, svc) {
84  declareProperty("Auditors", m_audNameList );
85  declareProperty("Enable", m_isEnabled = true);
86 }
87 
88 // Inherited Service overrides:
89 //
90  // Initialize the service.
93  if ( sc.isFailure() )
94  return sc;
95 
96  // create auditor objects for all named auditors
97  sc = syncAuditors_();
98 
99  return sc;
100 }
101 
102  // Finalise the service.
104 
105  for (auto& it : m_pAudList ) {
106  if(it->isEnabled()) it->sysFinalize().ignore();
107  }
108  m_pAudList.clear();
109 
110  // Finalize this specific service
111  return Service::finalize();
112 }
113 
114 // --------- "Before" methods ---------
115 void AuditorSvc::before(StandardEventType evt, INamedInterface* obj) {
116  if (!isEnabled()) return;
117  for (auto& it : m_pAudList ) {
118  if(it->isEnabled()) it->before(evt,obj);
119  }
120 }
121 
122 void AuditorSvc::before(StandardEventType evt, const std::string &name) {
123  if (!isEnabled()) return;
124  for (auto& it : m_pAudList ) {
125  if(it->isEnabled()) it->before(evt,name);
126  }
127 }
128 
129 void AuditorSvc::before(CustomEventTypeRef evt, INamedInterface* obj) {
130  if (!isEnabled()) return;
131  for (auto& it : m_pAudList) {
132  if(it->isEnabled()) it->before(evt,obj);
133  }
134 }
135 
136 void AuditorSvc::before(CustomEventTypeRef evt, const std::string &name) {
137  if (!isEnabled()) return;
138  for (auto& it : m_pAudList ) {
139  if(it->isEnabled()) it->before(evt,name);
140  }
141 }
142 
143 // --------- "After" methods ---------
144 void AuditorSvc::after(StandardEventType evt, INamedInterface* obj, const StatusCode& sc) {
145  if (!isEnabled()) return;
146  for (auto& it : m_pAudList ) {
147  if(it->isEnabled()) it->after(evt,obj,sc);
148  }
149 }
150 
151 void AuditorSvc::after(StandardEventType evt, const std::string &name, const StatusCode& sc) {
152  if (!isEnabled()) return;
153  for (auto& it : m_pAudList ) {
154  if(it->isEnabled()) it->after(evt,name,sc);
155  }
156 }
157 
158 void AuditorSvc::after(CustomEventTypeRef evt, INamedInterface* obj, const StatusCode& sc) {
159  if (!isEnabled()) return;
160  for (auto& it : m_pAudList ) {
161  if(it->isEnabled()) it->after(evt,obj,sc);
162  }
163 }
164 
165 void AuditorSvc::after(CustomEventTypeRef evt, const std::string &name, const StatusCode& sc) {
166  if (!isEnabled()) return;
167  for (auto& it : m_pAudList) {
168  if(it->isEnabled()) it->after(evt,name,sc);
169  }
170 }
171 
172 // --------- obsolete methods ---------
173 #define OBSOLETION(name) \
174  void AuditorSvc::name(INamedInterface*) { \
175  throw GaudiException("The method IAuditor::" #name " is obsolete do not call it.", \
176  "AuditorSvc::" #name , StatusCode::FAILURE); \
177  }
178 
179 OBSOLETION(beforeInitialize)
180 OBSOLETION(afterInitialize)
181 
182 OBSOLETION(beforeReinitialize)
183 OBSOLETION(afterReinitialize)
184 
185 OBSOLETION(beforeExecute)
186 void AuditorSvc::afterExecute(INamedInterface*,const StatusCode&) {
187  throw GaudiException("The method afterExecute is obsolete do not call it.",
188  "AuditorSvc::afterExecute" , StatusCode::FAILURE);
189 }
190 
191 OBSOLETION(beforeBeginRun)
192 OBSOLETION(afterBeginRun)
193 
194 OBSOLETION(beforeEndRun)
195 OBSOLETION(afterEndRun)
196 
197 OBSOLETION(beforeFinalize)
198 OBSOLETION(afterFinalize)
199 
200 
201 bool AuditorSvc::isEnabled( ) const {
202  return m_isEnabled;
203 }
204 
206  return Service::sysInitialize();
207 }
209  return Service::sysFinalize();
210 }
211 
212 
213 IAuditor* AuditorSvc::getAuditor( const std::string& name ) {
214  // by interactively setting properties, auditors might be out of sync
215  if ( !syncAuditors_().isSuccess() ) {
216  // as we didn't manage to sync auditors, the safest bet is to assume the
217  // worse...
218  // So don't let clients play with an AuditorSvc in an inconsistent state
219  return nullptr;
220  }
221 
222  // search available auditors, returns 0 on error
223  return findAuditor_( name );
224 }
StatusCode initialize() override
Definition: AuditorSvc.cpp:91
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
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
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Definition: Service.cpp:188
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
void after(StandardEventType, INamedInterface *, const StatusCode &) override
Definition: AuditorSvc.cpp:144
STL namespace.
StatusCode sysFinalize() override
Definition: AuditorSvc.cpp:208
AuditorSvc(const std::string &name, ISvcLocator *svc)
Definition: AuditorSvc.cpp:82
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
std::vector< SmartIF< IAuditor > > m_pAudList
Definition: AuditorSvc.h:103
void before(StandardEventType, INamedInterface *) override
The following methods are meant to be implemented by the child class...
Definition: AuditorSvc.cpp:115
bool isEnabled() const override
Definition: AuditorSvc.cpp:201
virtual const std::string & name() const =0
Retrieve the name of the instance.
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
StatusCode sysInitialize() override
Definition: AuditorSvc.cpp:205
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
StatusCode sysFinalize() override
Finalize Service.
Definition: Service.cpp:153
SmartIF< IAuditor > findAuditor_(const std::string &)
Definition: AuditorSvc.cpp:41
IInterface compliant class extending IInterface with the name() method.
StatusCode finalize() override
Definition: AuditorSvc.cpp:103
#define OBSOLETION(name)
Definition: AuditorSvc.cpp:173
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
const std::string & type() const
IAuditor * getAuditor(const std::string &name) override
Definition: AuditorSvc.cpp:213
tuple item
print s1,s2
Definition: ana.py:146
SmartIF< IAuditor > newAuditor_(MsgStream &, const std::string &)
Definition: AuditorSvc.cpp:22
std::vector< std::string > m_audNameList
Definition: AuditorSvc.h:100
bool m_isEnabled
Definition: AuditorSvc.h:106
const std::string & name() const
void ignore() const
Definition: StatusCode.h:108
StatusCode syncAuditors_()
Definition: AuditorSvc.cpp:49
list i
Definition: ana.py:128
The IAuditor is the interface implmented by the AlgAuditor base class.
Definition: IAuditor.h:18
StatusCode sysInitialize() override
Initialize Service.
Definition: Service.cpp:26