Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f5098d57)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AuditorSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 <Gaudi/Auditor.h>
14 #include <Gaudi/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 std::unique_ptr<Gaudi::IAuditor> AuditorSvc::newAuditor( MsgStream& log, std::string_view name ) {
26  // locate the auditor factory, instantiate a new auditor, initialize it
28  std::unique_ptr<Gaudi::IAuditor> aud{
29  Gaudi::Auditor::Factory::create( item.type(), item.name(), serviceLocator().get() ) };
30  if ( aud ) {
31  // make sure we increase the internal reference counter used by SmartIF or any usage of it
32  // somewhere else will delete the Auditor in our back !
33  // note that release is never called, as we handle the ownership via unique_ptr and do not
34  // rely on Gaudi's legacy black magic
35  aud->addRef();
36  if ( m_targetState >= Gaudi::StateMachine::INITIALIZED && aud->sysInitialize().isFailure() ) {
37  log << MSG::WARNING << "Failed to initialize Auditor " << name << endmsg;
38  aud.reset();
39  }
40  } else {
41  log << MSG::WARNING << "Unable to retrieve factory for Auditor " << name << endmsg;
42  }
43  return aud;
44 }
45 
46 Gaudi::IAuditor* AuditorSvc::getAuditor( const std::string& name ) const {
47  // find an auditor by name, return 0 on error
48  auto it = std::find_if(
50  [item_name = Gaudi::Utils::TypeNameString( name ).name()]( auto const& i ) { return i->name() == item_name; } );
51  return it != std::end( m_pAudList ) ? it->get() : nullptr;
52 }
53 
54 StatusCode AuditorSvc::addAuditor( std::string const& name ) {
55  // this is clumsy, but the PropertyHolder won't tell us when my property changes right
56  // under my nose, so I'll have to figure this out the hard way
57  if ( !hasAuditor( name ) ) { // if auditor does not yet exist
58  auto aud = newAuditor( msgStream(), name );
59  if ( aud ) {
60  m_pAudList.push_back( std::move( aud ) );
61  } else {
62  error() << "Error constructing Auditor " << name << endmsg;
63  return StatusCode::FAILURE;
64  }
65  }
66  return StatusCode::SUCCESS;
67 }
68 
69 std::optional<StatusCode> AuditorSvc::removesAuditor( std::string const& name ) {
70  auto it = std::find_if(
72  [item_name = Gaudi::Utils::TypeNameString( name ).name()]( auto const& i ) { return i->name() == item_name; } );
73  if ( it != std::end( m_pAudList ) ) {
74  auto sc = ( *it )->sysFinalize();
75  if ( sc.isFailure() ) { error() << "Finalization of auditor " << name << " failed : " << sc << endmsg; }
76  m_pAudList.erase( it );
77  return sc;
78  }
79  return {};
80 }
81 
83  // drop all existing Auditors no more declared (and finalize them in case)
84  auto pastEnd = std::remove_if( begin( m_pAudList ), end( m_pAudList ), [this]( auto& entry ) -> bool {
85  return std::find( begin( m_audNameList ), end( m_audNameList ), entry->name() ) != m_audNameList.end();
86  } );
87  std::for_each( pastEnd, m_pAudList.end(), [this]( auto& entry ) {
88  if ( entry->isEnabled() ) {
89  auto sc = entry->sysFinalize();
90  if ( sc.isFailure() ) { error() << "Finalization of auditor " << entry->name() << " failed : " << sc << endmsg; }
91  }
92  } );
93  m_pAudList.erase( pastEnd, m_pAudList.end() );
94  // create all newly declared Auditors
96  for ( auto& it : m_audNameList ) {
97  if ( addAuditor( it ).isFailure() ) sc = StatusCode::FAILURE;
98  }
99  return sc;
100 }
101 
103  for ( auto& it : m_pAudList ) {
104  if ( it->isEnabled() ) it->sysFinalize().ignore();
105  }
106  m_pAudList.clear();
107  return Service::finalize();
108 }
109 
110 void AuditorSvc::before( std::string const& evt, const std::string& name, EventContext const& context ) {
111  if ( !isEnabled() ) return;
112  for ( auto& it : m_pAudList ) {
113  if ( it->isEnabled() ) it->before( evt, name, context );
114  }
115 }
116 
117 void AuditorSvc::after( std::string const& evt, const std::string& name, EventContext const& context,
118  StatusCode const& sc ) {
119  if ( !isEnabled() ) return;
120  for ( auto& it : m_pAudList ) {
121  if ( it->isEnabled() ) it->after( evt, name, context, sc );
122  }
123 }
IOTest.evt
evt
Definition: IOTest.py:107
AuditorSvc.h
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:49
Gaudi.Configuration.log
log
Definition: Configuration.py:28
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
GaudiException.h
Gaudi::IAuditor
The IAuditor is the interface implemented by the Auditor base class.
Definition: IAuditor.h:26
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
AuditorSvc::removesAuditor
std::optional< StatusCode > removesAuditor(std::string const &name) override
removes an Auditor. Returns whether the Auditor was present (and thus removed)
Definition: AuditorSvc.cpp:69
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
AuditorSvc::hasAuditor
bool hasAuditor(std::string const &name) const override
management functionality: check if an Auditor exists
Definition: AuditorSvc.h:41
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:224
AuditorSvc::addAuditor
StatusCode addAuditor(std::string const &name) override
adds a new Auditor
Definition: AuditorSvc.cpp:54
INamedInterface.h
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
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:334
StatusCode
Definition: StatusCode.h:65
AuditorSvc::getAuditor
Gaudi::IAuditor * getAuditor(std::string const &name) const override
management functionality: retrieve an Auditor
Definition: AuditorSvc.cpp:46
Gaudi::cxx::for_each
void for_each(ContainerOfSynced &c, Fun &&f)
Definition: SynchronizedValue.h:98
AuditorSvc
This service manages Auditors.
Definition: AuditorSvc.h:22
IAuditor.h
AuditorSvc::isEnabled
bool isEnabled() const override
Definition: AuditorSvc.h:33
AuditorSvc::finalize
StatusCode finalize() override
Definition: AuditorSvc.cpp:102
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
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Utils::TypeNameString::type
const std::string & type() const
Definition: TypeNameString.h:48
AuditorSvc::newAuditor
std::unique_ptr< Gaudi::IAuditor > newAuditor(MsgStream &, std::string_view)
Definition: AuditorSvc.cpp:25
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:140
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
AuditorSvc::after
void after(std::string const &, std::string const &, EventContext const &, StatusCode const &) override
Definition: AuditorSvc.cpp:117
EventContext
Definition: EventContext.h:34
AuditorSvc::m_pAudList
std::vector< std::unique_ptr< Gaudi::IAuditor > > m_pAudList
Definition: AuditorSvc.h:70
AuditorSvc::m_audNameList
Gaudi::Property< std::vector< std::string > > m_audNameList
Definition: AuditorSvc.h:54
IOTest.end
end
Definition: IOTest.py:125
AuditorSvc::before
void before(std::string const &, std::string const &, EventContext const &) override
Definition: AuditorSvc.cpp:110
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
AuditorSvc::syncAuditors
StatusCode syncAuditors()
internal mathod to update auditors when m_audNameList is changed
Definition: AuditorSvc.cpp:82
MsgStream.h
Auditor.h