All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 <string>
10 #include <iomanip>
11 #include <vector>
12 #include <sstream>
13 
24 class MsgStream {
25 private:
29  typedef std::ios_base::fmtflags FLAG_TYPE;
30  typedef std::ios_base::iostate STATE_TYPE;
31 protected:
35  std::string m_buffer;
37  std::string m_source;
39  std::ostringstream m_stream;
41  bool m_active;
52  static bool m_countInactive;
53 
54 public:
56  GAUDI_API MsgStream(IMessageSvc* svc, int buffer_length=128);
58  GAUDI_API MsgStream(IMessageSvc* svc, const std::string& source, int buffer_length=128);
61  : m_service(msg.m_service),
62  m_active(msg.m_active),
63  m_level(msg.m_level),
66  {
67  try { // ignore exception if we cannot copy the string
68  m_source = msg.m_source;
69  }
70  catch (...) {}
71  }
73  GAUDI_API virtual ~MsgStream();
75  MsgStream& report(int lvl) {
76  lvl = (lvl >= MSG::NUM_LEVELS) ?
77  MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
78  if ((m_currLevel=MSG::Level(lvl)) >= level()) {
79  activate();
80  } else {
81  deactivate();
82 #ifndef NDEBUG
85  }
86 #endif
87  }
88  return *this;
89  }
91  virtual GAUDI_API MsgStream& doOutput();
93  const std::string& buffer() const {
94  return m_buffer;
95  }
97  std::ostringstream& stream() {
98  return m_stream;
99  }
101  void setMsgSvc( IMessageSvc* svc ) {
102  m_service = svc;
103  }
105  void setLevel(int level) {
106  level = (level >= MSG::NUM_LEVELS) ?
107  MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
108  m_level = MSG::Level(level);
109  }
112  return m_level;
113  }
116  return m_currLevel;
117  }
119  void activate() {
120  m_active = true;
121  }
123  void deactivate() {
124  m_active = false;
125  }
127  bool isActive() const {
128  return m_active;
129  }
130  // oMsgStream flush emulation
132  if ( isActive() ) m_stream.flush();
133  return *this;
134  }
135  // oMsgStream write emulation
136  MsgStream& write(const char* buff,int len) {
137  if ( isActive() ) m_stream.write(buff, len);
138  return *this;
139  }
142  if ( isActive() ) _f(*this);
143  return *this;
144  }
146  MsgStream& operator<<(std::ostream& (*_f)(std::ostream&)) {
147  if ( isActive() ) _f(m_stream);
148  return *this;
149  }
151  MsgStream& operator<<(std::ios& (*_f)(std::ios&)) {
152  if ( isActive() ) _f(m_stream);
153  return *this;
154  }
157  return report(level);
158  }
160  try {
161  // this may throw, and we cannot afford it if the stream is used in a catch block
162  if(isActive()) {
163  m_stream << arg;
164  }
165  } catch (...) {}
166  return *this;
167  }
168 
170  MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&)) {
171  if ( isActive() ) _f(m_stream);
172  return *this;
173  }
174 
176  long flags() const {
177  return isActive() ? m_stream.flags() : 0;
178  }
179  long flags(FLAG_TYPE v) {
180  return isActive() ? m_stream.flags(v) : 0;
181  }
182  long setf(FLAG_TYPE v) {
183  return isActive() ? m_stream.setf(v) : 0;
184  }
185  int width() const {
186  return isActive() ? m_stream.width() : 0;
187  }
188  int width(int v) {
189  return isActive() ? m_stream.width(v) : 0;
190  }
191  char fill() const {
192  return isActive() ? m_stream.fill() : (char)-1;
193  }
194  char fill(char v) {
195  return isActive() ? m_stream.fill(v) : (char)-1;
196  }
197  int precision() const {
198  return isActive() ? m_stream.precision(): 0;
199  }
200  int precision(int v) {
201  return isActive() ? m_stream.precision(v): 0;
202  }
203  int rdstate() const {
204  return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
205  }
206  int good() const {
207  return isActive() ? m_stream.good () : 0;
208  }
209  int eof() const {
210  return isActive() ? m_stream.eof () : 0;
211  }
212  int bad() const {
213  return isActive() ? m_stream.bad() : 0;
214  }
215  long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
216  return isActive() ? m_stream.setf(_f, _m) : 0;
217  }
218  void unsetf(FLAG_TYPE _l) {
219  if ( isActive() ) m_stream.unsetf(_l);
220  }
221  void clear(STATE_TYPE _i = std::ios_base::failbit) {
222  if ( isActive() ) m_stream.clear(_i);
223  }
224 
226  GAUDI_API void setColor(MSG::Color col);
229 
231  GAUDI_API void resetColor();
232 
235  static GAUDI_API bool enableCountInactive(bool value = true);
236 
238  static GAUDI_API bool countInactive();
239 
240 };
241 
244  return s.doOutput();
245 }
246 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_ENDREQ)
247 #define endreq endmsg
249 #endif
250 
252 GAUDI_API std::string format(const char*, ... );
253 
254 #ifdef _WIN32
255 template<class _E> inline
256 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj) {
257 #if _MSC_VER > 1300
258  if ( s.isActive() ) s.stream().fill(obj._Fill);
259 #else
260  if ( s.isActive() ) s.stream().fill(obj._Ch);
261 #endif
262  return s;
263 }
264 template<class _Tm> inline
265 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
266 #if _MSC_VER > 1300
267  if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
268 #else
269  if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
270 #endif
271  return s;
272 }
273 #elif defined (__GNUC__)
275  const std::_Setiosflags &manip) {
276  try {
277  // this may throw, and we cannot afford it if the stream is used in a catch block
278  if ( s.isActive() ) s.stream() << manip;
279  } catch(...) {}
280  return s;
281 }
282 inline MsgStream& operator << (MsgStream& s,
283  const std::_Resetiosflags &manip) {
284  try {
285  // this may throw, and we cannot afford it if the stream is used in a catch block
286  if ( s.isActive() ) s.stream() << manip;
287  } catch (...) {}
288  return s;
289 }
290 inline MsgStream& operator << (MsgStream& s,
291  const std::_Setbase &manip) {
292  try {
293  // this may throw, and we cannot afford it if the stream is used in a catch block
294  if ( s.isActive() ) s.stream() << manip;
295  } catch (...) {}
296  return s;
297 }
298 inline MsgStream& operator << (MsgStream& s,
299  const std::_Setprecision &manip) {
300  try {
301  // this may throw, and we cannot afford it if the stream is used in a catch block
302  if ( s.isActive() ) s.stream() << manip;
303  } catch (...) {}
304  return s;
305 }
306 inline MsgStream& operator << (MsgStream& s,
307  const std::_Setw &manip) {
308  try {
309  // this may throw, and we cannot afford it if the stream is used in a catch block
310  if ( s.isActive() ) s.stream() << manip;
311  } catch (...) {}
312  return s;
313 }
314 
315 namespace MSG {
316  inline
317  MsgStream& dec(MsgStream& log) {
318  log.setf(std::ios_base::dec, std::ios_base::basefield);
319  return log;
320  }
321  inline
322  MsgStream& hex(MsgStream& log) {
323  log.setf(std::ios_base::hex, std::ios_base::basefield);
324  return log;
325  }
326 }
327 
328 #else // GCC, version << 3
329 template<class _Tm> inline
331 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip) {
332  try {
333  // this may throw, and we cannot afford it if the stream is used in a catch block
334  if ( s.isActive() ) s.stream() << manip;
335  } catch (...) {}
336  return s;
337 }
338 #endif // WIN32 or (__GNUC__)
339 
342 inline MsgStream& operator<< (MsgStream& s, const char *arg){
343  try {
344  // this may throw, and we cannot afford it if the stream is used in a catch block
345  if ( s.isActive() ) s.stream() << arg;
346  } catch (...) {}
347  return s;
348 }
349 
351 template <typename T>
352 MsgStream& operator<< (MsgStream& lhs, const T& arg) {
353  using namespace GaudiUtils;
354  if(lhs.isActive())
355  try {
356  // this may throw, and we cannot afford it if the stream is used in a catch block
357  lhs.stream() << arg;
358  }
359  catch (...) {}
360  return lhs;
361 }
362 
363 #ifdef __GNUC__
364 template<typename T>
366 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
367  if ( lhs.isActive() )
368  try {
369  // this may throw, and we cannot afford it if the stream is used in a catch block
370  lhs.stream() << manip;
371  } catch(...) {}
372  return lhs;
373 }
374 #endif
375 
376 #endif // GAUDIKERNEL_MSGSTREAM_H
377 
std::ostringstream & stream()
Access string MsgStream.
Definition: MsgStream.h:97
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & operator<<(longlong arg)
Definition: MsgStream.h:159
const std::string & buffer() const
Access string buffer.
Definition: MsgStream.h:93
MSG::Level currentLevel()
Retrieve current stream output level.
Definition: MsgStream.h:115
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
int width(int v)
Definition: MsgStream.h:188
bool m_useColors
use colors
Definition: MsgStream.h:47
char fill() const
Definition: MsgStream.h:191
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:117
int rdstate() const
Definition: MsgStream.h:203
int precision() const
Definition: MsgStream.h:197
int precision(int v)
Definition: MsgStream.h:200
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:111
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:52
int eof() const
Definition: MsgStream.h:209
int bad() const
Definition: MsgStream.h:212
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:127
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:33
MsgStream & write(const char *buff, int len)
Definition: MsgStream.h:136
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
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:72
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:37
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:43
MsgStream & operator<<(MsgStream &s, const std::smanip< _Tm > &manip)
I/O Manipulator for setfill.
Definition: MsgStream.h:331
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:90
std::ios_base::iostate STATE_TYPE
Definition: MsgStream.h:30
int good() const
Definition: MsgStream.h:206
MsgStream & operator<<(MsgStream &(*_f)(MsgStream &))
Accept MsgStream modifiers.
Definition: MsgStream.h:141
MsgStream & operator<<(std::ios_base &(*_f)(std::ios_base &))
Accept ios base class modifiers.
Definition: MsgStream.h:170
long setf(FLAG_TYPE _f, FLAG_TYPE _m)
Definition: MsgStream.h:215
MsgStream & operator<<(std::ios &(*_f)(std::ios &))
Accept ios modifiers.
Definition: MsgStream.h:151
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:221
void deactivate()
Deactivate MsgStream.
Definition: MsgStream.h:123
MsgStream & flush()
Definition: MsgStream.h:131
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:39
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
char fill(char v)
Definition: MsgStream.h:194
void setMsgSvc(IMessageSvc *svc)
Update IMessageSvc pointer.
Definition: MsgStream.h:101
bool m_active
Flag set to true if formatting engine is active.
Definition: MsgStream.h:41
std::string m_buffer
Use standard string for information buffering.
Definition: MsgStream.h:35
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:45
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:43
long flags() const
IOS emulation.
Definition: MsgStream.h:176
virtual GAUDI_API ~MsgStream()
Standard destructor.
Definition: MsgStream.cpp:69
int width() const
Definition: MsgStream.h:185
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:105
string s
Definition: gaudirun.py:210
MsgStream & operator<<(std::ostream &(*_f)(std::ostream &))
Accept oMsgStream modifiers.
Definition: MsgStream.h:146
void activate()
Activate MsgStream.
Definition: MsgStream.h:119
MsgStream(const MsgStream &msg)
Copy constructor.
Definition: MsgStream.h:60
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:50
long flags(FLAG_TYPE v)
Definition: MsgStream.h:179
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:182
#define GAUDI_API
Definition: Kernel.h:108
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:32
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:38
void unsetf(FLAG_TYPE _l)
Definition: MsgStream.h:218