Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
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-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 #if defined( __GNUC__ )
204 # ifndef __APPLE__
205 inline MsgStream& operator<<( MsgStream& s, const std::_Setiosflags& manip ) {
206  try {
207  // this may throw, and we cannot afford it if the stream is used in a catch block
208  if ( s.isActive() ) s.stream() << manip;
209  } catch ( ... ) {}
210  return s;
211 }
212 inline MsgStream& operator<<( MsgStream& s, const std::_Resetiosflags& manip ) {
213  try {
214  // this may throw, and we cannot afford it if the stream is used in a catch block
215  if ( s.isActive() ) s.stream() << manip;
216  } catch ( ... ) {}
217  return s;
218 }
219 inline MsgStream& operator<<( MsgStream& s, const std::_Setbase& manip ) {
220  try {
221  // this may throw, and we cannot afford it if the stream is used in a catch block
222  if ( s.isActive() ) s.stream() << manip;
223  } catch ( ... ) {}
224  return s;
225 }
226 inline MsgStream& operator<<( MsgStream& s, const std::_Setprecision& manip ) {
227  try {
228  // this may throw, and we cannot afford it if the stream is used in a catch block
229  if ( s.isActive() ) s.stream() << manip;
230  } catch ( ... ) {}
231  return s;
232 }
233 inline MsgStream& operator<<( MsgStream& s, const std::_Setw& manip ) {
234  try {
235  // this may throw, and we cannot afford it if the stream is used in a catch block
236  if ( s.isActive() ) s.stream() << manip;
237  } catch ( ... ) {}
238  return s;
239 }
240 # endif // not __APPLE__
241 #else // GCC, version << 3
242 template <class _Tm>
244 inline MsgStream& operator<<( MsgStream& s, const std::smanip<_Tm>& manip ) {
245  try {
246  // this may throw, and we cannot afford it if the stream is used in a catch block
247  if ( s.isActive() ) s.stream() << manip;
248  } catch ( ... ) {}
249  return s;
250 }
251 #endif // __GNUC__
252 
253 namespace MSG {
254  inline MsgStream& dec( MsgStream& log ) {
255  log << std::dec;
256  return log;
257  }
258  inline MsgStream& hex( MsgStream& log ) {
259  log << std::hex;
260  return log;
261  }
262 } // namespace MSG
263 
266 inline MsgStream& operator<<( MsgStream& s, const char* arg ) {
267  try {
268  // this may throw, and we cannot afford it if the stream is used in a catch block
269  if ( s.isActive() ) s.stream() << arg;
270  } catch ( ... ) {}
271  return s;
272 }
273 
275 template <typename T>
276 MsgStream& operator<<( MsgStream& lhs, const T& arg ) {
277  using namespace GaudiUtils;
278  if ( lhs.isActive() ) try {
279  // this may throw, and we cannot afford it if the stream is used in a catch block
280  lhs.stream() << arg;
281  } catch ( ... ) {}
282  return lhs;
283 }
284 
285 #if defined( __GNUC__ ) and not defined( __APPLE__ )
286 template <typename T>
288 MsgStream& operator<<( MsgStream& lhs, const std::_Setfill<T>& manip ) {
289  if ( lhs.isActive() ) try {
290  // this may throw, and we cannot afford it if the stream is used in a catch block
291  lhs.stream() << manip;
292  } catch ( ... ) {}
293  return lhs;
294 }
295 #endif
MSG
Print levels enumeration.
Definition: IMessageSvc.h:21
MSG::hex
MsgStream & hex(MsgStream &log)
Definition: MsgStream.h:258
MsgStream::operator<<
MsgStream & operator<<(long long arg)
Definition: MsgStream.h:144
IMessageSvc
Definition: IMessageSvc.h:34
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:145
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:254
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:244
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:93
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:49
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:85
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