Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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>
17 #include <GaudiKernel/StatusCode.h>
18 #include <iosfwd>
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 class 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 //
35 class MessageSvc : public extends<Service, IMessageSvc, IInactiveMessageCounter> {
36 public:
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 };
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 
124 protected:
126  virtual void i_reportMessage( const Message& msg, int outputLevel );
127 
129  virtual void i_reportMessage( const StatusCode& code, std::string_view source );
130 
131 private:
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, "" };
167  Gaudi::Property<bool> m_inactCount{ this, "countInactive", false, &MessageSvc::setupInactCount, "" };
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 };
MessageSvc::MsgAry::MsgAry
MsgAry()=default
Default constructor.
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:34
MessageSvc::reinitialize
StatusCode reinitialize() override
Definition: MessageSvc.cpp:99
MessageSvc::m_logColors
std::array< Gaudi::Property< std::vector< std::string > >, MSG::NUM_LEVELS > m_logColors
Definition: MessageSvc.h:148
Write.stream
stream
Definition: Write.py:32
MessageSvc::setupLimits
void setupLimits(Gaudi::Details::PropertyBase &prop)
Definition: MessageSvc.cpp:141
precedence.message
message
Definition: precedence.py:19
MessageSvc::m_stats
Gaudi::Property< bool > m_stats
Definition: MessageSvc.h:134
MessageSvc::MsgAry::msg
std::array< int, MSG::NUM_LEVELS > msg
Internal array of counters.
Definition: MessageSvc.h:189
MessageSvc::MsgAry
Private helper class to keep the count of messages of a type (MSG::LEVEL).
Definition: MessageSvc.h:187
MessageSvc::m_defaultTimeFormat
Gaudi::Property< std::string > m_defaultTimeFormat
Definition: MessageSvc.h:133
MessageSvc::insertMessage
void insertMessage(const StatusCode &code, Message message) override
Definition: MessageSvc.cpp:406
ISvcLocator
Definition: ISvcLocator.h:42
MessageSvc::incrInactiveCount
void incrInactiveCount(MSG::Level level, std::string_view src) override
Definition: MessageSvc.cpp:456
MessageSvc::reportMessage
void reportMessage(const Message &message) override
Definition: MessageSvc.cpp:357
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
MessageSvc::m_logColorCodes
std::string m_logColorCodes[MSG::NUM_LEVELS]
Definition: MessageSvc.h:184
MessageSvc::m_reportMutex
std::recursive_mutex m_reportMutex
Mutex to synchronize multiple threads printing.
Definition: MessageSvc.h:208
MessageSvc::i_reportMessage
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
Definition: MessageSvc.cpp:310
MessageSvc::messageCount
int messageCount(MSG::Level logLevel) const override
Definition: MessageSvc.cpp:454
StatusCode.h
IMessageSvc.h
MessageSvc::m_streamMap
StreamMap m_streamMap
Stream map.
Definition: MessageSvc.h:180
MessageSvc::m_thresholdMapMutex
std::recursive_mutex m_thresholdMapMutex
Mutex to synchronize multiple access to m_thresholdMap (.
Definition: MessageSvc.h:215
Message::getDefaultFormat
static const std::string getDefaultFormat()
Get the default format string.
Definition: Message.h:68
MessageSvc::m_msgLimit
std::array< Gaudi::Property< int >, MSG::NUM_LEVELS > m_msgLimit
Definition: MessageSvc.h:157
MessageSvc::m_statLevel
Gaudi::Property< unsigned int > m_statLevel
Definition: MessageSvc.h:135
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:333
StatusCode
Definition: StatusCode.h:64
Message
Definition: Message.h:25
MessageSvc::ThresholdMap
std::map< std::string, int, std::less<> > ThresholdMap
Definition: MessageSvc.h:40
MessageSvc::MessageMap
std::multimap< StatusCode, Message > MessageMap
Definition: MessageSvc.h:39
MessageSvc::m_sourceMap
std::map< std::string, MsgAry, std::less<> > m_sourceMap
Definition: MessageSvc.h:194
MessageSvc::m_msgCount
std::array< int, MSG::NUM_LEVELS > m_msgCount
Definition: MessageSvc.h:196
MessageSvc::m_defaultMessage
Message m_defaultMessage
Default Message.
Definition: MessageSvc.h:179
MessageSvc::m_thresholdMap
ThresholdMap m_thresholdMap
Output level threshold map.
Definition: MessageSvc.h:182
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:19
MessageSvc::m_defaultStream
std::ostream * m_defaultStream
Pointer to the output stream.
Definition: MessageSvc.h:178
MessageSvc::defaultStream
std::ostream * defaultStream() const override
Definition: MessageSvc.h:92
MessageSvc::m_messageMap
MessageMap m_messageMap
Message map.
Definition: MessageSvc.h:181
MessageSvc::setupThreshold
void setupThreshold(Gaudi::Details::PropertyBase &prop)
Definition: MessageSvc.cpp:160
gaudirun.level
level
Definition: gaudirun.py:364
MessageSvc::m_tracedInactiveSources
Gaudi::Property< std::vector< std::string > > m_tracedInactiveSources
Definition: MessageSvc.h:169
MessageSvc::m_inactiveMap
std::map< std::string, MsgAry, std::less<> > m_inactiveMap
Definition: MessageSvc.h:194
MessageSvc::setDefaultStream
void setDefaultStream(std::ostream *stream) override
Definition: MessageSvc.h:95
MessageSvc
Definition: MessageSvc.h:35
Service.h
MessageSvc::outputLevel
int outputLevel() const override
Definition: MessageSvc.cpp:428
MessageSvc::finalize
StatusCode finalize() override
Definition: MessageSvc.cpp:198
MessageSvc::m_inactCount
Gaudi::Property< bool > m_inactCount
Definition: MessageSvc.h:167
MSG::Level
Level
Definition: IMessageSvc.h:22
gaudirun.type
type
Definition: gaudirun.py:160
MessageSvc::m_messageMapMutex
std::recursive_mutex m_messageMapMutex
Mutex to synchronize multiple access to m_messageMap.
Definition: MessageSvc.h:211
MessageSvc::NamedStream
std::pair< std::string, std::ostream * > NamedStream
Definition: MessageSvc.h:37
MessageSvc::m_loggedStreamsName
Gaudi::Property< std::map< std::string, std::string, std::less<> > > m_loggedStreamsName
Definition: MessageSvc.h:175
MessageSvc::setupInactCount
void setupInactCount(Gaudi::Details::PropertyBase &prop)
Definition: MessageSvc.cpp:190
MessageSvc::eraseMessage
void eraseMessage() override
Definition: MessageSvc.cpp:411
MessageSvc::m_color
Gaudi::Property< bool > m_color
Definition: MessageSvc.h:146
MessageSvc::m_loggedStreams
std::map< std::string, std::shared_ptr< std::ostream >, std::less<> > m_loggedStreams
Definition: MessageSvc.h:198
MessageSvc::initialize
StatusCode initialize() override
Definition: MessageSvc.cpp:89
Message::getDefaultTimeFormat
static const std::string getDefaultTimeFormat()
Get the default time format string.
Definition: Message.h:77
MessageSvc::getLogColor
std::string getLogColor(int logLevel) const override
Definition: MessageSvc.cpp:450
MessageSvc::eraseStream
void eraseStream() override
Definition: MessageSvc.cpp:389
MessageSvc::m_thresholdProp
std::array< Gaudi::Property< std::vector< std::string > >, MSG::NUM_LEVELS > m_thresholdProp
Definition: MessageSvc.h:137
MessageSvc::setupLogStreams
void setupLogStreams()
Definition: MessageSvc.cpp:470
MessageSvc::MessageSvc
MessageSvc(const std::string &name, ISvcLocator *svcloc)
Definition: MessageSvc.cpp:64
MessageSvc::m_suppress
Gaudi::Property< bool > m_suppress
Definition: MessageSvc.h:166
MSG::NUM_LEVELS
@ NUM_LEVELS
Definition: IMessageSvc.h:22
MessageSvc::StreamMap
std::multimap< int, NamedStream > StreamMap
Definition: MessageSvc.h:38
MessageSvc::insertStream
void insertStream(int message_type, std::string name, std::ostream *stream) override
Definition: MessageSvc.cpp:385
MessageSvc::useColor
bool useColor() const override
Definition: MessageSvc.h:113
MessageSvc::m_defaultFormat
Gaudi::Property< std::string > m_defaultFormat
Definition: MessageSvc.h:132
MessageSvc::setOutputLevel
void setOutputLevel(int new_level) override
Definition: MessageSvc.cpp:436
Gaudi::Property< std::string >
Property.h
Message.h
MessageSvc::setupColors
void setupColors(Gaudi::Details::PropertyBase &prop)
Definition: MessageSvc.cpp:107