The Gaudi Framework  v30r3 (a5ef0a68)
MsgStream.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_MSGSTREAM_H
2 #define GAUDIKERNEL_MSGSTREAM_H
3 
4 // Include files
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:
43  bool m_active = false;
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 );
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; }
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
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  }
131  {
132  if ( isActive() ) _f( *this );
133  return *this;
134  }
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 ); }
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 
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 {
300  {
301  log << std::dec;
302  return log;
303  }
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
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:98
T setf(T...args)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & operator<<(longlong arg)
Definition: MsgStream.h:149
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:96
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
int width(int v)
Definition: MsgStream.h:173
bool m_useColors
use colors
Definition: MsgStream.h:49
char fill() const
Definition: MsgStream.h:174
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:108
int rdstate() const
Definition: MsgStream.h:178
int precision() const
Definition: MsgStream.h:176
int precision(int v)
Definition: MsgStream.h:177
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:54
T good(T...args)
int eof() const
Definition: MsgStream.h:180
int bad() const
Definition: MsgStream.h:181
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:299
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:116
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:35
T precision(T...args)
T rdstate(T...args)
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:124
virtual void incrInactiveCount(MSG::Level level, const std::string &src)=0
Increment deactivated message count.
MsgStream & report(int lvl)
Initialize report of new message: activate if print level is sufficient.
Definition: MsgStream.h:78
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:304
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:60
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:39
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
STL class.
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:45
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:79
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:31
int good() const
Definition: MsgStream.h:179
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:130
T flags(T...args)
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:162
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:182
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:142
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:110
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:187
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:114
MsgStream & flush()
Definition: MsgStream.h:118
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:41
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:148
T width(T...args)
char fill(char v)
Definition: MsgStream.h:175
T clear(T...args)
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:100
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:43
T flush(T...args)
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:37
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:47
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:40
long flags() const
IOS emulation.
Definition: MsgStream.h:169
T write(T...args)
Print levels enumeration.
Definition: IMessageSvc.h:14
int width() const
Definition: MsgStream.h:172
Forward declarations for the functions in SerializeSTL.h.
Definition: __init__.py:1
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
string s
Definition: gaudirun.py:253
T dec(T...args)
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:136
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
T unsetf(T...args)
T fill(T...args)
void activate()
Activate MsgStream.
Definition: MsgStream.h:112
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:62
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:52
long flags(FLAG_TYPE v)
Definition: MsgStream.h:170
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:30
long setf(FLAG_TYPE v)
Definition: MsgStream.h:171
#define GAUDI_API
Definition: Kernel.h:104
STL class.
STL class.
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:31
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
virtual GAUDI_API ~MsgStream()=default
Standard destructor.
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:38
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:183