| The Gaudi Framework v32r1 (f65d50dc)

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 private:
29 typedef std::ios_base::fmtflags FLAG_TYPE;
30 typedef std::ios_base::iostate STATE_TYPE;
31
32 protected:
34 IMessageSvc* m_service;
36 std::string m_buffer;
38 std::string m_source;
40 std::ostringstream m_stream;
42 bool m_active = false;
44 MSG::Level m_level;
46 MSG::Level m_currLevel;
48 bool m_useColors;
51 IInactiveMessageCounter* m_inactCounter = nullptr;
53 static bool m_countInactive;
54
55 public:
57 GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
59 GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
61 MsgStream( const MsgStream& msg )
62 : m_service( msg.m_service )
63 , m_active( msg.m_active )
64 , m_level( msg.m_level )
65 , m_currLevel( msg.m_currLevel )
66 , m_useColors( msg.m_useColors )
67 , m_inactCounter( msg.m_inactCounter ) {
68 try { // ignore exception if we cannot copy the string
69 m_source = msg.m_source;
70 } catch ( ... ) {}
71 }
73 GAUDI_API virtual ~MsgStream() = default;
75 MsgStream& report( int lvl ) {
76 lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
77 if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
78 activate();
79 } else {
80 deactivate();
81 #ifndef NDEBUG
82 if ( MsgStream::countInactive() && m_inactCounter ) {
83 m_inactCounter->incrInactiveCount( MSG::Level( lvl ), m_source );
84 }
85 #endif
86 }
87 return *this;
88 }
90 virtual GAUDI_API MsgStream& doOutput();
92 const std::string& buffer() const { return m_buffer; }
94 std::ostringstream& stream() { return m_stream; }
96 void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
98 void setLevel( int level ) {
99 level = ( level >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( level < MSG::NIL ) ? MSG::NIL : level;
100 m_level = MSG::Level( level );
101 }
103 MSG::Level level() const { return m_level; }
105 MSG::Level currentLevel() const { return m_currLevel; }
107 void activate() { m_active = true; }
109 void deactivate() { m_active = false; }
111 bool isActive() const { return m_active; }
112 // oMsgStream flush emulation
113 MsgStream& flush() {
114 if ( isActive() ) m_stream.flush();
115 return *this;
116 }
117 // oMsgStream write emulation
118 MsgStream& write( const char* buff, int len ) {
119 if ( isActive() ) m_stream.write( buff, len );
120 return *this;
121 }
123 MsgStream& operator<<( MsgStream& ( *_f )(MsgStream&)) {
124 if ( isActive() ) _f( *this );
125 return *this;
126 }
128 MsgStream& operator<<( std::ostream& ( *_f )(std::ostream&)) {
129 if ( isActive() ) _f( m_stream );
130 return *this;
131 }
133 MsgStream& operator<<( std::ios& ( *_f )(std::ios&)) {
134 if ( isActive() ) _f( m_stream );
135 return *this;
136 }
138 MsgStream& operator<<( MSG::Level level ) { return report( level ); }
139 MsgStream& operator<<( long long arg ) {
140 try {
141 // this may throw, and we cannot afford it if the stream is used in a catch block
142 if ( isActive() ) { m_stream << arg; }
143 } catch ( ... ) {}
144 return *this;
145 }
146
148 MsgStream& operator<<( std::ios_base& ( *_f )(std::ios_base&)) {
149 if ( isActive() ) _f( m_stream );
150 return *this;
151 }
152
154 long flags() const { return isActive() ? m_stream.flags() : 0; }
155 long flags( FLAG_TYPE v ) { return isActive() ? m_stream.flags( v ) : 0; }
156 long setf( FLAG_TYPE v ) { return isActive() ? m_stream.setf( v ) : 0; }
157 int width() const { return isActive() ? m_stream.width() : 0; }
158 int width( int v ) { return isActive() ? m_stream.width( v ) : 0; }
159 char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
160 char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
161 int precision() const { return isActive() ? m_stream.precision() : 0; }
162 int precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
163 int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
164 int good() const { return isActive() ? m_stream.good() : 0; }
165 int eof() const { return isActive() ? m_stream.eof() : 0; }
166 int bad() const { return isActive() ? m_stream.bad() : 0; }
167 long setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
168 void unsetf( FLAG_TYPE _l ) {
169 if ( isActive() ) m_stream.unsetf( _l );
170 }
171 void clear( STATE_TYPE _i = std::ios_base::failbit ) {
172 if ( isActive() ) m_stream.clear( _i );
173 }
174
176 GAUDI_API void setColor( MSG::Color col );
178 GAUDI_API void setColor( MSG::Color fg, MSG::Color bg );
179
181 GAUDI_API void resetColor();
182
185 static GAUDI_API bool enableCountInactive( bool value = true );
186
188 static GAUDI_API bool countInactive();
189 };
190
192 inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
193
195 GAUDI_API std::string format( const char*, ... );
196
197 #ifdef _WIN32
198 template <class _E>
199 inline MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj ) {
200 # if _MSC_VER > 1300
201 if ( s.isActive() ) s.stream().fill( obj._Fill );
202 # else
203 if ( s.isActive() ) s.stream().fill( obj._Ch );
204 # endif
205 return s;
206 }
207 template <class _Tm>
208 inline MsgStream& operator<<( MsgStream& s, const std::_Smanip<_Tm>& manip ) {
209 # if _MSC_VER > 1300
210 if ( s.isActive() ) ( *manip._Pfun )( s.stream(), manip._Manarg );
211 # else
212 if ( s.isActive() ) ( *manip._Pf )( s.stream(), manip._Manarg );
213 # endif
214 return s;
215 }
216 #elif defined( __GNUC__ )
217 # ifndef __APPLE__
218 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
219 try {
220 // this may throw, and we cannot afford it if the stream is used in a catch block
221 if ( s.isActive() ) s.stream() << manip;
222 } catch ( ... ) {}
223 return s;
224 }
225 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
226 try {
227 // this may throw, and we cannot afford it if the stream is used in a catch block
228 if ( s.isActive() ) s.stream() << manip;
229 } catch ( ... ) {}
230 return s;
231 }
232 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
233 try {
234 // this may throw, and we cannot afford it if the stream is used in a catch block
235 if ( s.isActive() ) s.stream() << manip;
236 } catch ( ... ) {}
237 return s;
238 }
239 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
240 try {
241 // this may throw, and we cannot afford it if the stream is used in a catch block
242 if ( s.isActive() ) s.stream() << manip;
243 } catch ( ... ) {}
244 return s;
245 }
246 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip ) {
247 try {
248 // this may throw, and we cannot afford it if the stream is used in a catch block
249 if ( s.isActive() ) s.stream() << manip;
250 } catch ( ... ) {}
251 return s;
252 }
253 # endif // not __APPLE__
254 #else // GCC, version << 3
255 template <class _Tm>
257 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
258 try {
259 // this may throw, and we cannot afford it if the stream is used in a catch block
260 if ( s.isActive() ) s.stream() << manip;
261 } catch ( ... ) {}
262 return s;
263 }
264 #endif // WIN32 or (__GNUC__)
265
266 namespace MSG {
267 inline MsgStream& dec( MsgStream& log ) {
268 log << std::dec;
269 return log;
270 }
271 inline MsgStream& hex( MsgStream& log ) {
272 log << std::hex;
273 return log;
274 }
275 } // namespace MSG
276
279 inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
280 try {
281 // this may throw, and we cannot afford it if the stream is used in a catch block
282 if ( s.isActive() ) s.stream() << arg;
283 } catch ( ... ) {}
284 return s;
285 }
286
288 template <typename T>
289 MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
290 using namespace GaudiUtils;
291 if ( lhs.isActive() ) try {
292 // this may throw, and we cannot afford it if the stream is used in a catch block
293 lhs.stream() << arg;
294 } catch ( ... ) {}
295 return lhs;
296 }
297
298 #if defined( __GNUC__ ) and not defined( __APPLE__ )
299 template <typename T>
301 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
302 if ( lhs.isActive() ) try {
303 // this may throw, and we cannot afford it if the stream is used in a catch block
304 lhs.stream() << manip;
305 } catch ( ... ) {}
306 return lhs;
307 }
308 #endif
309
310 #endif // GAUDIKERNEL_MSGSTREAM_H
MsgStream::stream
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:94
MsgStream::eof
int eof() const
Definition: MsgStream.h:165
std::ostringstream::setf
T setf(T... args)
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:111
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
MsgStream::width
int width(int v)
Definition: MsgStream.h:158
MsgStream::currentLevel
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:105
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:48
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:99
MsgStream::precision
int precision(int v)
Definition: MsgStream.h:162
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:53
std::ostringstream::good
T good(T... args)
MsgStream::bad
int bad() const
Definition: MsgStream.h:166
MSG::dec
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:267
IInactiveMessageCounter
Definition: IMessageSvc.h:156
MsgStream::operator<<
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:139
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:34
std::ostringstream::precision
T precision(T... args)
MsgStream::flags
long flags() const
IOS emulation.
Definition: MsgStream.h:154
std::ostringstream::rdstate
T rdstate(T... args)
MsgStream::write
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:118
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:75
MSG::hex
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:271
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:57
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:92
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:38
SerializeSTL.h
Provide serialization function (output only) for some common STL classes (vectors,...
std::string
STL class.
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:44
operator<<
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:257
std::ostringstream::eof
T eof(T... args)
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:74
MsgStream::STATE_TYPE
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:30
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:123
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:148
MsgStream::setf
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:167
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:133
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:171
std::ostringstream
STL class.
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:109
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:113
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:40
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
MsgStream::operator<<
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:138
std::ostringstream::width
T width(T... args)
MSG::NIL
Definition: IMessageSvc.h:15
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:163
IMessageSvc.h
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:160
std::ostringstream::clear
T clear(T... args)
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:96
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:42
std::ostringstream::flush
T flush(T... args)
MsgStream::m_buffer
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:36
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:46
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:39
std::ostringstream::write
T write(T... args)
MSG::NUM_LEVELS
Definition: IMessageSvc.h:15
MSG
Print levels enumeration.
Definition: IMessageSvc.h:14
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
gaudirun.s
string s
Definition: gaudirun.py:316
MsgStream::fill
char fill() const
Definition: MsgStream.h:159
std::dec
T dec(T... args)
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:128
MsgStream::width
int width() const
Definition: MsgStream.h:157
std::ostringstream::unsetf
T unsetf(T... args)
MsgStream::precision
int precision() const
Definition: MsgStream.h:161
std::ostringstream::fill
T fill(T... args)
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:107
MsgStream::MsgStream
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:61
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:51
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:155
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:156
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
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:15
MsgStream::good
int good() const
Definition: MsgStream.h:164
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
MSG::Color
Color
Definition: IMessageSvc.h:16
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:37
MsgStream::unsetf
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:168
Gaudi.Configuration.log
log
Definition: Configuration.py:13