| The Gaudi Framework v27r1

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

- File List
- File Members

MsgStream.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_MSGSTREAM_H
2 #define GAUDIKERNEL_MSGSTREAM_H
3
4 // Include files
5 #include "GaudiKernel/IMessageSvc.h"
6 #include "GaudiKernel/SerializeSTL.h"
7 // Standard C++ classes
8 #include <cstdio>
9 #include <string>
10 #include <iomanip>
11 #include <vector>
12 #include <sstream>
13
24 class MsgStream {
25 private:
29 typedef std::ios_base::fmtflags FLAG_TYPE;
30 typedef std::ios_base::iostate STATE_TYPE;
31 protected:
33 IMessageSvc* m_service;
35 std::string m_buffer;
37 std::string m_source;
39 std::ostringstream m_stream;
41 bool m_active = false;
43 MSG::Level m_level;
45 MSG::Level m_currLevel;
47 bool m_useColors;
50 IInactiveMessageCounter* m_inactCounter = nullptr;
52 static bool m_countInactive;
53
54 public:
56 GAUDI_API MsgStream(IMessageSvc* svc, int buffer_length=128);
58 GAUDI_API MsgStream(IMessageSvc* svc, std::string source, int buffer_length=128);
60 MsgStream(const MsgStream& msg)
61 : m_service(msg.m_service),
62 m_active(msg.m_active),
63 m_level(msg.m_level),
64 m_currLevel(msg.m_currLevel),
65 m_useColors(msg.m_useColors),
66 m_inactCounter(msg.m_inactCounter)
67 {
68 try { // ignore exception if we cannot copy the string
69 m_source = msg.m_source;
70 }
71 catch (...) {}
72 }
74 GAUDI_API virtual ~MsgStream() = default;
76 MsgStream& report(int lvl) {
77 lvl = (lvl >= MSG::NUM_LEVELS) ?
78 MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
79 if ((m_currLevel=MSG::Level(lvl)) >= level()) {
80 activate();
81 } else {
82 deactivate();
83 #ifndef NDEBUG
84 if (MsgStream::countInactive() && m_inactCounter) {
85 m_inactCounter->incrInactiveCount(MSG::Level(lvl),m_source);
86 }
87 #endif
88 }
89 return *this;
90 }
92 virtual GAUDI_API MsgStream& doOutput();
94 const std::string& buffer() const {
95 return m_buffer;
96 }
98 std::ostringstream& stream() {
99 return m_stream;
100 }
102 void setMsgSvc( IMessageSvc* svc ) {
103 m_service = svc;
104 }
106 void setLevel(int level) {
107 level = (level >= MSG::NUM_LEVELS) ?
108 MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
109 m_level = MSG::Level(level);
110 }
112 MSG::Level level() {
113 return m_level;
114 }
116 MSG::Level currentLevel() {
117 return m_currLevel;
118 }
120 void activate() {
121 m_active = true;
122 }
124 void deactivate() {
125 m_active = false;
126 }
128 bool isActive() const {
129 return m_active;
130 }
131 // oMsgStream flush emulation
132 MsgStream& flush() {
133 if ( isActive() ) m_stream.flush();
134 return *this;
135 }
136 // oMsgStream write emulation
137 MsgStream& write(const char* buff,int len) {
138 if ( isActive() ) m_stream.write(buff, len);
139 return *this;
140 }
142 MsgStream& operator<<(MsgStream& (*_f)(MsgStream&)) {
143 if ( isActive() ) _f(*this);
144 return *this;
145 }
147 MsgStream& operator<<(std::ostream& (*_f)(std::ostream&)) {
148 if ( isActive() ) _f(m_stream);
149 return *this;
150 }
152 MsgStream& operator<<(std::ios& (*_f)(std::ios&)) {
153 if ( isActive() ) _f(m_stream);
154 return *this;
155 }
157 MsgStream& operator<< (MSG::Level level) {
158 return report(level);
159 }
160 MsgStream& operator<<(longlong arg) {
161 try {
162 // this may throw, and we cannot afford it if the stream is used in a catch block
163 if(isActive()) {
164 m_stream << arg;
165 }
166 } catch (...) {}
167 return *this;
168 }
169
171 MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&)) {
172 if ( isActive() ) _f(m_stream);
173 return *this;
174 }
175
177 long flags() const {
178 return isActive() ? m_stream.flags() : 0;
179 }
180 long flags(FLAG_TYPE v) {
181 return isActive() ? m_stream.flags(v) : 0;
182 }
183 long setf(FLAG_TYPE v) {
184 return isActive() ? m_stream.setf(v) : 0;
185 }
186 int width() const {
187 return isActive() ? m_stream.width() : 0;
188 }
189 int width(int v) {
190 return isActive() ? m_stream.width(v) : 0;
191 }
192 char fill() const {
193 return isActive() ? m_stream.fill() : (char)-1;
194 }
195 char fill(char v) {
196 return isActive() ? m_stream.fill(v) : (char)-1;
197 }
198 int precision() const {
199 return isActive() ? m_stream.precision(): 0;
200 }
201 int precision(int v) {
202 return isActive() ? m_stream.precision(v): 0;
203 }
204 int rdstate() const {
205 return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
206 }
207 int good() const {
208 return isActive() ? m_stream.good () : 0;
209 }
210 int eof() const {
211 return isActive() ? m_stream.eof () : 0;
212 }
213 int bad() const {
214 return isActive() ? m_stream.bad() : 0;
215 }
216 long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
217 return isActive() ? m_stream.setf(_f, _m) : 0;
218 }
219 void unsetf(FLAG_TYPE _l) {
220 if ( isActive() ) m_stream.unsetf(_l);
221 }
222 void clear(STATE_TYPE _i = std::ios_base::failbit) {
223 if ( isActive() ) m_stream.clear(_i);
224 }
225
227 GAUDI_API void setColor(MSG::Color col);
229 GAUDI_API void setColor(MSG::Color fg, MSG::Color bg);
230
232 GAUDI_API void resetColor();
233
236 static GAUDI_API bool enableCountInactive(bool value = true);
237
239 static GAUDI_API bool countInactive();
240
241 };
242
244 inline MsgStream& endmsg(MsgStream& s) {
245 return s.doOutput();
246 }
247 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_ENDREQ)
248 #define endreq endmsg
250 #endif
251
253 GAUDI_API std::string format(const char*, ... );
254
255 #ifdef _WIN32
256 template<class _E> inline
257 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj) {
258 #if _MSC_VER > 1300
259 if ( s.isActive() ) s.stream().fill(obj._Fill);
260 #else
261 if ( s.isActive() ) s.stream().fill(obj._Ch);
262 #endif
263 return s;
264 }
265 template<class _Tm> inline
266 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
267 #if _MSC_VER > 1300
268 if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
269 #else
270 if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
271 #endif
272 return s;
273 }
274 #elif defined (__GNUC__)
275 inline MsgStream& operator << (MsgStream& s,
276 const std::_Setiosflags &manip) {
277 try {
278 // this may throw, and we cannot afford it if the stream is used in a catch block
279 if ( s.isActive() ) s.stream() << manip;
280 } catch(...) {}
281 return s;
282 }
283 inline MsgStream& operator << (MsgStream& s,
284 const std::_Resetiosflags &manip) {
285 try {
286 // this may throw, and we cannot afford it if the stream is used in a catch block
287 if ( s.isActive() ) s.stream() << manip;
288 } catch (...) {}
289 return s;
290 }
291 inline MsgStream& operator << (MsgStream& s,
292 const std::_Setbase &manip) {
293 try {
294 // this may throw, and we cannot afford it if the stream is used in a catch block
295 if ( s.isActive() ) s.stream() << manip;
296 } catch (...) {}
297 return s;
298 }
299 inline MsgStream& operator << (MsgStream& s,
300 const std::_Setprecision &manip) {
301 try {
302 // this may throw, and we cannot afford it if the stream is used in a catch block
303 if ( s.isActive() ) s.stream() << manip;
304 } catch (...) {}
305 return s;
306 }
307 inline MsgStream& operator << (MsgStream& s,
308 const std::_Setw &manip) {
309 try {
310 // this may throw, and we cannot afford it if the stream is used in a catch block
311 if ( s.isActive() ) s.stream() << manip;
312 } catch (...) {}
313 return s;
314 }
315
316 namespace MSG {
317 inline
318 MsgStream& dec(MsgStream& log) {
319 log.setf(std::ios_base::dec, std::ios_base::basefield);
320 return log;
321 }
322 inline
323 MsgStream& hex(MsgStream& log) {
324 log.setf(std::ios_base::hex, std::ios_base::basefield);
325 return log;
326 }
327 }
328
329 #else // GCC, version << 3
330 template<class _Tm> inline
332 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip) {
333 try {
334 // this may throw, and we cannot afford it if the stream is used in a catch block
335 if ( s.isActive() ) s.stream() << manip;
336 } catch (...) {}
337 return s;
338 }
339 #endif // WIN32 or (__GNUC__)
340
343 inline MsgStream& operator<< (MsgStream& s, const char *arg){
344 try {
345 // this may throw, and we cannot afford it if the stream is used in a catch block
346 if ( s.isActive() ) s.stream() << arg;
347 } catch (...) {}
348 return s;
349 }
350
352 template <typename T>
353 MsgStream& operator<< (MsgStream& lhs, const T& arg) {
354 using namespace GaudiUtils;
355 if(lhs.isActive())
356 try {
357 // this may throw, and we cannot afford it if the stream is used in a catch block
358 lhs.stream() << arg;
359 }
360 catch (...) {}
361 return lhs;
362 }
363
364 #ifdef __GNUC__
365 template<typename T>
367 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
368 if ( lhs.isActive() )
369 try {
370 // this may throw, and we cannot afford it if the stream is used in a catch block
371 lhs.stream() << manip;
372 } catch(...) {}
373 return lhs;
374 }
375 #endif
376
377 #endif // GAUDIKERNEL_MSGSTREAM_H
MsgStream::stream
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:98
std::ostringstream::setf
T setf(T...args)
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream::operator<<
MsgStream & operator<<(longlong arg)
Definition: MsgStream.h:160
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:94
MsgStream::currentLevel
MSG::Level currentLevel()
Retrieve current stream output level.
Definition: MsgStream.h:116
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
MsgStream::width
int width(int v)
Definition: MsgStream.h:189
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:47
MsgStream::fill
char fill() const
Definition: MsgStream.h:192
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:108
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:204
MsgStream::precision
int precision() const
Definition: MsgStream.h:198
GaudiMP.FdsRegistry.msg
tuple msg
Definition: FdsRegistry.py:8
MsgStream::precision
int precision(int v)
Definition: MsgStream.h:201
MsgStream::level
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
Gaudi.Configuration.log
tuple log
Definition: Configuration.py:13
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:52
std::ostringstream::good
T good(T...args)
MsgStream::eof
int eof() const
Definition: MsgStream.h:210
MsgStream::bad
int bad() const
Definition: MsgStream.h:213
IInactiveMessageCounter
Definition: IMessageSvc.h:189
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::ostringstream::precision
T precision(T...args)
std::ostringstream::rdstate
T rdstate(T...args)
MsgStream::write
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:137
IInactiveMessageCounter::incrInactiveCount
virtual void incrInactiveCount(MSG::Level level, const std::string &src)=0
Increment deactivated message count.
MsgStream::report
MsgStream & report(int lvl)
Initialize report of new message: activate if print level is sufficient.
Definition: MsgStream.h:76
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
SerializeSTL.h
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
std::string
STL class.
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:43
operator<<
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:332
std::ostringstream::eof
T eof(T...args)
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:81
MsgStream::STATE_TYPE
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:30
MsgStream::good
int good() const
Definition: MsgStream.h:207
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:142
MSG::Level
Level
Definition: IMessageSvc.h:15
std::ostringstream::flags
T flags(T...args)
MsgStream::operator<<
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:171
MsgStream::setf
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:216
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:152
__longlong
Definition: Kernel.h:56
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:222
std::ostringstream
STL class.
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:124
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:132
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::width
T width(T...args)
MSG::NIL
Definition: IMessageSvc.h:16
dump_component_properties.value
list value
Definition: dump_component_properties.py:12
IMessageSvc.h
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:195
std::ostringstream::clear
T clear(T...args)
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:102
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:41
std::ostringstream::flush
T flush(T...args)
MsgStream::m_buffer
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:35
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:45
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:43
MsgStream::flags
long flags() const
IOS emulation.
Definition: MsgStream.h:177
std::ostringstream::write
T write(T...args)
MSG::NUM_LEVELS
Definition: IMessageSvc.h:24
MSG
Print levels enumeration.
Definition: IMessageSvc.h:14
MsgStream::width
int width() const
Definition: MsgStream.h:186
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:139
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
gaudirun.s
string s
Definition: gaudirun.py:245
std::dec
T dec(T...args)
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:147
std::ostringstream::unsetf
T unsetf(T...args)
std::ostringstream::fill
T fill(T...args)
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:120
MsgStream::MsgStream
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:60
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:50
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:180
MsgStream::FLAG_TYPE
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:29
MsgStream::setf
long setf(FLAG_TYPE v)
Definition: MsgStream.h:183
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
std::ios_base
STL class.
std::ostream
STL class.
std::ostringstream::bad
T bad(T...args)
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:32
MSG::ALWAYS
Definition: IMessageSvc.h:23
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
MSG::Color
Color
Definition: IMessageSvc.h:26
MsgStream::~MsgStream
virtual GAUDI_API ~MsgStream()=default
Standard destructor.
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:38
MsgStream::unsetf
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:219