The Gaudi Framework  master (e98cfcff)
Loading...
Searching...
No Matches
MessageSvc.h
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#pragma once
12
13#include <Gaudi/Property.h>
15#include <GaudiKernel/Message.h>
16#include <GaudiKernel/Service.h>
18
19#include <iosfwd>
20#include <map>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <vector>
25
26class ISvcLocator;
27
28//
29// ClassName: MessageSvc
30//
31// Description: The MessageSvc service implements the IMessageSvc interface and provides the
32// basic messaging needed by batch oriented applications.
33//
34// Author: Iain Last
35//
36class MessageSvc : public extends<Service, IMessageSvc, IInactiveMessageCounter> {
37public:
38 typedef std::pair<std::string, std::ostream*> NamedStream;
39 typedef std::multimap<int, NamedStream> StreamMap;
40 typedef std::multimap<StatusCode, Message> MessageMap;
41 typedef std::map<std::string, int, std::less<>> ThresholdMap;
42
43 // Default constructor.
44 MessageSvc( const std::string& name, ISvcLocator* svcloc );
45
46 // Implementation of IService::reinitialize()
47 StatusCode reinitialize() override;
48 // Implementation of IService::initialize()
49 StatusCode initialize() override;
50 // Implementation of IService::finalize()
51 StatusCode finalize() override;
52
53 // Implementation of IMessageSvc::reportMessage()
54 void reportMessage( const Message& message ) override;
55
56 // Implementation of IMessageSvc::reportMessage()
57 void reportMessage( const Message& msg, int outputLevel ) override;
58
59 // Implementation of IMessageSvc::reportMessage()
60 void reportMessage( const StatusCode& code, std::string_view source = "" ) override;
61
62 // Implementation of IMessageSvc::reportMessage()
63 void reportMessage( std::string source, int type, std::string message ) override;
64
65 // Implementation of IMessageSvc::insertMessage()
66 void insertMessage( const StatusCode& code, Message message ) override;
67
68 // Implementation of IMessageSvc::eraseMessage()
69 void eraseMessage() override;
70
71 // Implementation of IMessageSvc::eraseMessage()
72 void eraseMessage( const StatusCode& code ) override;
73
74 // Implementation of IMessageSvc::eraseMessage()
75 void eraseMessage( const StatusCode& code, const Message& message ) override;
76
77 // Implementation of IMessageSvc::insertStream()
78 void insertStream( int message_type, std::string name, std::ostream* stream ) override;
79
80 // Implementation of IMessageSvc::eraseStream()
81 void eraseStream() override;
82
83 // Implementation of IMessageSvc::eraseStream()
84 void eraseStream( int message_type ) override;
85
86 // Implementation of IMessageSvc::eraseStream()
87 void eraseStream( int message_type, std::ostream* stream ) override;
88
89 // Implementation of IMessageSvc::eraseStream()
90 void eraseStream( std::ostream* stream ) override;
91
92 // Implementation of IMessageSvc::desaultStream()
93 std::ostream* defaultStream() const override { return m_defaultStream; }
94
95 // Implementation of IMessageSvc::setDefaultStream()
96 void setDefaultStream( std::ostream* stream ) override {
97 auto lock = std::scoped_lock{ m_reportMutex };
98 m_defaultStream = stream;
99 }
100
101 // Implementation of IMessageSvc::ouputLevel()
102 int outputLevel() const override;
103
104 // Implementation of IMessageSvc::ouputLevel()
105 int outputLevel( std::string_view source ) const override;
106
107 // Implementation of IMessageSvc::setOuputLevel()
108 void setOutputLevel( int new_level ) override;
109
110 // Implementation of IMessageSvc::setOuputLevel()
111 void setOutputLevel( std::string_view source, int new_level ) override;
112
113 // Implementation of IMessageSvc::useColor()
114 bool useColor() const override { return m_color; }
115
116 // Implementation of IMessageSvc::getLogColor()
117 std::string getLogColor( int logLevel ) const override;
118
119 // Implementation of IMessageSvc::messageCount()
120 int messageCount( MSG::Level logLevel ) const override;
121
122 // Implementation of IInactiveMessageCounter::incrInactiveCount()
123 void incrInactiveCount( MSG::Level level, std::string_view src ) override;
124
125 // Override setPropertyRepr to serialize property writes with m_reportMutex,
126 // preventing data races between property updates and concurrent
127 // i_reportMessage calls.
128 StatusCode setPropertyRepr( const std::string& n, const std::string& r ) override;
129
130protected:
132 virtual void i_reportMessage( const Message& msg, int outputLevel );
133
135 virtual void i_reportMessage( const StatusCode& code, std::string_view source );
136
137 bool m_suppress{ false };
139 virtual void i_onSuppressChanged( bool v ) { m_suppress = v; }
140 virtual void i_onDefaultFormatChanged( const std::string& v ) { m_defaultFormat = v; }
141
142private:
144 mutable std::recursive_mutex m_reportMutex;
145
150 Gaudi::Property<bool> m_stats{ this, "showStats", false, "" };
151 Gaudi::Property<unsigned int> m_statLevel{ this, "statLevel", 0, "" };
152
153 std::array<Gaudi::Property<std::vector<std::string>>, MSG::NUM_LEVELS> m_thresholdProp{ { { /*ignored*/ },
154 { this, "setVerbose" },
155 { this, "setDebug" },
156 { this, "setInfo" },
157 { this, "setWarning" },
158 { this, "setError" },
159 { this, "setFatal" },
160 { this, "setAlways" } } };
161
162 Gaudi::Property<bool> m_color{ this, "useColors", false, "" };
163
164 std::array<Gaudi::Property<std::vector<std::string>>, MSG::NUM_LEVELS> m_logColors{ { { /*ignored*/ },
165 { this, "verboseColorCode" },
166 { this, "debugColorCode" },
167 { this, "infoColorCode" },
168 { this, "warningColorCode" },
169 { this, "errorColorCode" },
170 { this, "fatalColorCode" },
171 { this, "alwaysColorCode" } } };
172
173 std::array<Gaudi::Property<int>, MSG::NUM_LEVELS> m_msgLimit{ { { this, "defaultLimit", 500 },
174 { this, "verboseLimit", 500 },
175 { this, "debugLimit", 500 },
176 { this, "infoLimit", 500 },
177 { this, "warningLimit", 500 },
178 { this, "errorLimit", 500 },
179 { this, "fatalLimit", 500 },
180 { this, "alwaysLimit", 0 } } };
181
182 Gaudi::Property<bool> m_suppressProp{ this, "enableSuppression", false,
183 [this]( auto& ) { i_onSuppressChanged( m_suppressProp.value() ); }, "" };
185
187 this,
188 "tracedInactiveSources",
189 {},
190 "for each message source specified, print a stack trace for the unprotected and unseen messages" };
191
193 this, "loggedStreams", {}, "MessageStream sources we want to dump into a logfile" };
194
195 std::ostream* m_defaultStream = &std::cout;
200
202
204 struct MsgAry final {
206 std::array<int, MSG::NUM_LEVELS> msg = { { 0 } };
208 MsgAry() = default;
209 };
210
211 std::map<std::string, MsgAry, std::less<>> m_sourceMap, m_inactiveMap;
212
213 std::array<int, MSG::NUM_LEVELS> m_msgCount;
214
215 std::map<std::string, std::shared_ptr<std::ostream>, std::less<>> m_loggedStreams;
216
221
222 void setupLogStreams();
223
225 mutable std::recursive_mutex m_messageMapMutex;
226
229 mutable std::recursive_mutex m_thresholdMapMutex;
230};
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
The Message class.
Definition Message.h:25
static const std::string getDefaultTimeFormat()
Get the default time format string.
Definition Message.h:77
static const std::string getDefaultFormat()
Get the default format string.
Definition Message.h:68
std::ostream * m_defaultStream
Pointer to the output stream.
Definition MessageSvc.h:195
Gaudi::Property< bool > m_color
Definition MessageSvc.h:162
std::multimap< int, NamedStream > StreamMap
Definition MessageSvc.h:39
void reportMessage(const Message &message) override
Gaudi::Property< std::string > m_defaultTimeFormat
Definition MessageSvc.h:149
std::recursive_mutex m_messageMapMutex
Mutex to synchronize multiple access to m_messageMap.
Definition MessageSvc.h:225
Message m_defaultMessage
Default Message.
Definition MessageSvc.h:196
MessageMap m_messageMap
Message map.
Definition MessageSvc.h:198
Gaudi::Property< bool > m_suppressProp
Definition MessageSvc.h:182
StatusCode reinitialize() override
void setupLogStreams()
StreamMap m_streamMap
Stream map.
Definition MessageSvc.h:197
std::map< std::string, MsgAry, std::less<> > m_sourceMap
Definition MessageSvc.h:211
ThresholdMap m_thresholdMap
Output level threshold map.
Definition MessageSvc.h:199
Gaudi::Property< bool > m_inactCount
Definition MessageSvc.h:184
void setDefaultStream(std::ostream *stream) override
Definition MessageSvc.h:96
std::map< std::string, std::shared_ptr< std::ostream >, std::less<> > m_loggedStreams
Definition MessageSvc.h:215
std::array< Gaudi::Property< int >, MSG::NUM_LEVELS > m_msgLimit
Definition MessageSvc.h:173
virtual void i_onSuppressChanged(bool v)
Definition MessageSvc.h:139
Gaudi::Property< std::string > m_defaultFormatProp
Definition MessageSvc.h:146
void setupInactCount(Gaudi::Details::PropertyBase &prop)
void setupThreshold(Gaudi::Details::PropertyBase &prop)
std::map< std::string, MsgAry, std::less<> > m_inactiveMap
Definition MessageSvc.h:211
std::array< int, MSG::NUM_LEVELS > m_msgCount
Definition MessageSvc.h:213
std::array< Gaudi::Property< std::vector< std::string > >, MSG::NUM_LEVELS > m_logColors
Definition MessageSvc.h:164
int outputLevel() const override
virtual void i_onDefaultFormatChanged(const std::string &v)
Definition MessageSvc.h:140
int messageCount(MSG::Level logLevel) const override
void setOutputLevel(int new_level) override
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
StatusCode initialize() override
bool useColor() const override
Definition MessageSvc.h:114
void eraseStream() override
std::string m_defaultFormat
Definition MessageSvc.h:138
std::array< Gaudi::Property< std::vector< std::string > >, MSG::NUM_LEVELS > m_thresholdProp
Definition MessageSvc.h:153
void eraseMessage() override
void setupLimits(Gaudi::Details::PropertyBase &prop)
StatusCode finalize() override
Gaudi::Property< std::vector< std::string > > m_tracedInactiveSources
Definition MessageSvc.h:186
std::string getLogColor(int logLevel) const override
Gaudi::Property< unsigned int > m_statLevel
Definition MessageSvc.h:151
StatusCode setPropertyRepr(const std::string &n, const std::string &r) override
std::map< std::string, int, std::less<> > ThresholdMap
Definition MessageSvc.h:41
std::recursive_mutex m_thresholdMapMutex
Mutex to synchronize multiple access to m_thresholdMap (.
Definition MessageSvc.h:229
void setupColors(Gaudi::Details::PropertyBase &prop)
std::multimap< StatusCode, Message > MessageMap
Definition MessageSvc.h:40
Gaudi::Property< bool > m_stats
Definition MessageSvc.h:150
std::recursive_mutex m_reportMutex
Mutex to synchronize multiple threads printing.
Definition MessageSvc.h:144
std::ostream * defaultStream() const override
Definition MessageSvc.h:93
Gaudi::Property< std::map< std::string, std::string, std::less<> > > m_loggedStreamsName
Definition MessageSvc.h:192
std::pair< std::string, std::ostream * > NamedStream
Definition MessageSvc.h:38
void insertMessage(const StatusCode &code, Message message) override
bool m_suppress
Definition MessageSvc.h:137
void insertStream(int message_type, std::string name, std::ostream *stream) override
MessageSvc(const std::string &name, ISvcLocator *svcloc)
void incrInactiveCount(MSG::Level level, std::string_view src) override
std::string m_logColorCodes[MSG::NUM_LEVELS]
Definition MessageSvc.h:201
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
@ NUM_LEVELS
Definition IMessageSvc.h:22
MsgAry()=default
Default constructor.
std::array< int, MSG::NUM_LEVELS > msg
Internal array of counters.
Definition MessageSvc.h:206