The Gaudi Framework  v30r3 (a5ef0a68)
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 #include "GaudiKernel/MsgStream.h"
23 #include "GaudiKernel/Message.h"
24 
25 #include <cstdarg>
26 #include <cstdio>
27 #include <iostream>
28 
29 bool MsgStream::m_countInactive = false;
30 
32 {
33  bool old = m_countInactive;
34  m_countInactive = value;
35  return old;
36 }
37 
39 
41 {
42  setLevel( svc ? svc->outputLevel() : MSG::INFO );
44  m_useColors = ( svc ? svc->useColor() : false );
45 #ifndef NDEBUG
46  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
47 #endif
48 }
49 
50 MsgStream::MsgStream( IMessageSvc* svc, std::string source, int ) : m_service( svc ), m_source( std::move( source ) )
51 {
52  setLevel( svc ? svc->outputLevel( m_source ) : MSG::INFO );
54  m_useColors = ( svc && svc->useColor() );
55 #ifndef NDEBUG
56  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
57 #endif
58 }
59 
61 {
62  try {
63  // This piece of code may throw and we cannot afford it when we print a message
64  // in the middle of a catch block.
65  if ( isActive() ) {
67  if ( m_service ) {
69  } else {
70  std::cout << msg << std::endl;
71  }
72  }
73  m_stream.str( "" );
74  } catch ( ... ) {
75  }
76  return *this;
77 }
78 
80 {
81 #ifndef _WIN32
82  if ( m_useColors ) {
83  int fc = 90 + col;
84  try { // this may throw and we must not do it
85  m_stream << "\x1b[" << fc << ";1m";
86  } catch ( ... ) {
87  }
88  }
89 #endif
90 }
91 
93 {
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  } catch ( ... ) {
103  }
104  }
105 #endif
106 }
107 
109 {
110 #ifndef _WIN32
111  if ( m_useColors ) {
112  try { // this may throw and we must not do it
113  m_stream << "\x1b[m" << m_service->getLogColor( m_currLevel );
114  } catch ( ... ) {
115  }
116  }
117 #endif
118 }
119 
120 std::string format( const char* fmt, ... )
121 {
122  const int buffsize = 2048;
123  static char buffer[buffsize];
124  va_list arguments;
125  va_start( arguments, fmt );
126  if ( vsnprintf( buffer, buffsize, fmt, arguments ) >= buffsize )
127  throw GaudiException( "Insufficient buffer size (" + std::to_string( buffsize ) + ") when formatting message",
128  "MsgStream", StatusCode::FAILURE );
129  va_end( arguments );
130  return std::string( buffer );
131 }
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
virtual bool useColor() const =0
Show whether colors are used.
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:96
bool m_useColors
use colors
Definition: MsgStream.h:49
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:54
T to_string(T...args)
T endl(T...args)
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:116
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:35
STL namespace.
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:60
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:39
STL class.
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:45
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:79
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:41
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:47
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:40
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
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:52
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:31
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.