The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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>
20
21// Instantiation of a static factory class used by clients to create
22// instances of this service
24
25std::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
46Gaudi::IAuditor* AuditorSvc::getAuditor( const std::string& name ) const {
47 // find an auditor by name, return 0 on error
48 auto it = std::find_if(
49 std::begin( m_pAudList ), std::end( m_pAudList ),
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
54StatusCode 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;
64 }
65 }
67}
68
69std::optional<StatusCode> AuditorSvc::removesAuditor( std::string const& name ) {
70 auto it = std::find_if(
71 std::begin( m_pAudList ), std::end( m_pAudList ),
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
110void 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
117void 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}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
This service manages Auditors.
Definition AuditorSvc.h:22
void before(std::string const &, std::string const &, EventContext const &) override
std::vector< std::unique_ptr< Gaudi::IAuditor > > m_pAudList
Definition AuditorSvc.h:70
void after(std::string const &, std::string const &, EventContext const &, StatusCode const &) override
Gaudi::IAuditor * getAuditor(std::string const &name) const override
management functionality: retrieve an Auditor
std::unique_ptr< Gaudi::IAuditor > newAuditor(MsgStream &, std::string_view)
StatusCode addAuditor(std::string const &name) override
adds a new Auditor
StatusCode finalize() override
Gaudi::Property< std::vector< std::string > > m_audNameList
Definition AuditorSvc.h:54
bool isEnabled() const override
Definition AuditorSvc.h:33
bool hasAuditor(std::string const &name) const override
management functionality: check if an Auditor exists
Definition AuditorSvc.h:41
std::optional< StatusCode > removesAuditor(std::string const &name) override
removes an Auditor. Returns whether the Auditor was present (and thus removed)
StatusCode syncAuditors()
internal mathod to update auditors when m_audNameList is changed
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
This class represents an entry point to all the event specific data.
The IAuditor is the interface implemented by the Auditor base class.
Definition IAuditor.h:26
Helper class to parse a string of format "type/name".
const std::string & type() const
const std::string & name() const
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
StatusCode finalize() override
Definition Service.cpp:223
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
Gaudi::StateMachine::State m_targetState
Service state.
Definition Service.h:157
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
@ WARNING
Definition IMessageSvc.h:22
STL namespace.