The Gaudi Framework  v40r0 (475e45c1)
MsgStream.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
15 #include <iomanip>
16 #include <sstream>
17 #include <string>
18 
29 class MsgStream {
30 private:
34  typedef std::ios_base::fmtflags FLAG_TYPE;
35  typedef std::ios_base::iostate STATE_TYPE;
36 
37 protected:
41  std::string m_buffer;
43  std::string m_source;
45  std::ostringstream m_stream;
47  bool m_active = false;
58  static bool m_countInactive;
59 
60 public:
62  GAUDI_API MsgStream( IMessageSvc* svc, int buffer_length = 128 );
64  GAUDI_API MsgStream( IMessageSvc* svc, std::string source, int buffer_length = 128 );
68  , m_active( msg.m_active )
69  , m_level( msg.m_level )
73  try { // ignore exception if we cannot copy the string
74  m_source = msg.m_source;
75  } catch ( ... ) {}
76  }
78  GAUDI_API virtual ~MsgStream() = default;
80  MsgStream& report( int lvl ) {
81  lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
82  if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
83  activate();
84  } else {
85  deactivate();
86 #ifndef NDEBUG
89  }
90 #endif
91  }
92  return *this;
93  }
95  virtual GAUDI_API MsgStream& doOutput();
97  const std::string& buffer() const { return m_buffer; }
99  std::ostringstream& stream() { return m_stream; }
101  void setMsgSvc( IMessageSvc* svc ) { m_service = svc; }
103  void setLevel( int 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  if ( isActive() ) m_stream.flush();
120  return *this;
121  }
122  // oMsgStream write emulation
123  MsgStream& write( const char* buff, int len ) {
124  if ( isActive() ) m_stream.write( buff, len );
125  return *this;
126  }
129  if ( isActive() ) _f( *this );
130  return *this;
131  }
133  MsgStream& operator<<( std::ostream& ( *_f )(std::ostream&)) {
134  if ( isActive() ) _f( m_stream );
135  return *this;
136  }
138  MsgStream& operator<<( std::ios& ( *_f )(std::ios&)) {
139  if ( isActive() ) _f( m_stream );
140  return *this;
141  }
144  MsgStream& operator<<( long long arg ) {
145  try {
146  // this may throw, and we cannot afford it if the stream is used in a catch block
147  if ( isActive() ) { m_stream << arg; }
148  } catch ( ... ) {}
149  return *this;
150  }
151 
153  MsgStream& operator<<( std::ios_base& ( *_f )(std::ios_base&)) {
154  if ( isActive() ) _f( m_stream );
155  return *this;
156  }
157 
159  std::streamsize width() const { return isActive() ? m_stream.width() : 0; }
160  std::streamsize width( std::streamsize v ) { return isActive() ? m_stream.width( v ) : 0; }
161  std::streamsize precision() const { return isActive() ? m_stream.precision() : 0; }
162  std::streamsize precision( int v ) { return isActive() ? m_stream.precision( v ) : 0; }
163 
164  long flags() const { return isActive() ? m_stream.flags() : 0; }
165  long flags( FLAG_TYPE v ) { return isActive() ? m_stream.flags( v ) : 0; }
166  long setf( FLAG_TYPE v ) { return isActive() ? m_stream.setf( v ) : 0; }
167  char fill() const { return isActive() ? m_stream.fill() : (char)-1; }
168  char fill( char v ) { return isActive() ? m_stream.fill( v ) : (char)-1; }
169  int rdstate() const { return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }
170  bool good() const { return isActive() && m_stream.good(); }
171  bool eof() const { return isActive() && m_stream.eof(); }
172  bool bad() const { return isActive() && m_stream.bad(); }
173  int setf( FLAG_TYPE _f, FLAG_TYPE _m ) { return isActive() ? m_stream.setf( _f, _m ) : 0; }
174  void unsetf( FLAG_TYPE _l ) {
175  if ( isActive() ) m_stream.unsetf( _l );
176  }
177  void clear( STATE_TYPE _i = std::ios_base::failbit ) {
178  if ( isActive() ) m_stream.clear( _i );
179  }
180 
182  GAUDI_API void setColor( MSG::Color col );
184  GAUDI_API void setColor( MSG::Color fg, MSG::Color bg );
185 
187  GAUDI_API void resetColor();
188 
191  static GAUDI_API bool enableCountInactive( bool value = true );
192 
194  static GAUDI_API bool countInactive();
195 };
196 
198 inline MsgStream& endmsg( MsgStream& s ) { return s.doOutput(); }
199 
201 GAUDI_API std::string format( const char*, ... );
202 
203 #ifdef _WIN32
204 template <class _E>
205 inline MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj ) {
206 # if _MSC_VER > 1300
207  if ( s.isActive() ) s.stream().fill( obj._Fill );
208 # else
209  if ( s.isActive() ) s.stream().fill( obj._Ch );
210 # endif
211  return s;
212 }
213 template <class _Tm>
214 inline MsgStream& operator<<( MsgStream& s, const std::_Smanip<_Tm>& manip ) {
215 # if _MSC_VER > 1300
216  if ( s.isActive() ) ( *manip._Pfun )( s.stream(), manip._Manarg );
217 # else
218  if ( s.isActive() ) ( *manip._Pf )( s.stream(), manip._Manarg );
219 # endif
220  return s;
221 }
222 #elif defined( __GNUC__ )
223 # ifndef __APPLE__
224 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
225  try {
226  // this may throw, and we cannot afford it if the stream is used in a catch block
227  if ( s.isActive() ) s.stream() << manip;
228  } catch ( ... ) {}
229  return s;
230 }
231 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
232  try {
233  // this may throw, and we cannot afford it if the stream is used in a catch block
234  if ( s.isActive() ) s.stream() << manip;
235  } catch ( ... ) {}
236  return s;
237 }
238 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
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  return s;
244 }
245 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
246  try {
247  // this may throw, and we cannot afford it if the stream is used in a catch block
248  if ( s.isActive() ) s.stream() << manip;
249  } catch ( ... ) {}
250  return s;
251 }
252 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip ) {
253  try {
254  // this may throw, and we cannot afford it if the stream is used in a catch block
255  if ( s.isActive() ) s.stream() << manip;
256  } catch ( ... ) {}
257  return s;
258 }
259 # endif // not __APPLE__
260 #else // GCC, version << 3
261 template <class _Tm>
263 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
264  try {
265  // this may throw, and we cannot afford it if the stream is used in a catch block
266  if ( s.isActive() ) s.stream() << manip;
267  } catch ( ... ) {}
268  return s;
269 }
270 #endif // WIN32 or (__GNUC__)
271 
272 namespace MSG {
273  inline MsgStream& dec( MsgStream& log ) {
274  log << std::dec;
275  return log;
276  }
277  inline MsgStream& hex( MsgStream& log ) {
278  log << std::hex;
279  return log;
280  }
281 } // namespace MSG
282 
285 inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
286  try {
287  // this may throw, and we cannot afford it if the stream is used in a catch block
288  if ( s.isActive() ) s.stream() << arg;
289  } catch ( ... ) {}
290  return s;
291 }
292 
294 template <typename T>
295 MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
296  using namespace GaudiUtils;
297  if ( lhs.isActive() ) try {
298  // this may throw, and we cannot afford it if the stream is used in a catch block
299  lhs.stream() << arg;
300  } catch ( ... ) {}
301  return lhs;
302 }
303 
304 #if defined( __GNUC__ ) and not defined( __APPLE__ )
305 template <typename T>
307 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
308  if ( lhs.isActive() ) try {
309  // this may throw, and we cannot afford it if the stream is used in a catch block
310  lhs.stream() << manip;
311  } catch ( ... ) {}
312  return lhs;
313 }
314 #endif
MSG
Print levels enumeration.
Definition: IMessageSvc.h:21
MSG::hex
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:277
MsgStream::operator<<
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:144
IMessageSvc
Definition: IMessageSvc.h:44
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:21
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:39
MSG::Color
Color
Definition: IMessageSvc.h:23
MsgStream::flags
long flags() const
Definition: MsgStream.h:164
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:53
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:47
MsgStream::currentLevel
MSG::Level currentLevel() const
Retrieve current stream output level.
Definition: MsgStream.h:110
gaudirun.s
string s
Definition: gaudirun.py:346
IInactiveMessageCounter
Definition: IMessageSvc.h:155
MsgStream::buffer
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:97
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:80
MsgStream::write
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:123
MsgStream::setf
int setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:173
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:64
MsgStream::STATE_TYPE
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:35
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:49
MsgStream::operator<<
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:153
IMessageSvc.h
MsgStream::width
std::streamsize width() const
IOS emulation.
Definition: MsgStream.h:159
MsgStream::bad
bool bad() const
Definition: MsgStream.h:172
MsgStream::flush
MsgStream & flush()
Definition: MsgStream.h:118
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:45
MsgStream::operator<<
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:128
MSG::dec
MsgStream & dec(MsgStream &log)
Definition: MsgStream.h:273
MsgStream::rdstate
int rdstate() const
Definition: MsgStream.h:169
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:43
operator<<
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:263
MsgStream::m_buffer
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:41
MsgStream::deactivate
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:114
MsgStream::clear
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:177
MsgStream::m_active
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:47
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:99
MsgStream::operator<<
MsgStream & operator<<(MSG::Level level)
Accept MsgStream activation using MsgStreamer operator.
Definition: MsgStream.h:143
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
MsgStream::good
bool good() const
Definition: MsgStream.h:170
MsgStream::operator<<
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:138
MsgStream
Definition: MsgStream.h:29
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:51
MsgStream::setMsgSvc
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:101
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:29
MSG::Level
Level
Definition: IMessageSvc.h:22
MsgStream::eof
bool eof() const
Definition: MsgStream.h:171
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:22
MsgStream::width
std::streamsize width(std::streamsize v)
Definition: MsgStream.h:160
MsgStream::precision
std::streamsize precision(int v)
Definition: MsgStream.h:162
MSG::NIL
@ NIL
Definition: IMessageSvc.h:22
MsgStream::fill
char fill(char v)
Definition: MsgStream.h:168
Properties.v
v
Definition: Properties.py:122
MsgStream::flags
long flags(FLAG_TYPE v)
Definition: MsgStream.h:165
SerializeSTL.h
MsgStream::fill
char fill() const
Definition: MsgStream.h:167
MsgStream::operator<<
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:133
GaudiUtils
Definition: Allocator.h:62
MsgStream::~MsgStream
virtual GAUDI_API ~MsgStream()=default
Standard destructor.
MSG::NUM_LEVELS
@ NUM_LEVELS
Definition: IMessageSvc.h:22
MsgStream::MsgStream
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:66
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:103
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:56
MsgStream::activate
void activate()
Activate MsgStream.
Definition: MsgStream.h:112
MsgStream::unsetf
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:174
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:116
MsgStream::FLAG_TYPE
std::ios_base::fmtflags FLAG_TYPE
Error return code in case ios modification is requested for inactive streams.
Definition: MsgStream.h:34
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83
MsgStream::setf
long setf(FLAG_TYPE v)
Definition: MsgStream.h:166
MsgStream::stream
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:99
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:89
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:58
MsgStream::precision
std::streamsize precision() const
Definition: MsgStream.h:161
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:27