The Gaudi Framework  master (37c0b60a)
MsgStream.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 
33 class MsgStream {
34 private:
38  typedef std::ios_base::fmtflags FLAG_TYPE;
39  typedef std::ios_base::iostate STATE_TYPE;
40 
41 protected:
51  bool m_active = false;
62  static bool m_countInactive;
63 
64 public:
66  GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
68  GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
72  , m_active( msg.m_active )
73  , m_level( msg.m_level )
77  try { // ignore exception if we cannot copy the string
78  m_source = msg.m_source;
79  } catch ( ... ) {}
80  }
82  GAUDI_API virtual ~MsgStream() = default;
84  MsgStream& report( int lvl ) {
85  lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
86  if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
87  activate();
88  } else {
89  deactivate();
90 #ifndef NDEBUG
93  }
94 #endif
95  }
96  return *this;
97  }
99  virtual GAUDI_API MsgStream& doOutput();
101  const std::string& buffer() const { return m_buffer; }
105  void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
107  void setLevel( int level ) {
109  m_level = MSG::Level( level );
110  }
112  MSG::Level level() const { return m_level; }
114  MSG::Level currentLevel() const { return m_currLevel; }
116  void activate() { m_active = true; }
118  void deactivate() { m_active = false; }
120  bool isActive() const { return m_active; }
121  // oMsgStream flush emulation
123  if ( isActive() ) m_stream.flush();
124  return *this;
125  }
126  // oMsgStream write emulation
127  MsgStream& write( const char* buff, int len ) {
128  if ( isActive() ) m_stream.write( buff, len );
129  return *this;
130  }
133  if ( isActive() ) _f( *this );
134  return *this;
135  }
138  if ( isActive() ) _f( m_stream );
139  return *this;
140  }
142  MsgStream& operator<<( std::ios& ( *_f )(std::ios&)) {
143  if ( isActive() ) _f( m_stream );
144  return *this;
145  }
148  MsgStream& operator<<( long long arg ) {
149  try {
150  // this may throw, and we cannot afford it if the stream is used in a catch block
151  if ( isActive() ) { m_stream << arg; }
152  } catch ( ... ) {}
153  return *this;
154  }
155 
158  if ( isActive() ) _f( m_stream );
159  return *this;
160  }
161 
163  std::streamsize width() const { return isActive() ? m_stream.width() : 0; }
165  std::streamsize precision() const { return isActive() ? m_stream.precision() : 0; }
166  std::streamsize precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
167 
168  long flags() const { return isActive() ? m_stream.flags() : 0; }
169  long flags( FLAG_TYPE v ) { return isActive() ? m_stream.flags( v ) : 0; }
170  long setf( FLAG_TYPE v ) { return isActive() ? m_stream.setf( v ) : 0; }
171  char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
172  char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
173  int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
174  bool good() const { return isActive() && m_stream.good(); }
175  bool eof() const { return isActive() && m_stream.eof(); }
176  bool bad() const { return isActive() && m_stream.bad(); }
177  int setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
178  void unsetf( FLAG_TYPE _l ) {
179  if ( isActive() ) m_stream.unsetf( _l );
180  }
181  void clear( STATE_TYPE _i = std::ios_base::failbit ) {
182  if ( isActive() ) m_stream.clear( _i );
183  }
184 
186  GAUDI_API void setColor( MSG::Color col );
188  GAUDI_API void setColor( MSG::Color fg, MSG::Color bg );
189 
191  GAUDI_API void resetColor();
192 
195  static GAUDI_API bool enableCountInactive( bool value = true );
196 
198  static GAUDI_API bool countInactive();
199 };
200 
202 inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
203 
205 GAUDI_API std::string format( const char*, ... );
206 
207 #ifdef _WIN32
208 template <class _E>
209 inline MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj ) {
210 # if _MSC_VER > 1300
211  if ( s.isActive() ) s.stream().fill( obj._Fill );
212 # else
213  if ( s.isActive() ) s.stream().fill( obj._Ch );
214 # endif
215  return s;
216 }
217 template <class _Tm>
218 inline MsgStream& operator<<( MsgStream& s, const std::_Smanip<_Tm>& manip ) {
219 # if _MSC_VER > 1300
220  if ( s.isActive() ) ( *manip._Pfun )( s.stream(), manip._Manarg );
221 # else
222  if ( s.isActive() ) ( *manip._Pf )( s.stream(), manip._Manarg );
223 # endif
224  return s;
225 }
226 #elif defined( __GNUC__ )
227 # ifndef __APPLE__
228 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
229  try {
230  // this may throw, and we cannot afford it if the stream is used in a catch block
231  if ( s.isActive() ) s.stream() << manip;
232  } catch ( ... ) {}
233  return s;
234 }
235 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
236  try {
237  // this may throw, and we cannot afford it if the stream is used in a catch block
238  if ( s.isActive() ) s.stream() << manip;
239  } catch ( ... ) {}
240  return s;
241 }
242 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
243  try {
244  // this may throw, and we cannot afford it if the stream is used in a catch block
245  if ( s.isActive() ) s.stream() << manip;
246  } catch ( ... ) {}
247  return s;
248 }
249 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
250  try {
251  // this may throw, and we cannot afford it if the stream is used in a catch block
252  if ( s.isActive() ) s.stream() << manip;
253  } catch ( ... ) {}
254  return s;
255 }
256 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip ) {
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  return s;
262 }
263 # endif // not __APPLE__
264 #else // GCC, version << 3
265 template <class _Tm>
267 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
268  try {
269  // this may throw, and we cannot afford it if the stream is used in a catch block
270  if ( s.isActive() ) s.stream() << manip;
271  } catch ( ... ) {}
272  return s;
273 }
274 #endif // WIN32 or (__GNUC__)
275 
276 namespace MSG {
277  inline MsgStream& dec( MsgStream& log ) {
278  log << std::dec;
279  return log;
280  }
281  inline MsgStream& hex( MsgStream& log ) {
282  log << std::hex;
283  return log;
284  }
285 } // namespace MSG
286 
289 inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
290  try {
291  // this may throw, and we cannot afford it if the stream is used in a catch block
292  if ( s.isActive() ) s.stream() << arg;
293  } catch ( ... ) {}
294  return s;
295 }
296 
298 template <typename T>
299 MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
300  using namespace GaudiUtils;
301  if ( lhs.isActive() ) try {
302  // this may throw, and we cannot afford it if the stream is used in a catch block
303  lhs.stream() << arg;
304  } catch ( ... ) {}
305  return lhs;
306 }
307 
308 #if defined( __GNUC__ ) and not defined( __APPLE__ )
309 template <typename T>
311 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
312  if ( lhs.isActive() ) try {
313  // this may throw, and we cannot afford it if the stream is used in a catch block
314  lhs.stream() << manip;
315  } catch ( ... ) {}
316  return lhs;
317 }
318 #endif
319 
320 #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:281
std::string
STL class.
MsgStream::operator<<
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:148
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:28
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:43
MSG::Color
Color
Definition: IMessageSvc.h:26
MsgStream::flags
long flags() const
Definition: MsgStream.h:168
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:57
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:114
gaudirun.s
string s
Definition: gaudirun.py:346
IInactiveMessageCounter
Definition: IMessageSvc.h:158
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:101
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:84
MsgStream::write
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:127
std::ostringstream::rdstate
T rdstate(T... args)
MsgStream::setf
int setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:177
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:39
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:53
MsgStream::operator<<
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:157
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:163
std::dec
T dec(T... args)
MsgStream::bad
bool bad() const
Definition: MsgStream.h:176
std::streamsize
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:122
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:49
std::ostream
STL class.
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:132
std::ostringstream::bad
T bad(T... args)
MSG::dec
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:277
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:173
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:47
operator<<
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:267
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:45
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:118
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:181
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:51
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:147
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::ostringstream::flags
T flags(T... args)
MsgStream::good
bool good() const
Definition: MsgStream.h:174
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:142
MsgStream
Definition: MsgStream.h:33
std::ostringstream::good
T good(T... args)
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:55
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:105
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:175
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
MsgStream::width
std::streamsize width(std::streamsize v)
Definition: MsgStream.h:164
MsgStream::precision
std::streamsize precision(int v)
Definition: MsgStream.h:166
MSG::NIL
@ NIL
Definition: IMessageSvc.h:25
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:172
Properties.v
v
Definition: Properties.py:122
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:169
SerializeSTL.h
MsgStream::fill
char fill() const
Definition: MsgStream.h:171
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:137
GaudiUtils
Definition: Allocator.h:72
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:70
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:107
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:60
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:116
MsgStream::unsetf
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:178
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:120
MsgStream::FLAG_TYPE
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:38
std::ios_base
STL class.
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:112
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
MsgStream::setf
long setf(FLAG_TYPE v)
Definition: MsgStream.h:170
MsgStream::stream
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:103
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:62
MsgStream::precision
std::streamsize precision() const
Definition: MsgStream.h:165
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:47