| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
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 <stdarg.h>
28 #include <stdio.h>
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 m_source(""),
46 m_active(false),
47 m_inactCounter(0)
48 {
49 setLevel((0==svc) ? MSG::INFO : svc->outputLevel());
50 m_useColors = (0==svc) ? false : svc->useColor();
51 #ifndef NDEBUG
52 m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
53 #endif
54 }
55
56 MsgStream::MsgStream(IMessageSvc* svc, const std::string& source, int)
57 : m_service(svc),
58 m_source(source),
59 m_active(false),
60 m_inactCounter(0)
61 {
62 setLevel((0==svc) ? MSG::INFO : svc->outputLevel(source));
63 m_useColors = (0==svc) ? false : svc->useColor();
64 #ifndef NDEBUG
65 m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
66 #endif
67 }
68
69 MsgStream::~MsgStream() {
70 }
71
72 MsgStream& MsgStream::doOutput() {
73 try {
74 // This piece of code may throw and we cannot afford it when we print a message
75 // in the middle of a catch block.
76 if ( isActive() ) {
77 Message msg(m_source,m_currLevel,m_stream.str());
78 if ( m_service != 0 ) {
79 m_service->reportMessage (msg, m_currLevel);
80 }
81 else {
82 std::cout << msg << std::endl;
83 }
84 }
85 m_stream.str("");
86 } catch(...) {}
87 return *this;
88 }
89
90 void MsgStream::setColor(MSG::Color col) {
91 #ifndef _WIN32
92 if ( m_useColors) {
93 int fc = 90 + col;
94 try { // this may throw and we must not do it
95 m_stream << "\x1b[" << fc << ";1m";
96 }
97 catch(...) {}
98 }
99 #endif
100 }
101
102 void MsgStream::setColor(MSG::Color fg, MSG::Color bg) {
103 #ifndef _WIN32
104 if ( m_useColors ) {
105 try { // this may throw and we must not do it
106 int fc = 90 + fg;
107 m_stream << "\x1b[" << fc;
108 int bc = 100 + bg;
109 m_stream << ";" << bc;
110 m_stream << ";1m";
111 }
112 catch(...) {}
113 }
114 #endif
115 }
116
117 void MsgStream::resetColor() {
118 #ifndef _WIN32
119 if ( m_useColors ) {
120 try { // this may throw and we must not do it
121 m_stream << "\x1b[m" << m_service->getLogColor(m_currLevel);
122 }
123 catch(...) {}
124 }
125 #endif
126 }
127
128 #ifdef WIN32
129 // Disable warning
130 // C4996: 'vsprintf': This function or variable may be unsafe.
131 #pragma warning(disable:4996)
132 #endif
133 std::string format( const char* fmt, ... )
134 {
135 const int buffsize = 2048;
136 static char buffer[buffsize];
137 va_list arguments;
138 va_start( arguments, fmt );
139 if( vsprintf(buffer, fmt, arguments) >= buffsize )
140 throw GaudiException("Insufficient buffer size (2048) when formatting message",
141 "MsgStream", 0);
142 return std::string(buffer);
143 }