All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MsgStream.cpp
Go to the documentation of this file.
1 //====================================================================
2 // MsgStream.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : System ( The LHCb Offline System)
6 //
7 // Description: Class to ease error logging to the message service
8 //
9 // Author : M.Frank
10 // History :
11 // +---------+----------------------------------------------+---------
12 // | Date | Comment | Who
13 // +---------+----------------------------------------------+---------
14 // | 29/10/98| Initial version | MF
15 // +---------+----------------------------------------------+---------
16 //
17 //====================================================================
18 #define KERNEL_MSGSTREAM_CPP
19 
20 
21 #include "GaudiKernel/MsgStream.h"
22 #include "GaudiKernel/Message.h"
25 
26 #include <iostream>
27 #include <cstdarg>
28 #include <cstdio>
29 
30 bool MsgStream::m_countInactive = false;
31 
33  bool old = m_countInactive;
34  m_countInactive = value;
35  return old;
36 }
37 
39  return m_countInactive;
40 }
41 
42 
44 : m_service(svc)
45 {
46  setLevel( svc ? svc->outputLevel() : MSG::INFO );
47  m_useColors = ( svc ? svc->useColor() : false );
48 #ifndef NDEBUG
49  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
50 #endif
51 }
52 
54 : m_service(svc),
55  m_source(std::move(source))
56 {
57  setLevel( svc ? svc->outputLevel(m_source) : MSG::INFO );
58  m_useColors = ( svc && svc->useColor() );
59 #ifndef NDEBUG
60  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
61 #endif
62 }
63 
65  try {
66  // This piece of code may throw and we cannot afford it when we print a message
67  // in the middle of a catch block.
68  if ( isActive() ) {
70  if ( m_service ) {
72  } else {
73  std::cout << msg << std::endl;
74  }
75  }
76  m_stream.str("");
77  } catch(...) {}
78  return *this;
79 }
80 
82 #ifndef _WIN32
83  if ( m_useColors) {
84  int fc = 90 + col;
85  try { // this may throw and we must not do it
86  m_stream << "\x1b[" << fc << ";1m";
87  }
88  catch(...) {}
89  }
90 #endif
91 }
92 
94 #ifndef _WIN32
95  if ( m_useColors ) {
96  try { // this may throw and we must not do it
97  int fc = 90 + fg;
98  m_stream << "\x1b[" << fc;
99  int bc = 100 + bg;
100  m_stream << ";" << bc;
101  m_stream << ";1m";
102  }
103  catch(...) {}
104  }
105 #endif
106 }
107 
109 #ifndef _WIN32
110  if ( m_useColors ) {
111  try { // this may throw and we must not do it
112  m_stream << "\x1b[m" << m_service->getLogColor(m_currLevel);
113  }
114  catch(...) {}
115  }
116 #endif
117 }
118 
119 std::string format( const char* fmt, ... )
120 {
121  const int buffsize = 2048;
122  static char buffer[buffsize];
123  va_list arguments;
124  va_start( arguments, fmt );
125  if( vsnprintf(buffer, buffsize, fmt, arguments) >= buffsize )
126  throw GaudiException("Insufficient buffer size (" + std::to_string(buffsize) + ") when formatting message",
127  "MsgStream", 0);
128  va_end( arguments );
129  return std::string(buffer);
130 }
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
virtual bool useColor() const =0
Show whether colors are used.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:94
bool m_useColors
use colors
Definition: MsgStream.h:47
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:108
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:52
T to_string(T...args)
T endl(T...args)
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:128
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:33
STL namespace.
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:64
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:37
STL class.
virtual std::string getLogColor(int logLevel) const =0
Get the color codes for various log levels.
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:81
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:39
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:45
The Message class.
Definition: Message.h:15
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:43
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
virtual void reportMessage(const Message &msg, int outputLevel)=0
Report a message by sending a Message object to the message service.
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:50
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:32
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:38
virtual int outputLevel() const =0
Retrieve the current output level threshold.