| The Gaudi Framework v32r1 (f65d50dc)

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"
21 #include "GaudiKernel/GaudiException.h"
22 #include "GaudiKernel/IMessageSvc.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
37 bool MsgStream::countInactive() { return m_countInactive; }
38
39 MsgStream::MsgStream( IMessageSvc* svc, int ) : m_service( svc ) {
40 setLevel( svc ? svc->outputLevel() : MSG::INFO );
41 m_currLevel = m_level;
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 );
50 m_currLevel = m_level;
51 m_useColors = ( svc && svc->useColor() );
52 #ifndef NDEBUG
53 m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
54 #endif
55 }
56
57 MsgStream& MsgStream::doOutput() {
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() ) {
62 const Message msg( m_source, m_currLevel, m_stream.str() );
63 if ( m_service ) {
64 m_service->reportMessage( msg, m_currLevel );
65 } else {
66 std::cout << msg << std::endl;
67 }
68 }
69 m_stream.str( "" );
70 } catch ( ... ) {}
71 return *this;
72 }
73
74 void MsgStream::setColor( MSG::Color col ) {
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
85 void MsgStream::setColor( MSG::Color fg, MSG::Color bg ) {
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
99 void MsgStream::resetColor() {
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 }
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:111
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:48
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:99
MsgStream.h
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:53
std::to_string
T to_string(T... args)
std::endl
T endl(T... args)
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:34
std
STL namespace.
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:57
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:38
std::string
STL class.
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:44
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:74
IMessageSvc::outputLevel
virtual int outputLevel() const =0
Retrieve the current output level threshold.
GaudiException.h
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:40
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
std::ostringstream::str
T str(T... args)
IMessageSvc.h
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:46
IMessageSvc::getLogColor
virtual std::string getLogColor(int logLevel) const =0
Get the color codes for various log levels.
Message
The Message class.
Definition: Message.h:17
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:39
IMessageSvc::useColor
virtual bool useColor() const =0
Show whether colors are used.
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
IMessageSvc::reportMessage
virtual void reportMessage(const Message &msg, int outputLevel)=0
Report a message by sending a Message object to the message service.
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:51
va_list
MSG::INFO
Definition: IMessageSvc.h:15
std::cout
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:31
MSG::Color
Color
Definition: IMessageSvc.h:16
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:37
Message.h