Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 private:
29  typedef std::ios_base::fmtflags FLAG_TYPE;
30  typedef std::ios_base::iostate STATE_TYPE;
31 
32 protected:
42  bool m_active = false;
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 );
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; }
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
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  }
124  if ( isActive() ) _f( *this );
125  return *this;
126  }
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 
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
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:94
T setf(T...args)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:92
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
int width(int v)
Definition: MsgStream.h:158
bool m_useColors
use colors
Definition: MsgStream.h:48
char fill() const
Definition: MsgStream.h:159
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:99
int rdstate() const
Definition: MsgStream.h:163
int precision() const
Definition: MsgStream.h:161
int precision(int v)
Definition: MsgStream.h:162
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:53
T good(T...args)
int eof() const
Definition: MsgStream.h:165
int bad() const
Definition: MsgStream.h:166
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:267
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:139
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:111
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:34
T precision(T...args)
T rdstate(T...args)
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:118
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:75
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:271
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:57
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:38
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:44
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:74
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:30
int good() const
Definition: MsgStream.h:164
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:123
T flags(T...args)
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:148
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:167
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:133
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:105
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:171
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:109
MsgStream & flush()
Definition: MsgStream.h:113
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:40
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:138
T width(T...args)
char fill(char v)
Definition: MsgStream.h:160
T clear(T...args)
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:96
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:42
T flush(T...args)
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:36
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:46
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:39
long flags() const
IOS emulation.
Definition: MsgStream.h:154
T write(T...args)
Print levels enumeration.
Definition: IMessageSvc.h:14
int width() const
Definition: MsgStream.h:157
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
string s
Definition: gaudirun.py:312
T dec(T...args)
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:128
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
T unsetf(T...args)
T fill(T...args)
void activate()
Activate MsgStream.
Definition: MsgStream.h:107
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:61
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:51
long flags(FLAG_TYPE v)
Definition: MsgStream.h:155
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:29
long setf(FLAG_TYPE v)
Definition: MsgStream.h:156
#define GAUDI_API
Definition: Kernel.h:71
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:192
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:37
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:168