The Gaudi Framework  master (37c0b60a)
AuditorSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 }
IOTest.evt
evt
Definition: IOTest.py:107
AuditorSvc::sysFinalize
StatusCode sysFinalize() override
Definition: AuditorSvc.cpp:196
AuditorSvc::after
void after(StandardEventType, INamedInterface *, const StatusCode &) override
Definition: AuditorSvc.cpp:143
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
Service::sysInitialize
StatusCode sysInitialize() override
Initialize Service
Definition: Service.cpp:34
AuditorSvc.h
std::string
STL class.
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:49
Gaudi.Configuration.log
log
Definition: Configuration.py:28
AuditorSvc::syncAuditors_
StatusCode syncAuditors_()
Definition: AuditorSvc.cpp:60
std::move
T move(T... args)
AuditorSvc::m_pAudList
std::vector< SmartIF< IAuditor > > m_pAudList
Definition: AuditorSvc.h:95
GaudiException.h
AuditorSvc::before
void before(StandardEventType, INamedInterface *) override
The following methods are meant to be implemented by the child class...
Definition: AuditorSvc.cpp:114
std::find_if
T find_if(T... args)
std::vector::size
T size(T... args)
AuditorSvc::initialize
StatusCode initialize() override
Definition: AuditorSvc.cpp:91
GaudiException
Definition: GaudiException.h:31
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
std::vector::clear
T clear(T... args)
std::vector::push_back
T push_back(T... args)
INamedInterface.h
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
AuditorSvc
Definition: AuditorSvc.h:28
IAuditor.h
AuditorSvc::isEnabled
bool isEnabled() const override
Definition: AuditorSvc.cpp:193
AuditorSvc::finalize
StatusCode finalize() override
Definition: AuditorSvc.cpp:102
SmartIF
Definition: IConverter.h:25
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
MsgStream
Definition: MsgStream.h:33
TypeNameString.h
Gaudi::Utils::TypeNameString::type
const std::string & type() const
Definition: TypeNameString.h:48
AuditorSvc::newAuditor_
SmartIF< IAuditor > newAuditor_(MsgStream &, std::string_view)
Definition: AuditorSvc.cpp:32
OBSOLETION
#define OBSOLETION(name)
Definition: AuditorSvc.cpp:172
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
AuditorSvc::sysInitialize
StatusCode sysInitialize() override
Definition: AuditorSvc.cpp:195
INamedInterface
Definition: INamedInterface.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Service::sysFinalize
StatusCode sysFinalize() override
Finalize Service
Definition: Service.cpp:193
std::begin
T begin(T... args)
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
AuditorSvc::m_audNameList
Gaudi::Property< std::vector< std::string > > m_audNameList
Definition: AuditorSvc.h:90
AuditorSvc::findAuditor_
SmartIF< IAuditor > findAuditor_(std::string_view)
Definition: AuditorSvc.cpp:51
std::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
IAuditor
Definition: IAuditor.h:28
MsgStream.h
AuditorSvc::getAuditor
IAuditor * getAuditor(const std::string &name) override
Definition: AuditorSvc.cpp:198
Auditor.h