MessageSvc.h
Go to the documentation of this file.
1 #ifndef GAUDI_MESSAGESVC_H
2 #define GAUDI_MESSAGESVC_H
3 
4 // Include files
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <mutex>
9 #include <set>
10 #include <iosfwd>
11 #include <memory>
12 
13 #include "GaudiKernel/StatusCode.h"
14 #include "GaudiKernel/Service.h"
15 #include "GaudiKernel/IMessageSvc.h"
16 #include "GaudiKernel/Message.h"
17 #include "GaudiKernel/Property.h"
18 
19 // Forward declarations
20 class ISvcLocator;
21 
22 //
23 // ClassName: MessageSvc
24 //
25 // Description: The MessageSvc service implements the IMessageSvc interface and provides the
26 // basic messaging needed by batch oriented applications.
27 //
28 // Author: Iain Last
29 //
30 class MessageSvc : public extends2<Service, IMessageSvc, IInactiveMessageCounter> {
31 public:
32  typedef std::pair< std::string, std::ostream* > NamedStream;
33  typedef std::multimap< int, NamedStream > StreamMap;
34  typedef std::multimap< StatusCode, Message > MessageMap;
35  typedef std::map< std::string, int > ThresholdMap;
36 
37  // Default constructor.
38  MessageSvc( const std::string& name, ISvcLocator* svcloc );
39  // Destructor.
40  ~MessageSvc() override = default;
41 
42  // Implementation of IService::reinitialize()
43  StatusCode reinitialize() override;
44  // Implementation of IService::initialize()
45  StatusCode initialize() override;
46  // Implementation of IService::finalize()
47  StatusCode finalize() override;
48 
49  // Implementation of IMessageSvc::reportMessage()
50  void reportMessage( const Message& message ) override;
51 
52  // Implementation of IMessageSvc::reportMessage()
53  void reportMessage( const Message& msg, int outputLevel ) override;
54 
55  // Implementation of IMessageSvc::reportMessage()
56  void reportMessage( const StatusCode& code, const std::string& source = "") override;
57 
58  // Implementation of IMessageSvc::reportMessage()
59  void reportMessage( const char* source, int type, const char* message) override;
60 
61  // Implementation of IMessageSvc::reportMessage()
62  void reportMessage( const std::string& source, int type, const std::string& message) override;
63 
64  // Implementation of IMessageSvc::insertMessage()
65  void insertMessage( const StatusCode& code, const 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, const 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 {
93  return m_defaultStream;
94  }
95 
96  // Implementation of IMessageSvc::setDefaultStream()
97  void setDefaultStream( std::ostream* stream ) override{
98  std::unique_lock<std::recursive_mutex> lock(m_reportMutex);
99  m_defaultStream = stream;
100  }
101 
102  // Implementation of IMessageSvc::ouputLevel()
103  int outputLevel() const override;
104 
105  // Implementation of IMessageSvc::ouputLevel()
106  int outputLevel(const std::string& source) const override;
107 
108  // Implementation of IMessageSvc::setOuputLevel()
109  void setOutputLevel(int new_level) override;
110 
111  // Implementation of IMessageSvc::setOuputLevel()
112  void setOutputLevel(const std::string& source, int new_level) override;
113 
114  // Implementation of IMessageSvc::useColor()
115  bool useColor() const override { return m_color; }
116 
117  // Implementation of IMessageSvc::getLogColor()
118  std::string getLogColor(int logLevel) const override;
119 
120  // Implementation of IMessageSvc::messageCount()
121  int messageCount( MSG::Level logLevel ) const override;
122 
123  // Implementation of IInactiveMessageCounter::incrInactiveCount()
125  const std::string& src ) override;
126 
127 protected:
129  virtual void i_reportMessage( const Message& msg, int outputLevel );
130 
132  virtual void i_reportMessage( const StatusCode& code, const std::string& source);
133 
134 
135 private:
136  std::ostream* m_defaultStream;
138  StreamMap m_streamMap;
139  MessageMap m_messageMap;
140  ThresholdMap m_thresholdMap;
141  std::string m_defaultFormat;
142  std::string m_defaultTimeFormat;
149 
151 
153  struct MsgAry final {
155  std::array<int,MSG::NUM_LEVELS> msg = {{0}}; // empty value-initialization will trigger zero-initialize of all elements (i.e. 0)
157  MsgAry() = default;
158  };
159 
160  std::map<std::string,MsgAry> m_sourceMap, m_inactiveMap;
162 
163  std::string colTrans(std::string, int);
164  typedef std::map<std::string, MSG::Color> ColorMap;
165  ColorMap m_colMap;
166 
167  std::array<int,MSG::NUM_LEVELS> m_msgCount;
168 
169  std::map<std::string, std::string> m_loggedStreamsName;
170  std::map<std::string, std::shared_ptr<std::ostream>> m_loggedStreams;
171 
172  void initColors(Property& prop);
173  void setupColors(Property& prop);
174  void setupLimits(Property& prop);
175  void setupThreshold(Property& prop);
176  void setupInactCount(Property& prop);
177 
178  void setupLogStreams();
179 
180  void tee( const std::string& sourceName, const std::string& logFileName,
181  const std::set<std::string>& declaredOutFileNames );
182 
184  mutable std::recursive_mutex m_reportMutex;
185 
187  mutable std::recursive_mutex m_messageMapMutex;
188 
191  mutable std::recursive_mutex m_thresholdMapMutex;
192 };
193 
194 #endif
IntegerProperty m_msgLimit[MSG::NUM_LEVELS]
Definition: MessageSvc.h:148
std::string colTrans(std::string, int)
Definition: MessageSvc.cpp:430
std::ostream * m_defaultStream
Pointer to the output stream.
Definition: MessageSvc.h:136
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
std::multimap< int, NamedStream > StreamMap
Definition: MessageSvc.h:33
StreamMap m_streamMap
Stream map.
Definition: MessageSvc.h:138
std::array< int, MSG::NUM_LEVELS > m_msgCount
Definition: MessageSvc.h:167
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
Definition: MessageSvc.cpp:447
ColorMap m_colMap
Definition: MessageSvc.h:165
Message m_defaultMessage
Default Message.
Definition: MessageSvc.h:137
MessageMap m_messageMap
Message map.
Definition: MessageSvc.h:139
void setupLimits(Property &prop)
Definition: MessageSvc.cpp:241
void setupInactCount(Property &prop)
Definition: MessageSvc.cpp:305
void setupLogStreams()
Definition: MessageSvc.cpp:738
MessageSvc(const std::string &name, ISvcLocator *svcloc)
Definition: MessageSvc.cpp:57
std::recursive_mutex m_messageMapMutex
Mutex to synchronize multiple access to m_messageMap.
Definition: MessageSvc.h:187
StatusCode reinitialize() override
Reinitialize Service.
Definition: MessageSvc.cpp:154
std::pair< std::string, std::ostream * > NamedStream
Definition: MessageSvc.h:32
void eraseMessage() override
Definition: MessageSvc.cpp:654
std::map< std::string, int > ThresholdMap
Definition: MessageSvc.h:35
UnsignedIntegerProperty m_statLevel
Definition: MessageSvc.h:146
void eraseStream() override
Definition: MessageSvc.cpp:588
void incrInactiveCount(MSG::Level level, const std::string &src) override
Definition: MessageSvc.cpp:731
std::map< std::string, std::shared_ptr< std::ostream > > m_loggedStreams
Definition: MessageSvc.h:170
Private helper class to keep the count of messages of a type (MSG::LEVEL).
Definition: MessageSvc.h:153
std::map< std::string, MsgAry > m_sourceMap
Definition: MessageSvc.h:160
std::map< std::string, MSG::Color > ColorMap
Definition: MessageSvc.h:164
bool useColor() const override
Definition: MessageSvc.h:115
std::string getLogColor(int logLevel) const override
Definition: MessageSvc.cpp:717
void setOutputLevel(int new_level) override
Definition: MessageSvc.cpp:703
StatusCode initialize() override
Initialize Service.
Definition: MessageSvc.cpp:119
ThresholdMap m_thresholdMap
Output level threshold map.
Definition: MessageSvc.h:140
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
void reportMessage(const Message &message) override
Definition: MessageSvc.cpp:506
def lock(file)
Definition: locker.py:16
std::recursive_mutex m_reportMutex
Mutex to synchronize multiple threads printing.
Definition: MessageSvc.h:184
std::multimap< StatusCode, Message > MessageMap
Definition: MessageSvc.h:34
MsgAry()=default
Default constructor.
StringArrayProperty m_thresholdProp[MSG::NUM_LEVELS]
Properties controlling.
Definition: MessageSvc.h:143
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
The Message class.
Definition: Message.h:14
StatusCode finalize() override
Finalize Service.
Definition: MessageSvc.cpp:317
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
~MessageSvc() override=default
std::string m_defaultFormat
Default format for the messages.
Definition: MessageSvc.h:141
std::ostream * defaultStream() const override
Definition: MessageSvc.h:92
void setupColors(Property &prop)
Definition: MessageSvc.cpp:191
std::map< std::string, MsgAry > m_inactiveMap
Definition: MessageSvc.h:160
void initColors(Property &prop)
Definition: MessageSvc.cpp:161
std::string m_logColorCodes[MSG::NUM_LEVELS]
Definition: MessageSvc.h:150
int messageCount(MSG::Level logLevel) const override
Definition: MessageSvc.cpp:723
BooleanProperty m_color
Definition: MessageSvc.h:144
BooleanProperty m_inactCount
Definition: MessageSvc.h:161
int outputLevel() const override
Definition: MessageSvc.cpp:689
void tee(const std::string &sourceName, const std::string &logFileName, const std::set< std::string > &declaredOutFileNames)
Definition: MessageSvc.cpp:757
BooleanProperty m_stats
Definition: MessageSvc.h:145
std::map< std::string, std::string > m_loggedStreamsName
Definition: MessageSvc.h:169
void insertMessage(const StatusCode &code, const Message &message) override
Definition: MessageSvc.cpp:641
BooleanProperty m_suppress
Definition: MessageSvc.h:161
void setupThreshold(Property &prop)
Definition: MessageSvc.cpp:268
std::array< int, MSG::NUM_LEVELS > msg
Internal array of counters.
Definition: MessageSvc.h:155
std::string m_defaultTimeFormat
Default format for timestamps in the messages.
Definition: MessageSvc.h:142
void insertStream(int message_type, const std::string &name, std::ostream *stream) override
Definition: MessageSvc.cpp:574
void setDefaultStream(std::ostream *stream) override
Definition: MessageSvc.h:97
string type
Definition: gaudirun.py:151
StringArrayProperty m_logColors[MSG::NUM_LEVELS]
Definition: MessageSvc.h:147
std::recursive_mutex m_thresholdMapMutex
Mutex to synchronize multiple access to m_thresholdMap (.
Definition: MessageSvc.h:191