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