The Gaudi Framework
master (1fbcc22a)
Toggle main menu visibility
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
30
class
InertMessageSvc
:
public
MessageSvc
{
31
public
:
32
using
MessageSvc::MessageSvc
;
33
35
StatusCode
initialize
()
override
;
36
38
StatusCode
finalize
()
override
;
39
40
using
MessageSvc::reportMessage
;
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
51
private
:
52
void
m_activate
();
53
void
m_deactivate
();
54
std::atomic<bool>
m_isActive
=
false
;
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
64
std::thread
m_thread
;
65
};
66
67
DECLARE_COMPONENT
(
InertMessageSvc
)
68
69
StatusCode
InertMessageSvc
::
initialize
() {
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
;
74
m_thread
=
std::thread
( &
InertMessageSvc::m_activate
,
this
);
75
76
return
StatusCode::SUCCESS
;
77
}
78
79
StatusCode
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
86
void
InertMessageSvc::m_activate
() {
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
95
void
InertMessageSvc::m_deactivate
() {
96
if
(
m_isActive
) {
97
// This would be the last action
98
m_messageActionsQueue
.emplace( [
this
]() {
m_isActive
=
false
; } );
99
}
100
}
101
108
void
InertMessageSvc::reportMessage
(
const
Message
&
msg
,
int
outputLevel
) {
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
114
void
InertMessageSvc::reportMessage
(
const
Message
&
msg
) {
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
120
void
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
}
MessageSvc.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition
MsgStream.h:198
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition
PluginServiceV1.h:45
functional
memory
thread
std::atomic
std::thread
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition
CommonMessaging.h:124
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition
CommonMessaging.h:115
InertMessageSvc
Thread safe extension to the standard MessageSvc.
Definition
InertMessageSvc.cpp:30
InertMessageSvc::i_onDefaultFormatChanged
void i_onDefaultFormatChanged(const std::string &v) override
Definition
InertMessageSvc.cpp:59
InertMessageSvc::reportMessage
void reportMessage(const Message &msg) override
Implementation of IMessageSvc::reportMessage().
Definition
InertMessageSvc.cpp:114
InertMessageSvc::finalize
StatusCode finalize() override
Finalization of the service.
InertMessageSvc::initialize
StatusCode initialize() override
Initialization of the service.
Definition
InertMessageSvc.cpp:69
InertMessageSvc::m_isActive
std::atomic< bool > m_isActive
Definition
InertMessageSvc.cpp:54
InertMessageSvc::m_thread
std::thread m_thread
Definition
InertMessageSvc.cpp:64
InertMessageSvc::m_messageActionsQueue
tbb::concurrent_bounded_queue< std::function< void()> > m_messageActionsQueue
Definition
InertMessageSvc.cpp:62
InertMessageSvc::m_activate
void m_activate()
Definition
InertMessageSvc.cpp:86
InertMessageSvc::m_deactivate
void m_deactivate()
Definition
InertMessageSvc.cpp:95
InertMessageSvc::i_onSuppressChanged
void i_onSuppressChanged(bool v) override
Definition
InertMessageSvc.cpp:56
InertMessageSvc::MessageSvc
MessageSvc(const std::string &name, ISvcLocator *svcloc)
Definition
MessageSvc.cpp:65
Message
The Message class.
Definition
Message.h:25
Message::Message
Message()=default
Default constructor.
MessageSvc::reportMessage
void reportMessage(const Message &message) override
Definition
MessageSvc.cpp:358
MessageSvc::outputLevel
int outputLevel() const override
Definition
MessageSvc.cpp:434
MessageSvc::i_reportMessage
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
Definition
MessageSvc.cpp:311
MessageSvc::initialize
StatusCode initialize() override
Definition
MessageSvc.cpp:90
MessageSvc::m_defaultFormat
std::string m_defaultFormat
Definition
MessageSvc.h:138
MessageSvc::finalize
StatusCode finalize() override
Definition
MessageSvc.cpp:199
MessageSvc::m_suppress
bool m_suppress
Definition
MessageSvc.h:137
MessageSvc::MessageSvc
MessageSvc(const std::string &name, ISvcLocator *svcloc)
Definition
MessageSvc.cpp:65
StatusCode
This class is used for returning status codes from appropriate routines.
Definition
StatusCode.h:64
StatusCode::isFailure
bool isFailure() const
Definition
StatusCode.h:118
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition
StatusCode.h:99
GaudiCoreSvc
src
MessageSvc
InertMessageSvc.cpp
Generated on
for The Gaudi Framework by
1.18.0