The Gaudi Framework  v33r0 (d5ea422b)
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, const std::string& 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
53  const std::string item_name = Gaudi::Utils::TypeNameString( name ).name();
55  [&]( const IAuditor* i ) { return i->name() == item_name; } );
56  return SmartIF<IAuditor>{it != std::end( m_pAudList ) ? *it : nullptr};
57 }
58 
60  if ( m_audNameList.size() == m_pAudList.size() ) return StatusCode::SUCCESS;
61 
62  StatusCode sc;
63 
64  // if ( sc.isFailure() ) {
65  // error() << "Unable to locate ObjectManager Service" << endmsg;
66  // return sc;
67  // }
68 
69  // create all declared Auditors that do not yet exist
70  for ( auto& it : m_audNameList ) {
71 
72  // this is clumsy, but the PropertyHolder won't tell us when my property changes right
73  // under my nose, so I'll have to figure this out the hard way
74  if ( !findAuditor_( it ) ) { // if auditor does not yet exist
75  auto aud = newAuditor_( msgStream(), it );
76  if ( aud ) {
77  m_pAudList.push_back( std::move( aud ) );
78  } else {
79  error() << "Error constructing Auditor " << it << endmsg;
81  }
82  }
83  }
84  return sc;
85 }
86 
87 // Inherited Service overrides:
88 //
89 // Initialize the service.
92  if ( sc.isFailure() ) return sc;
93 
94  // create auditor objects for all named auditors
95  sc = syncAuditors_();
96 
97  return sc;
98 }
99 
100 // Finalise the service.
102 
103  for ( auto& it : m_pAudList ) {
104  if ( it->isEnabled() ) it->sysFinalize().ignore();
105  }
106  m_pAudList.clear();
107 
108  // Finalize this specific service
109  return Service::finalize();
110 }
111 
112 // --------- "Before" methods ---------
113 void AuditorSvc::before( StandardEventType evt, INamedInterface* obj ) {
114  if ( !isEnabled() ) return;
115  for ( auto& it : m_pAudList ) {
116  if ( it->isEnabled() ) it->before( evt, obj );
117  }
118 }
119 
120 void AuditorSvc::before( StandardEventType evt, const std::string& name ) {
121  if ( !isEnabled() ) return;
122  for ( auto& it : m_pAudList ) {
123  if ( it->isEnabled() ) it->before( evt, name );
124  }
125 }
126 
127 void AuditorSvc::before( CustomEventTypeRef evt, INamedInterface* obj ) {
128  if ( !isEnabled() ) return;
129  for ( auto& it : m_pAudList ) {
130  if ( it->isEnabled() ) it->before( evt, obj );
131  }
132 }
133 
134 void AuditorSvc::before( CustomEventTypeRef evt, const std::string& name ) {
135  if ( !isEnabled() ) return;
136  for ( auto& it : m_pAudList ) {
137  if ( it->isEnabled() ) it->before( evt, name );
138  }
139 }
140 
141 // --------- "After" methods ---------
142 void AuditorSvc::after( StandardEventType evt, INamedInterface* obj, const StatusCode& sc ) {
143  if ( !isEnabled() ) return;
144  for ( auto& it : m_pAudList ) {
145  if ( it->isEnabled() ) it->after( evt, obj, sc );
146  }
147 }
148 
149 void AuditorSvc::after( StandardEventType evt, const std::string& name, const StatusCode& sc ) {
150  if ( !isEnabled() ) return;
151  for ( auto& it : m_pAudList ) {
152  if ( it->isEnabled() ) it->after( evt, name, sc );
153  }
154 }
155 
156 void AuditorSvc::after( CustomEventTypeRef evt, INamedInterface* obj, const StatusCode& sc ) {
157  if ( !isEnabled() ) return;
158  for ( auto& it : m_pAudList ) {
159  if ( it->isEnabled() ) it->after( evt, obj, sc );
160  }
161 }
162 
163 void AuditorSvc::after( CustomEventTypeRef evt, const std::string& name, const StatusCode& sc ) {
164  if ( !isEnabled() ) return;
165  for ( auto& it : m_pAudList ) {
166  if ( it->isEnabled() ) it->after( evt, name, sc );
167  }
168 }
169 
170 // --------- obsolete methods ---------
171 #define OBSOLETION( name ) \
172  void AuditorSvc::name( INamedInterface* ) { \
173  throw GaudiException( "The method IAuditor::" #name " is obsolete do not call it.", "AuditorSvc::" #name, \
174  StatusCode::FAILURE ); \
175  }
176 
177 OBSOLETION( beforeInitialize )
178 OBSOLETION( afterInitialize )
179 
180 OBSOLETION( beforeReinitialize )
181 OBSOLETION( afterReinitialize )
182 
183 OBSOLETION( beforeExecute )
184 void AuditorSvc::afterExecute( INamedInterface*, const StatusCode& ) {
185  throw GaudiException( "The method afterExecute is obsolete do not call it.", "AuditorSvc::afterExecute",
187 }
188 
189 OBSOLETION( beforeFinalize )
190 OBSOLETION( afterFinalize )
191 
192 bool AuditorSvc::isEnabled() const { return m_isEnabled; }
193 
196 
198  // by interactively setting properties, auditors might be out of sync
199  if ( !syncAuditors_().isSuccess() ) {
200  // as we didn't manage to sync auditors, the safest bet is to assume the
201  // worse...
202  // So don't let clients play with an AuditorSvc in an inconsistent state
203  return nullptr;
204  }
205 
206  // search available auditors, returns 0 on error
207  return findAuditor_( name );
208 }
StatusCode initialize() override
Definition: AuditorSvc.cpp:90
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:142
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
STL namespace.
StatusCode sysFinalize() override
Definition: AuditorSvc.cpp:195
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:113
bool isEnabled() const override
Definition: AuditorSvc.cpp:192
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
#define DECLARE_COMPONENT(type)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
T push_back(T... args)
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:194
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_(const std::string &)
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:164
StatusCode finalize() override
Definition: AuditorSvc.cpp:101
#define OBSOLETION(name)
Definition: AuditorSvc.cpp:171
T begin(T... args)
IAuditor * getAuditor(const std::string &name) override
Definition: AuditorSvc.cpp:197
constexpr static const auto FAILURE
Definition: StatusCode.h:97
SmartIF< IAuditor > newAuditor_(MsgStream &, const std::string &)
Definition: AuditorSvc.cpp:32
bool isFailure() const
Definition: StatusCode.h:141
StatusCode syncAuditors_()
Definition: AuditorSvc.cpp:59
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