| The Gaudi Framework v28r2p1 (f1a77ff4)

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