| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
33 bool old = m_countInactive;
34 m_countInactive = value;
35 return old;
36 }
37
38 bool MsgStream::countInactive() { return m_countInactive; }
39
40 MsgStream::MsgStream( IMessageSvc* svc, int ) : m_service( svc )
41 {
42 setLevel( svc ? svc->outputLevel() : MSG::INFO );
43 m_currLevel = m_level;
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 );
53 m_currLevel = m_level;
54 m_useColors = ( svc && svc->useColor() );
55 #ifndef NDEBUG
56 m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
57 #endif
58 }
59
60 MsgStream& MsgStream::doOutput()
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() ) {
66 const Message msg( m_source, m_currLevel, m_stream.str() );
67 if ( m_service ) {
68 m_service->reportMessage( msg, m_currLevel );
69 } else {
70 std::cout << msg << std::endl;
71 }
72 }
73 m_stream.str( "" );
74 } catch ( ... ) {
75 }
76 return *this;
77 }
78
79 void MsgStream::setColor( MSG::Color col )
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
92 void MsgStream::setColor( MSG::Color fg, MSG::Color bg )
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
108 void MsgStream::resetColor()
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", 0 );
129 va_end( arguments );
130 return std::string( buffer );
131 }
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
IMessageSvc::useColor
virtual bool useColor() const =0
Show whether colors are used.
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:96
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:49
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:108
MsgStream.h
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:54
std::to_string
T to_string(T...args)
std::endl
T endl(T...args)
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:116
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:35
std
STL namespace.
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:60
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:39
std::string
STL class.
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:45
IMessageSvc::getLogColor
virtual std::string getLogColor(int logLevel) const =0
Get the color codes for various log levels.
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:79
GaudiException.h
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:41
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
std::ostringstream::str
T str(T...args)
IMessageSvc.h
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:47
Message
The Message class.
Definition: Message.h:15
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:40
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
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:52
va_list
MSG::INFO
Definition: IMessageSvc.h:16
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:17
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:38
Message.h
IMessageSvc::outputLevel
virtual int outputLevel() const =0
Retrieve the current output level threshold.