The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
MsgStream.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
15#include <iomanip>
16#include <sstream>
17#include <string>
18
29class MsgStream {
30private:
34 typedef std::ios_base::fmtflags FLAG_TYPE;
35 typedef std::ios_base::iostate STATE_TYPE;
36
37protected:
41 std::string m_buffer;
43 std::string m_source;
45 std::ostringstream m_stream;
47 bool m_active = false;
58 static bool m_countInactive;
59
60public:
62 GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
64 GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
66 MsgStream( const MsgStream& msg )
67 : m_service( msg.m_service )
68 , m_active( msg.m_active )
69 , m_level( msg.m_level )
73 try { // ignore exception if we cannot copy the string
74 m_source = msg.m_source;
75 } catch ( ... ) {}
76 }
77
78 GAUDI_API virtual ~MsgStream() = default;
80 MsgStream& report( int lvl ) {
81 lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
82 if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
83 activate();
84 } else {
85 deactivate();
86#ifndef NDEBUG
88 m_inactCounter->incrInactiveCount( MSG::Level( lvl ), m_source );
89 }
90#endif
91 }
92 return *this;
93 }
94
95 virtual GAUDI_API MsgStream& doOutput();
97 const std::string& buffer() const { return m_buffer; }
99 std::ostringstream& stream() { return m_stream; }
101 void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
103 void setLevel( int level ) {
106 }
107
108 MSG::Level level() const { return m_level; }
112 void activate() { m_active = true; }
114 void deactivate() { m_active = false; }
116 bool isActive() const { return m_active; }
117 // oMsgStream flush emulation
119 if ( isActive() ) m_stream.flush();
120 return *this;
121 }
122 // oMsgStream write emulation
123 MsgStream& write( const char* buff, int len ) {
124 if ( isActive() ) m_stream.write( buff, len );
125 return *this;
126 }
127
129 if ( isActive() ) _f( *this );
130 return *this;
131 }
132
133 MsgStream& operator<<( std::ostream& ( *_f )(std::ostream&)) {
134 if ( isActive() ) _f( m_stream );
135 return *this;
136 }
137
138 MsgStream& operator<<( std::ios& ( *_f )(std::ios&)) {
139 if ( isActive() ) _f( m_stream );
140 return *this;
141 }
142
144 MsgStream& operator<<( long long arg ) {
145 try {
146 // this may throw, and we cannot afford it if the stream is used in a catch block
147 if ( isActive() ) { m_stream << arg; }
148 } catch ( ... ) {}
149 return *this;
150 }
151
153 MsgStream& operator<<( std::ios_base& ( *_f )(std::ios_base&)) {
154 if ( isActive() ) _f( m_stream );
155 return *this;
156 }
157
159 std::streamsize width() const { return isActive() ? m_stream.width() : 0; }
160 std::streamsize width( std::streamsize v ) { return isActive() ? m_stream.width( v ) : 0; }
161 std::streamsize precision() const { return isActive() ? m_stream.precision() : 0; }
162 std::streamsize precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
163
164 long flags() const { return isActive() ? m_stream.flags() : 0; }
165 long flags( FLAG_TYPE v ) { return isActive() ? m_stream.flags( v ) : 0; }
166 long setf( FLAG_TYPE v ) { return isActive() ? m_stream.setf( v ) : 0; }
167 char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
168 char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
169 int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
170 bool good() const { return isActive() && m_stream.good(); }
171 bool eof() const { return isActive() && m_stream.eof(); }
172 bool bad() const { return isActive() && m_stream.bad(); }
173 int setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
174 void unsetf( FLAG_TYPE _l ) {
175 if ( isActive() ) m_stream.unsetf( _l );
176 }
177 void clear( STATE_TYPE _i = std::ios_base::failbit ) {
178 if ( isActive() ) m_stream.clear( _i );
179 }
180
182 GAUDI_API void setColor( MSG::Color col );
185
187 GAUDI_API void resetColor();
188
191 static GAUDI_API bool enableCountInactive( bool value = true );
192
194 static GAUDI_API bool countInactive();
195};
196
198inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
199
201GAUDI_API std::string format( const char*, ... );
202
203#if defined( __GNUC__ )
204# ifndef __APPLE__
205inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
206 try {
207 // this may throw, and we cannot afford it if the stream is used in a catch block
208 if ( s.isActive() ) s.stream() << manip;
209 } catch ( ... ) {}
210 return s;
211}
212inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
213 try {
214 // this may throw, and we cannot afford it if the stream is used in a catch block
215 if ( s.isActive() ) s.stream() << manip;
216 } catch ( ... ) {}
217 return s;
218}
219inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
220 try {
221 // this may throw, and we cannot afford it if the stream is used in a catch block
222 if ( s.isActive() ) s.stream() << manip;
223 } catch ( ... ) {}
224 return s;
225}
226inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
227 try {
228 // this may throw, and we cannot afford it if the stream is used in a catch block
229 if ( s.isActive() ) s.stream() << manip;
230 } catch ( ... ) {}
231 return s;
232}
233inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip ) {
234 try {
235 // this may throw, and we cannot afford it if the stream is used in a catch block
236 if ( s.isActive() ) s.stream() << manip;
237 } catch ( ... ) {}
238 return s;
239}
240# endif // not __APPLE__
241#else // GCC, version << 3
243template <class _Tm>
244inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
245 try {
246 // this may throw, and we cannot afford it if the stream is used in a catch block
247 if ( s.isActive() ) s.stream() << manip;
248 } catch ( ... ) {}
249 return s;
250}
251#endif // __GNUC__
252
253namespace MSG {
254 inline MsgStream& dec( MsgStream& log ) {
255 log << std::dec;
256 return log;
257 }
258 inline MsgStream& hex( MsgStream& log ) {
259 log << std::hex;
260 return log;
261 }
262} // namespace MSG
263
266inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
267 try {
268 // this may throw, and we cannot afford it if the stream is used in a catch block
269 if ( s.isActive() ) s.stream() << arg;
270 } catch ( ... ) {}
271 return s;
272}
273
275template <typename T>
276MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
277 using namespace GaudiUtils;
278 if ( lhs.isActive() ) try {
279 // this may throw, and we cannot afford it if the stream is used in a catch block
280 lhs.stream() << arg;
281 } catch ( ... ) {}
282 return lhs;
283}
284
285#if defined( __GNUC__ ) and not defined( __APPLE__ )
287template <typename T>
288MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
289 if ( lhs.isActive() ) try {
290 // this may throw, and we cannot afford it if the stream is used in a catch block
291 lhs.stream() << manip;
292 } catch ( ... ) {}
293 return lhs;
294}
295#endif
#define GAUDI_API
Definition Kernel.h:49
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition MsgStream.h:244
Provide serialization function (output only) for some common STL classes (vectors,...
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
std::streamsize precision(int v)
Definition MsgStream.h:162
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition MsgStream.h:45
MSG::Level level() const
Retrieve output level.
Definition MsgStream.h:108
std::string m_source
Use std::string for source information to be passed to the message service.
Definition MsgStream.h:43
std::string m_buffer
Use standard string for information buffering.
Definition MsgStream.h:41
void deactivate()
Deactivate MsgStream.
Definition MsgStream.h:114
void activate()
Activate MsgStream.
Definition MsgStream.h:112
bool good() const
Definition MsgStream.h:170
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition MsgStream.h:143
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition MsgStream.h:58
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition MsgStream.h:56
std::ios_base::iostate STATE_TYPE
Definition MsgStream.h:35
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition MsgStream.cpp:21
bool m_useColors
use colors
Definition MsgStream.h:53
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition MsgStream.cpp:27
MSG::Level m_level
Debug level of the message service.
Definition MsgStream.h:49
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition MsgStream.cpp:47
MsgStream(const MsgStream &msg)
Copy constructor.
Definition MsgStream.h:66
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition MsgStream.h:138
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition MsgStream.cpp:85
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition MsgStream.h:177
long flags(FLAG_TYPE v)
Definition MsgStream.h:165
std::streamsize width(std::streamsize v)
Definition MsgStream.h:160
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition MsgStream.h:133
MsgStream & write(const char *buff, int len)
Definition MsgStream.h:123
MSG::Level m_currLevel
Current debug level.
Definition MsgStream.h:51
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition MsgStream.cpp:29
MsgStream & operator<<(long long arg)
Definition MsgStream.h:144
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition MsgStream.h:110
MsgStream & flush()
Definition MsgStream.h:118
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition MsgStream.h:101
std::streamsize precision() const
Definition MsgStream.h:161
bool m_active
Flag set to true if formatting engine is active.
Definition MsgStream.h:47
char fill(char v)
Definition MsgStream.h:168
std::ostringstream & stream()
Access string MsgStream.
Definition MsgStream.h:99
bool isActive() const
Accessor: is MsgStream active.
Definition MsgStream.h:116
virtual GAUDI_API ~MsgStream()=default
Standard destructor.
MsgStream & report(int lvl)
Initialize report of new message: activate if print level is sufficient.
Definition MsgStream.h:80
void setLevel(int level)
Update outputlevel.
Definition MsgStream.h:103
long flags() const
Definition MsgStream.h:164
bool bad() const
Definition MsgStream.h:172
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition MsgStream.h:34
void unsetf(FLAG_TYPE _l)
Definition MsgStream.h:174
int rdstate() const
Definition MsgStream.h:169
std::streamsize width() const
IOS emulation.
Definition MsgStream.h:159
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition MsgStream.h:128
int setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition MsgStream.h:173
bool eof() const
Definition MsgStream.h:171
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition MsgStream.cpp:64
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition MsgStream.h:39
const std::string & buffer() const
Access string buffer.
Definition MsgStream.h:97
char fill() const
Definition MsgStream.h:167
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition MsgStream.h:153
long setf(FLAG_TYPE v)
Definition MsgStream.h:166
Print levels enumeration.
Definition IMessageSvc.h:21
MsgStream & dec(MsgStream &log)
Definition MsgStream.h:254
@ NIL
Definition IMessageSvc.h:22
@ NUM_LEVELS
Definition IMessageSvc.h:22
@ ALWAYS
Definition IMessageSvc.h:22
MsgStream & hex(MsgStream &log)
Definition MsgStream.h:258