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