Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MsgStream.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2021 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 #ifndef GAUDIKERNEL_MSGSTREAM_H
12 #define GAUDIKERNEL_MSGSTREAM_H
13 
14 // Include files
17 // Standard C++ classes
18 #include <cstdio>
19 #include <iomanip>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
34 class MsgStream {
35 private:
39  typedef std::ios_base::fmtflags FLAG_TYPE;
40  typedef std::ios_base::iostate STATE_TYPE;
41 
42 protected:
52  bool m_active = false;
63  static bool m_countInactive;
64 
65 public:
67  GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
69  GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
73  , m_active( msg.m_active )
74  , m_level( msg.m_level )
78  try { // ignore exception if we cannot copy the string
79  m_source = msg.m_source;
80  } catch ( ... ) {}
81  }
83  GAUDI_API virtual ~MsgStream() = default;
85  MsgStream& report( int lvl ) {
86  lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
87  if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
88  activate();
89  } else {
90  deactivate();
91 #ifndef NDEBUG
94  }
95 #endif
96  }
97  return *this;
98  }
100  virtual GAUDI_API MsgStream& doOutput();
102  const std::string& buffer() const { return m_buffer; }
106  void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
108  void setLevel( int level ) {
110  m_level = MSG::Level( level );
111  }
113  MSG::Level level() const { return m_level; }
115  MSG::Level currentLevel() const { return m_currLevel; }
117  void activate() { m_active = true; }
119  void deactivate() { m_active = false; }
121  bool isActive() const { return m_active; }
122  // oMsgStream flush emulation
124  if ( isActive() ) m_stream.flush();
125  return *this;
126  }
127  // oMsgStream write emulation
128  MsgStream& write( const char* buff, int len ) {
129  if ( isActive() ) m_stream.write( buff, len );
130  return *this;
131  }
134  if ( isActive() ) _f( *this );
135  return *this;
136  }
139  if ( isActive() ) _f( m_stream );
140  return *this;
141  }
143  MsgStream& operator<<( std::ios& ( *_f )(std::ios&)) {
144  if ( isActive() ) _f( m_stream );
145  return *this;
146  }
149  MsgStream& operator<<( long long arg ) {
150  try {
151  // this may throw, and we cannot afford it if the stream is used in a catch block
152  if ( isActive() ) { m_stream << arg; }
153  } catch ( ... ) {}
154  return *this;
155  }
156 
159  if ( isActive() ) _f( m_stream );
160  return *this;
161  }
162 
164  std::streamsize width() const { return isActive() ? m_stream.width() : 0; }
166  std::streamsize precision() const { return isActive() ? m_stream.precision() : 0; }
167  std::streamsize precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
168 
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  char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
173  char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
174  int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
175  bool good() const { return isActive() && m_stream.good(); }
176  bool eof() const { return isActive() && m_stream.eof(); }
177  bool bad() const { return isActive() && m_stream.bad(); }
178  int setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
179  void unsetf( FLAG_TYPE _l ) {
180  if ( isActive() ) m_stream.unsetf( _l );
181  }
182  void clear( STATE_TYPE _i = std::ios_base::failbit ) {
183  if ( isActive() ) m_stream.clear( _i );
184  }
185 
187  GAUDI_API void setColor( MSG::Color col );
189  GAUDI_API void setColor( MSG::Color fg, MSG::Color bg );
190 
192  GAUDI_API void resetColor();
193 
196  static GAUDI_API bool enableCountInactive( bool value = true );
197 
199  static GAUDI_API bool countInactive();
200 };
201 
203 inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
204 
206 GAUDI_API std::string format( const char*, ... );
207 
208 #ifdef _WIN32
209 template <class _E>
210 inline MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj ) {
211 # if _MSC_VER > 1300
212  if ( s.isActive() ) s.stream().fill( obj._Fill );
213 # else
214  if ( s.isActive() ) s.stream().fill( obj._Ch );
215 # endif
216  return s;
217 }
218 template <class _Tm>
219 inline MsgStream& operator<<( MsgStream& s, const std::_Smanip<_Tm>& manip ) {
220 # if _MSC_VER > 1300
221  if ( s.isActive() ) ( *manip._Pfun )( s.stream(), manip._Manarg );
222 # else
223  if ( s.isActive() ) ( *manip._Pf )( s.stream(), manip._Manarg );
224 # endif
225  return s;
226 }
227 #elif defined( __GNUC__ )
228 # ifndef __APPLE__
229 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
230  try {
231  // this may throw, and we cannot afford it if the stream is used in a catch block
232  if ( s.isActive() ) s.stream() << manip;
233  } catch ( ... ) {}
234  return s;
235 }
236 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
237  try {
238  // this may throw, and we cannot afford it if the stream is used in a catch block
239  if ( s.isActive() ) s.stream() << manip;
240  } catch ( ... ) {}
241  return s;
242 }
243 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
244  try {
245  // this may throw, and we cannot afford it if the stream is used in a catch block
246  if ( s.isActive() ) s.stream() << manip;
247  } catch ( ... ) {}
248  return s;
249 }
250 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
251  try {
252  // this may throw, and we cannot afford it if the stream is used in a catch block
253  if ( s.isActive() ) s.stream() << manip;
254  } catch ( ... ) {}
255  return s;
256 }
257 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& 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 // not __APPLE__
265 #else // GCC, version << 3
266 template <class _Tm>
268 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
269  try {
270  // this may throw, and we cannot afford it if the stream is used in a catch block
271  if ( s.isActive() ) s.stream() << manip;
272  } catch ( ... ) {}
273  return s;
274 }
275 #endif // WIN32 or (__GNUC__)
276 
277 namespace MSG {
278  inline MsgStream& dec( MsgStream& log ) {
279  log << std::dec;
280  return log;
281  }
282  inline MsgStream& hex( MsgStream& log ) {
283  log << std::hex;
284  return log;
285  }
286 } // namespace MSG
287 
290 inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
291  try {
292  // this may throw, and we cannot afford it if the stream is used in a catch block
293  if ( s.isActive() ) s.stream() << arg;
294  } catch ( ... ) {}
295  return s;
296 }
297 
299 template <typename T>
300 MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
301  using namespace GaudiUtils;
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() << arg;
305  } catch ( ... ) {}
306  return lhs;
307 }
308 
309 #if defined( __GNUC__ ) and not defined( __APPLE__ )
310 template <typename T>
312 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
313  if ( lhs.isActive() ) try {
314  // this may throw, and we cannot afford it if the stream is used in a catch block
315  lhs.stream() << manip;
316  } catch ( ... ) {}
317  return lhs;
318 }
319 #endif
320 
321 #endif // GAUDIKERNEL_MSGSTREAM_H
std::ostringstream::width
T width(T... args)
MSG
Print levels enumeration.
Definition: IMessageSvc.h:24
MSG::hex
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:282
std::string
STL class.
MsgStream::operator<<
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:149
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:30
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:41
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:44
MSG::Color
Color
Definition: IMessageSvc.h:26
MsgStream::flags
long flags() const
Definition: MsgStream.h:169
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:58
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:67
MsgStream::currentLevel
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:115
gaudirun.s
string s
Definition: gaudirun.py:348
IInactiveMessageCounter
Definition: IMessageSvc.h:158
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:102
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
MsgStream::report
MsgStream & report(int lvl)
Initialize report of new message: activate if print level is sufficient.
Definition: MsgStream.h:85
MsgStream::write
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:128
std::ostringstream::rdstate
T rdstate(T... args)
MsgStream::setf
int setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:178
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:84
MsgStream::STATE_TYPE
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:40
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:54
MsgStream::operator<<
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:158
IMessageSvc.h
std::ostringstream::write
T write(T... args)
std::ostringstream::clear
T clear(T... args)
std::ostringstream::fill
T fill(T... args)
MsgStream::width
std::streamsize width() const
IOS emulation.
Definition: MsgStream.h:164
std::dec
T dec(T... args)
MsgStream::bad
bool bad() const
Definition: MsgStream.h:177
std::streamsize
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:123
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:50
std::ostream
STL class.
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:133
std::ostringstream::bad
T bad(T... args)
MSG::dec
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:278
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:174
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:48
operator<<
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:268
std::ostringstream::setf
T setf(T... args)
std::ostringstream::flush
T flush(T... args)
MsgStream::m_buffer
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:46
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:119
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:182
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:52
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
MsgStream::operator<<
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:148
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::ostringstream::flags
T flags(T... args)
MsgStream::good
bool good() const
Definition: MsgStream.h:175
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:143
MsgStream
Definition: MsgStream.h:34
std::ostringstream::good
T good(T... args)
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:56
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:106
std::ostringstream
STL class.
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:49
MSG::Level
Level
Definition: IMessageSvc.h:25
MsgStream::eof
bool eof() const
Definition: MsgStream.h:176
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
MsgStream::width
std::streamsize width(std::streamsize v)
Definition: MsgStream.h:165
MsgStream::precision
std::streamsize precision(int v)
Definition: MsgStream.h:167
MSG::NIL
@ NIL
Definition: IMessageSvc.h:25
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:173
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:170
SerializeSTL.h
MsgStream::fill
char fill() const
Definition: MsgStream.h:172
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:138
GaudiUtils
Definition: GaudiHistoID.h:145
std::ostringstream::unsetf
T unsetf(T... args)
MsgStream::~MsgStream
virtual GAUDI_API ~MsgStream()=default
Standard destructor.
MSG::NUM_LEVELS
@ NUM_LEVELS
Definition: IMessageSvc.h:25
MsgStream::MsgStream
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:71
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:108
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:61
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:117
MsgStream::unsetf
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:179
IInactiveMessageCounter::incrInactiveCount
virtual void incrInactiveCount(MSG::Level level, std::string_view src)=0
Increment deactivated message count.
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:121
MsgStream::FLAG_TYPE
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:39
std::ios_base
STL class.
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:113
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
MsgStream::setf
long setf(FLAG_TYPE v)
Definition: MsgStream.h:171
MsgStream::stream
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:104
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:109
std::ostringstream::eof
T eof(T... args)
std::ostringstream::precision
T precision(T... args)
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:63
MsgStream::precision
std::streamsize precision() const
Definition: MsgStream.h:166
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:47