The Gaudi Framework master (1fbcc22a)
Loading...
Searching...
No Matches
InertMessageSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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 "MessageSvc.h"
12#include <functional>
13#include <memory>
14#include <tbb/concurrent_queue.h>
15#include <thread>
16
31public:
33
35 StatusCode initialize() override;
36
38 StatusCode finalize() override;
39
41
43 void reportMessage( const Message& msg ) override;
44
46 void reportMessage( const Message& msg, int outputLevel ) override;
47
49 void reportMessage( const StatusCode& code, std::string_view source = "" ) override;
50
51private:
52 void m_activate();
53 void m_deactivate();
55
56 void i_onSuppressChanged( bool v ) override {
57 m_messageActionsQueue.emplace( [this, v] { m_suppress = v; } );
58 }
59 void i_onDefaultFormatChanged( const std::string& v ) override {
60 m_messageActionsQueue.emplace( [this, v] { m_defaultFormat = v; } );
61 }
62 tbb::concurrent_bounded_queue<std::function<void()>> m_messageActionsQueue;
63
65};
66
68
70 StatusCode sc = MessageSvc::initialize(); // must be executed first
71 if ( sc.isFailure() ) return sc; // error printed already by MessageSvc
72
73 info() << "Activating in a separate thread" << endmsg;
75
77}
78
79StatusCode InertMessageSvc::InertMessageSvc::finalize() {
80 m_messageActionsQueue.emplace( [this] { m_suppress = false; } );
81 m_deactivate();
82 m_thread.join();
83 return MessageSvc::finalize(); // must be called after all other actions
84}
85
87 m_isActive = true;
88 std::function<void()> thisMessageAction;
89 while ( m_isActive || !m_messageActionsQueue.empty() ) {
90 m_messageActionsQueue.pop( thisMessageAction );
91 if ( thisMessageAction ) thisMessageAction();
92 }
93}
94
96 if ( m_isActive ) {
97 // This would be the last action
98 m_messageActionsQueue.emplace( [this]() { m_isActive = false; } );
99 }
100}
101
109 // msg has to be copied as the reference may become invalid by the time it is used
110 m_messageActionsQueue.emplace(
111 [this, m = Message( msg ), outputLevel]() { this->i_reportMessage( m, outputLevel ); } );
112}
113
115 // msg has to be copied as the reference may become invalid by the time it's used
116 m_messageActionsQueue.emplace(
117 [this, m = Message( msg )]() { this->i_reportMessage( m, this->outputLevel( m.getSource() ) ); } );
118}
119
120void InertMessageSvc::reportMessage( const StatusCode& code, std::string_view source ) {
121 // msg has to be copied as the source may become invalid by the time it's used
122 m_messageActionsQueue.emplace( [this, code, s = std::string{ source }]() { this->i_reportMessage( code, s ); } );
123}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Thread safe extension to the standard MessageSvc.
void i_onDefaultFormatChanged(const std::string &v) override
void reportMessage(const Message &msg) override
Implementation of IMessageSvc::reportMessage().
StatusCode finalize() override
Finalization of the service.
StatusCode initialize() override
Initialization of the service.
std::atomic< bool > m_isActive
tbb::concurrent_bounded_queue< std::function< void()> > m_messageActionsQueue
void i_onSuppressChanged(bool v) override
MessageSvc(const std::string &name, ISvcLocator *svcloc)
The Message class.
Definition Message.h:25
Message()=default
Default constructor.
void reportMessage(const Message &message) override
int outputLevel() const override
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
StatusCode initialize() override
std::string m_defaultFormat
Definition MessageSvc.h:138
StatusCode finalize() override
bool m_suppress
Definition MessageSvc.h:137
MessageSvc(const std::string &name, ISvcLocator *svcloc)
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:118
constexpr static const auto SUCCESS
Definition StatusCode.h:99