| The Gaudi Framework v29r0 (ff2e7097)

- 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 <iomanip>
10 #include <sstream>
11 #include <string>
12 #include <vector>
13
24 class MsgStream
25 {
26 private:
30 typedef std::ios_base::fmtflags FLAG_TYPE;
31 typedef std::ios_base::iostate STATE_TYPE;
32
33 protected:
35 IMessageSvc* m_service;
37 std::string m_buffer;
39 std::string m_source;
41 std::ostringstream m_stream;
43 bool m_active = false;
45 MSG::Level m_level;
47 MSG::Level m_currLevel;
49 bool m_useColors;
52 IInactiveMessageCounter* m_inactCounter = nullptr;
54 static bool m_countInactive;
55
56 public:
58 GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
60 GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
62 MsgStream( const MsgStream& msg )
63 : m_service( msg.m_service )
64 , m_active( msg.m_active )
65 , m_level( msg.m_level )
66 , m_currLevel( msg.m_currLevel )
67 , m_useColors( msg.m_useColors )
68 , m_inactCounter( msg.m_inactCounter )
69 {
70 try { // ignore exception if we cannot copy the string
71 m_source = msg.m_source;
72 } catch ( ... ) {
73 }
74 }
76 GAUDI_API virtual ~MsgStream() = default;
78 MsgStream& report( int lvl )
79 {
80 lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
81 if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
82 activate();
83 } else {
84 deactivate();
85 #ifndef NDEBUG
86 if ( MsgStream::countInactive() && m_inactCounter ) {
87 m_inactCounter->incrInactiveCount( MSG::Level( lvl ), m_source );
88 }
89 #endif
90 }
91 return *this;
92 }
94 virtual GAUDI_API MsgStream& doOutput();
96 const std::string& buffer() const { return m_buffer; }
98 std::ostringstream& stream() { return m_stream; }
100 void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
102 void setLevel( int level )
103 {
104 level = ( level >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( level < MSG::NIL ) ? MSG::NIL : level;
105 m_level = MSG::Level( level );
106 }
108 MSG::Level level() const { return m_level; }
110 MSG::Level currentLevel() const { return m_currLevel; }
112 void activate() { m_active = true; }
114 void deactivate() { m_active = false; }
116 bool isActive() const { return m_active; }
117 // oMsgStream flush emulation
118 MsgStream& flush()
119 {
120 if ( isActive() ) m_stream.flush();
121 return *this;
122 }
123 // oMsgStream write emulation
124 MsgStream& write( const char* buff, int len )
125 {
126 if ( isActive() ) m_stream.write( buff, len );
127 return *this;
128 }
130 MsgStream& operator<<( MsgStream& ( *_f )(MsgStream&))
131 {
132 if ( isActive() ) _f( *this );
133 return *this;
134 }
136 MsgStream& operator<<( std::ostream& ( *_f )(std::ostream&))
137 {
138 if ( isActive() ) _f( m_stream );
139 return *this;
140 }
142 MsgStream& operator<<( std::ios& ( *_f )(std::ios&))
143 {
144 if ( isActive() ) _f( m_stream );
145 return *this;
146 }
148 MsgStream& operator<<( MSG::Level level ) { return report( level ); }
149 MsgStream& operator<<( longlong arg )
150 {
151 try {
152 // this may throw, and we cannot afford it if the stream is used in a catch block
153 if ( isActive() ) {
154 m_stream << arg;
155 }
156 } catch ( ... ) {
157 }
158 return *this;
159 }
160
162 MsgStream& operator<<( std::ios_base& ( *_f )(std::ios_base&))
163 {
164 if ( isActive() ) _f( m_stream );
165 return *this;
166 }
167
169 long flags() const { return isActive() ? m_stream.flags() : 0; }
170 long flags( FLAG_TYPE v ) { return isActive() ? m_stream.flags( v ) : 0; }
171 long setf( FLAG_TYPE v ) { return isActive() ? m_stream.setf( v ) : 0; }
172 int width() const { return isActive() ? m_stream.width() : 0; }
173 int width( int v ) { return isActive() ? m_stream.width( v ) : 0; }
174 char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
175 char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
176 int precision() const { return isActive() ? m_stream.precision() : 0; }
177 int precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
178 int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
179 int good() const { return isActive() ? m_stream.good() : 0; }
180 int eof() const { return isActive() ? m_stream.eof() : 0; }
181 int bad() const { return isActive() ? m_stream.bad() : 0; }
182 long setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
183 void unsetf( FLAG_TYPE _l )
184 {
185 if ( isActive() ) m_stream.unsetf( _l );
186 }
187 void clear( STATE_TYPE _i = std::ios_base::failbit )
188 {
189 if ( isActive() ) m_stream.clear( _i );
190 }
191
193 GAUDI_API void setColor( MSG::Color col );
195 GAUDI_API void setColor( MSG::Color fg, MSG::Color bg );
196
198 GAUDI_API void resetColor();
199
202 static GAUDI_API bool enableCountInactive( bool value = true );
203
205 static GAUDI_API bool countInactive();
206 };
207
209 inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
210
212 GAUDI_API std::string format( const char*, ... );
213
214 #ifdef _WIN32
215 template <class _E>
216 inline MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj )
217 {
218 #if _MSC_VER > 1300
219 if ( s.isActive() ) s.stream().fill( obj._Fill );
220 #else
221 if ( s.isActive() ) s.stream().fill( obj._Ch );
222 #endif
223 return s;
224 }
225 template <class _Tm>
226 inline MsgStream& operator<<( MsgStream& s, const std::_Smanip<_Tm>& manip )
227 {
228 #if _MSC_VER > 1300
229 if ( s.isActive() ) ( *manip._Pfun )( s.stream(), manip._Manarg );
230 #else
231 if ( s.isActive() ) ( *manip._Pf )( s.stream(), manip._Manarg );
232 #endif
233 return s;
234 }
235 #elif defined( __GNUC__ )
236 #ifndef __APPLE__
237 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip )
238 {
239 try {
240 // this may throw, and we cannot afford it if the stream is used in a catch block
241 if ( s.isActive() ) s.stream() << manip;
242 } catch ( ... ) {
243 }
244 return s;
245 }
246 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip )
247 {
248 try {
249 // this may throw, and we cannot afford it if the stream is used in a catch block
250 if ( s.isActive() ) s.stream() << manip;
251 } catch ( ... ) {
252 }
253 return s;
254 }
255 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip )
256 {
257 try {
258 // this may throw, and we cannot afford it if the stream is used in a catch block
259 if ( s.isActive() ) s.stream() << manip;
260 } catch ( ... ) {
261 }
262 return s;
263 }
264 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip )
265 {
266 try {
267 // this may throw, and we cannot afford it if the stream is used in a catch block
268 if ( s.isActive() ) s.stream() << manip;
269 } catch ( ... ) {
270 }
271 return s;
272 }
273 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip )
274 {
275 try {
276 // this may throw, and we cannot afford it if the stream is used in a catch block
277 if ( s.isActive() ) s.stream() << manip;
278 } catch ( ... ) {
279 }
280 return s;
281 }
282 #endif // not __APPLE__
283 #else // GCC, version << 3
284 template <class _Tm>
286 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip )
287 {
288 try {
289 // this may throw, and we cannot afford it if the stream is used in a catch block
290 if ( s.isActive() ) s.stream() << manip;
291 } catch ( ... ) {
292 }
293 return s;
294 }
295 #endif // WIN32 or (__GNUC__)
296
297 namespace MSG
298 {
299 inline MsgStream& dec( MsgStream& log )
300 {
301 log << std::dec;
302 return log;
303 }
304 inline MsgStream& hex( MsgStream& log )
305 {
306 log << std::hex;
307 return log;
308 }
309 }
310
313 inline MsgStream& operator<<( MsgStream& s, const char* arg )
314 {
315 try {
316 // this may throw, and we cannot afford it if the stream is used in a catch block
317 if ( s.isActive() ) s.stream() << arg;
318 } catch ( ... ) {
319 }
320 return s;
321 }
322
324 template <typename T>
325 MsgStream& operator<<( MsgStream& lhs, const T& arg )
326 {
327 using namespace GaudiUtils;
328 if ( lhs.isActive() ) try {
329 // this may throw, and we cannot afford it if the stream is used in a catch block
330 lhs.stream() << arg;
331 } catch ( ... ) {
332 }
333 return lhs;
334 }
335
336 #if defined( __GNUC__ ) and not defined( __APPLE__ )
337 template <typename T>
339 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip )
340 {
341 if ( lhs.isActive() ) try {
342 // this may throw, and we cannot afford it if the stream is used in a catch block
343 lhs.stream() << manip;
344 } catch ( ... ) {
345 }
346 return lhs;
347 }
348 #endif
349
350 #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:149
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:96
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
MsgStream::width
int width(int v)
Definition: MsgStream.h:173
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:49
MsgStream::fill
char fill() const
Definition: MsgStream.h:174
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:108
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:178
MsgStream::precision
int precision() const
Definition: MsgStream.h:176
MsgStream::precision
int precision(int v)
Definition: MsgStream.h:177
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:54
std::ostringstream::good
T good(T...args)
MsgStream::eof
int eof() const
Definition: MsgStream.h:180
MsgStream::bad
int bad() const
Definition: MsgStream.h:181
MSG::dec
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:299
IInactiveMessageCounter
Definition: IMessageSvc.h:158
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::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:124
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:78
MSG::hex
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:304
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
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:45
std::ostringstream::eof
T eof(T...args)
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:79
MsgStream::STATE_TYPE
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:31
MsgStream::good
int good() const
Definition: MsgStream.h:179
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:130
MSG::Level
Level
Definition: IMessageSvc.h:16
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:162
MsgStream::setf
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:182
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:142
__longlong
Definition: Kernel.h:56
MsgStream::currentLevel
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:110
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:187
std::ostringstream
STL class.
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:114
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:118
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
MsgStream::operator<<
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:148
std::ostringstream::width
T width(T...args)
MSG::NIL
Definition: IMessageSvc.h:16
IMessageSvc.h
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:175
std::ostringstream::clear
T clear(T...args)
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:100
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:43
std::ostringstream::flush
T flush(T...args)
MsgStream::m_buffer
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:37
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:47
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:40
MsgStream::flags
long flags() const
IOS emulation.
Definition: MsgStream.h:169
std::ostringstream::write
T write(T...args)
MSG::NUM_LEVELS
Definition: IMessageSvc.h:16
MSG
Print levels enumeration.
Definition: IMessageSvc.h:14
MsgStream::width
int width() const
Definition: MsgStream.h:172
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:140
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
gaudirun.s
string s
Definition: gaudirun.py:253
std::dec
T dec(T...args)
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:136
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
std::ostringstream::unsetf
T unsetf(T...args)
std::ostringstream::fill
T fill(T...args)
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:112
MsgStream::MsgStream
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:62
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:52
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:170
MsgStream::FLAG_TYPE
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:30
MsgStream::setf
long setf(FLAG_TYPE v)
Definition: MsgStream.h:171
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
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:31
MSG::ALWAYS
Definition: IMessageSvc.h:16
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
MSG::Color
Color
Definition: IMessageSvc.h:17
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:183
Gaudi.Configuration.log
log
Definition: Configuration.py:13