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"
23 #include "GaudiKernel/GaudiException.h"
24 #include "GaudiKernel/IMessageSvc.h"
25 
26 #include <iostream>
27 #include <stdarg.h>
28 #include <stdio.h>
29 
30 bool MsgStream::m_countInactive = false;
31 
33  bool old = m_countInactive;
35  return old;
36 }
37 
39  return m_countInactive;
40 }
41 
42 
44 : m_service(svc),
45  m_source(""),
46  m_active(false),
47  m_inactCounter(0)
48 {
49  setLevel((0==svc) ? MSG::INFO : svc->outputLevel());
50  m_useColors = (0==svc) ? false : svc->useColor();
51 #ifndef NDEBUG
52  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
53 #endif
54 }
55 
56 MsgStream::MsgStream(IMessageSvc* svc, const std::string& source, int)
57 : m_service(svc),
58  m_source(source),
59  m_active(false),
60  m_inactCounter(0)
61 {
62  setLevel((0==svc) ? MSG::INFO : svc->outputLevel(source));
63  m_useColors = (0==svc) ? false : svc->useColor();
64 #ifndef NDEBUG
65  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
66 #endif
67 }
68 
70 }
71 
73  try {
74  // This piece of code may throw and we cannot afford it when we print a message
75  // in the middle of a catch block.
76  if ( isActive() ) {
78  if ( m_service != 0 ) {
80  }
81  else {
82  std::cout << msg << std::endl;
83  }
84  }
85  m_stream.str("");
86  } catch(...) {}
87  return *this;
88 }
89 
91 #ifndef _WIN32
92  if ( m_useColors) {
93  int fc = 90 + col;
94  try { // this may throw and we must not do it
95  m_stream << "\x1b[" << fc << ";1m";
96  }
97  catch(...) {}
98  }
99 #endif
100 }
101 
103 #ifndef _WIN32
104  if ( m_useColors ) {
105  try { // this may throw and we must not do it
106  int fc = 90 + fg;
107  m_stream << "\x1b[" << fc;
108  int bc = 100 + bg;
109  m_stream << ";" << bc;
110  m_stream << ";1m";
111  }
112  catch(...) {}
113  }
114 #endif
115 }
116 
118 #ifndef _WIN32
119  if ( m_useColors ) {
120  try { // this may throw and we must not do it
121  m_stream << "\x1b[m" << m_service->getLogColor(m_currLevel);
122  }
123  catch(...) {}
124  }
125 #endif
126 }
127 
128 #ifdef WIN32
129 // Disable warning
130 // C4996: 'vsprintf': This function or variable may be unsafe.
131 #pragma warning(disable:4996)
132 #endif
133 std::string format( const char* fmt, ... )
134 {
135  const int buffsize = 2048;
136  static char buffer[buffsize];
137  va_list arguments;
138  va_start( arguments, fmt );
139  if( vsprintf(buffer, fmt, arguments) >= buffsize )
140  throw GaudiException("Insufficient buffer size (2048) when formatting message",
141  "MsgStream", 0);
142  return std::string(buffer);
143 }
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:33
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
bool m_useColors
use colors
Definition: MsgStream.h:47
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:117
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:128
return false
Definition: Bootstrap.cpp:338
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:72
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:52
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:50
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:37
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:90
virtual int outputLevel() const =0
Retrieve the current output level threshold.
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
virtual std::string getLogColor(int logLevel) const =0
Get the color codes for various log levels.
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
virtual GAUDI_API ~MsgStream()
Standard destructor.
Definition: MsgStream.cpp:69
virtual bool useColor() const =0
Show whether colors are used.
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.
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