Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 #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 
31 bool MsgStream::enableCountInactive( bool value ) {
32  bool old = m_countInactive;
33  m_countInactive = value;
34  return old;
35 }
36 
38 
40  setLevel( svc ? svc->outputLevel() : MSG::INFO );
42  m_useColors = ( svc ? svc->useColor() : false );
43 #ifndef NDEBUG
44  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
45 #endif
46 }
47 
48 MsgStream::MsgStream( IMessageSvc* svc, std::string source, int ) : m_service( svc ), m_source( std::move( source ) ) {
49  setLevel( svc ? svc->outputLevel( m_source ) : MSG::INFO );
51  m_useColors = ( svc && svc->useColor() );
52 #ifndef NDEBUG
53  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
54 #endif
55 }
56 
58  try {
59  // This piece of code may throw and we cannot afford it when we print a message
60  // in the middle of a catch block.
61  if ( isActive() ) {
63  if ( m_service ) {
65  } else {
66  std::cout << msg << std::endl;
67  }
68  }
69  m_stream.str( "" );
70  } catch ( ... ) {}
71  return *this;
72 }
73 
75 #ifndef _WIN32
76  if ( m_useColors ) {
77  int fc = 90 + col;
78  try { // this may throw and we must not do it
79  m_stream << "\x1b[" << fc << ";1m";
80  } catch ( ... ) {}
81  }
82 #endif
83 }
84 
86 #ifndef _WIN32
87  if ( m_useColors ) {
88  try { // this may throw and we must not do it
89  int fc = 90 + fg;
90  m_stream << "\x1b[" << fc;
91  int bc = 100 + bg;
92  m_stream << ";" << bc;
93  m_stream << ";1m";
94  } catch ( ... ) {}
95  }
96 #endif
97 }
98 
100 #ifndef _WIN32
101  if ( m_useColors ) {
102  try { // this may throw and we must not do it
103  m_stream << "\x1b[m" << m_service->getLogColor( m_currLevel );
104  } catch ( ... ) {}
105  }
106 #endif
107 }
108 
109 std::string format( const char* fmt, ... ) {
110  const int buffsize = 2048;
111  static char buffer[buffsize];
112  va_list arguments;
113  va_start( arguments, fmt );
114  if ( vsnprintf( buffer, buffsize, fmt, arguments ) >= buffsize )
115  throw GaudiException( "Insufficient buffer size (" + std::to_string( buffsize ) + ") when formatting message",
116  "MsgStream", StatusCode::FAILURE );
117  va_end( arguments );
118  return std::string( buffer );
119 }
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
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:92
bool m_useColors
use colors
Definition: MsgStream.h:48
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:99
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:53
T to_string(T...args)
T endl(T...args)
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:111
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:34
STL namespace.
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:57
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:38
STL class.
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:44
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:74
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:40
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:46
The Message class.
Definition: Message.h:17
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:39
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
constexpr static const auto FAILURE
Definition: StatusCode.h:86
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:51
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:37
virtual int outputLevel() const =0
Retrieve the current output level threshold.