Gaudi Framework, version v23r2

Home   Generated: Thu Jun 28 2012

MsgStream.h

Go to the documentation of this file.
00001 #ifndef GAUDIKERNEL_MSGSTREAM_H
00002 #define GAUDIKERNEL_MSGSTREAM_H
00003 
00004 // Include files
00005 #include "GaudiKernel/IMessageSvc.h"
00006 #include "GaudiKernel/SerializeSTL.h"
00007 // Standard C++ classes
00008 #include <cstdio>
00009 #include <string>
00010 #include <iomanip>
00011 #include <vector>
00012 #include <sstream>
00013 
00024 class MsgStream {
00025 private:
00029   typedef std::ios_base::fmtflags FLAG_TYPE;
00030   typedef std::ios_base::iostate  STATE_TYPE;
00031 protected:
00033   IMessageSvc*    m_service;
00035   std::string     m_buffer;
00037   std::string     m_source;
00039   std::ostringstream m_stream;
00041   bool            m_active;
00043   MSG::Level      m_level;
00045   MSG::Level      m_currLevel;
00047   bool m_useColors;
00050   IInactiveMessageCounter*    m_inactCounter;
00052   static bool m_countInactive;
00053 
00054 public:
00056   GAUDI_API MsgStream(IMessageSvc* svc, int buffer_length=128);
00058   GAUDI_API MsgStream(IMessageSvc* svc, const std::string& source, int buffer_length=128);
00060   MsgStream(const MsgStream& msg)
00061     : m_service(msg.m_service),
00062       m_active(msg.m_active),
00063       m_level(msg.m_level),
00064       m_useColors(msg.m_useColors),
00065       m_inactCounter(msg.m_inactCounter)
00066   {
00067     try { // ignore exception if we cannot copy the string
00068       m_source = msg.m_source;
00069     }
00070     catch (...) {}
00071   }
00073   GAUDI_API virtual ~MsgStream();
00075   MsgStream& report(int lvl)   {
00076     lvl = (lvl >= MSG::NUM_LEVELS) ?
00077       MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
00078     if  ((m_currLevel=MSG::Level(lvl)) >= level()) {
00079       activate();
00080     } else {
00081       deactivate();
00082 #ifndef NDEBUG
00083       if (MsgStream::countInactive() && m_inactCounter) {
00084         m_inactCounter->incrInactiveCount(MSG::Level(lvl),m_source);
00085       }
00086 #endif
00087     }
00088     return *this;
00089   }
00091   virtual GAUDI_API MsgStream& doOutput();
00093   const std::string& buffer() const {
00094     return m_buffer;
00095   }
00097   std::ostringstream& stream()   {
00098     return m_stream;
00099   }
00101   void setMsgSvc( IMessageSvc* svc ) {
00102     m_service = svc;
00103   }
00105   void setLevel(int level)    {
00106     level = (level >= MSG::NUM_LEVELS) ?
00107       MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
00108     m_level = MSG::Level(level);
00109   }
00111   MSG::Level level()   {
00112     return m_level;
00113   }
00115   MSG::Level currentLevel()   {
00116     return m_currLevel;
00117   }
00119   void activate()     {
00120     m_active = true;
00121   }
00123   void deactivate()     {
00124     m_active = false;
00125   }
00127   bool isActive()  const   {
00128     return m_active;
00129   }
00130   // oMsgStream flush emulation
00131   MsgStream& flush()    {
00132     if ( isActive() ) m_stream.flush();
00133     return *this;
00134   }
00135   // oMsgStream write emulation
00136   MsgStream& write(const char* buff,int len)  {
00137     if ( isActive() ) m_stream.write(buff, len);
00138     return *this;
00139   }
00141   MsgStream& operator<<(MsgStream& (*_f)(MsgStream&))    {
00142     if ( isActive() ) _f(*this);
00143     return *this;
00144   }
00146   MsgStream& operator<<(std::ostream& (*_f)(std::ostream&))    {
00147     if ( isActive() ) _f(m_stream);
00148     return *this;
00149   }
00151   MsgStream& operator<<(std::ios& (*_f)(std::ios&))    {
00152     if ( isActive() ) _f(m_stream);
00153     return *this;
00154   }
00156   MsgStream& operator<< (MSG::Level level)  {
00157     return report(level);
00158   }
00159   MsgStream& operator<<(longlong arg) {
00160     try {
00161       // this may throw, and we cannot afford it if the stream is used in a catch block
00162       if(isActive()) {
00163         m_stream << arg;
00164       }
00165     } catch (...) {}
00166     return *this;
00167   }
00168 
00170   MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&))    {
00171     if ( isActive() ) _f(m_stream);
00172     return *this;
00173   }
00174 
00176   long flags() const {
00177     return isActive() ? m_stream.flags()    : 0;
00178   }
00179   long flags(FLAG_TYPE v) {
00180     return isActive() ? m_stream.flags(v)  :  0;
00181   }
00182   long setf(FLAG_TYPE v) {
00183     return isActive() ? m_stream.setf(v)  :  0;
00184   }
00185   int width() const {
00186     return isActive() ? m_stream.width()    : 0;
00187   }
00188   int width(int v) {
00189     return isActive() ? m_stream.width(v)    : 0;
00190   }
00191   char fill() const {
00192     return isActive() ? m_stream.fill()     : (char)-1;
00193   }
00194   char fill(char v) {
00195     return isActive() ? m_stream.fill(v)     : (char)-1;
00196   }
00197   int precision() const  {
00198     return isActive() ? m_stream.precision(): 0;
00199   }
00200   int precision(int v) {
00201     return isActive() ? m_stream.precision(v): 0;
00202   }
00203   int rdstate() const  {
00204     return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
00205   }
00206   int good() const  {
00207     return isActive() ? m_stream.good ()    : 0;
00208   }
00209   int eof() const  {
00210     return isActive() ? m_stream.eof ()     : 0;
00211   }
00212   int bad() const  {
00213     return isActive() ? m_stream.bad()      : 0;
00214   }
00215   long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
00216     return isActive() ? m_stream.setf(_f, _m)   : 0;
00217   }
00218   void unsetf(FLAG_TYPE _l)    {
00219     if ( isActive() ) m_stream.unsetf(_l);
00220   }
00221   void clear(STATE_TYPE _i = std::ios_base::failbit)  {
00222     if ( isActive() ) m_stream.clear(_i);
00223   }
00224 
00226   GAUDI_API void setColor(MSG::Color col);
00228   GAUDI_API void setColor(MSG::Color fg, MSG::Color bg);
00229 
00231   GAUDI_API void resetColor();
00232 
00235   static GAUDI_API bool enableCountInactive(bool value = true);
00236 
00238   static GAUDI_API bool countInactive();
00239 
00240 };
00241 
00243 inline MsgStream& endmsg(MsgStream& s) {
00244   return s.doOutput();
00245 }
00246 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_ENDREQ)
00247 
00248 #define endreq endmsg
00249 #endif
00250 
00252 GAUDI_API std::string format(const char*, ... );
00253 
00254 #ifdef _WIN32
00255 template<class _E> inline
00256 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj)    {
00257 #if _MSC_VER > 1300
00258   if ( s.isActive() ) s.stream().fill(obj._Fill);
00259 #else
00260   if ( s.isActive() ) s.stream().fill(obj._Ch);
00261 #endif
00262   return s;
00263 }
00264 template<class _Tm> inline
00265 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
00266 #if _MSC_VER > 1300
00267   if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
00268 #else
00269   if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
00270 #endif
00271   return s;
00272 }
00273 #elif defined (__GNUC__)
00274 inline MsgStream& operator << (MsgStream& s,
00275                                const std::_Setiosflags &manip) {
00276   try {
00277     // this may throw, and we cannot afford it if the stream is used in a catch block
00278     if ( s.isActive() ) s.stream() << manip;
00279   } catch(...) {}
00280   return s;
00281 }
00282 inline MsgStream& operator << (MsgStream& s,
00283                                const std::_Resetiosflags &manip)      {
00284   try {
00285     // this may throw, and we cannot afford it if the stream is used in a catch block
00286     if ( s.isActive() ) s.stream() << manip;
00287   } catch (...) {}
00288   return s;
00289 }
00290 inline MsgStream& operator << (MsgStream& s,
00291                                const std::_Setbase &manip)    {
00292   try {
00293     // this may throw, and we cannot afford it if the stream is used in a catch block
00294     if ( s.isActive() ) s.stream() << manip;
00295   } catch (...) {}
00296   return s;
00297 }
00298 inline MsgStream& operator << (MsgStream& s,
00299                                const std::_Setprecision &manip)       {
00300   try {
00301     // this may throw, and we cannot afford it if the stream is used in a catch block
00302     if ( s.isActive() ) s.stream() << manip;
00303   } catch (...) {}
00304   return s;
00305 }
00306 inline MsgStream& operator << (MsgStream& s,
00307                                const std::_Setw &manip)       {
00308   try {
00309     // this may throw, and we cannot afford it if the stream is used in a catch block
00310     if ( s.isActive() ) s.stream() << manip;
00311   } catch (...) {}
00312   return s;
00313 }
00314 
00315 namespace MSG {
00316   inline
00317   MsgStream& dec(MsgStream& log) {
00318     log.setf(std::ios_base::dec, std::ios_base::basefield);
00319     return log;
00320   }
00321   inline
00322   MsgStream& hex(MsgStream& log) {
00323     log.setf(std::ios_base::hex, std::ios_base::basefield);
00324     return log;
00325   }
00326 }
00327 
00328 #else // GCC, version << 3
00329 
00330 template<class _Tm> inline
00331 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip)  {
00332   try {
00333     // this may throw, and we cannot afford it if the stream is used in a catch block
00334     if ( s.isActive() ) s.stream() << manip;
00335   } catch (...) {}
00336   return s;
00337 }
00338 #endif    // WIN32 or (__GNUC__)
00339 
00342 inline MsgStream& operator<< (MsgStream& s, const char *arg){
00343   try {
00344     // this may throw, and we cannot afford it if the stream is used in a catch block
00345     if ( s.isActive() ) s.stream() << arg;
00346   } catch (...) {}
00347   return s;
00348 }
00349 
00351 template <typename T>
00352 MsgStream& operator<< (MsgStream& lhs, const T& arg)  {
00353   using namespace GaudiUtils;
00354   if(lhs.isActive())
00355     try {
00356       // this may throw, and we cannot afford it if the stream is used in a catch block
00357       lhs.stream() << arg;
00358     }
00359     catch (...) {}
00360   return lhs;
00361 }
00362 
00363 #ifdef __GNUC__
00364 
00365 template<typename T>
00366 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
00367   if ( lhs.isActive() )
00368     try {
00369       // this may throw, and we cannot afford it if the stream is used in a catch block
00370       lhs.stream() << manip;
00371     } catch(...) {}
00372   return lhs;
00373 }
00374 #endif
00375 
00376 #endif    // GAUDIKERNEL_MSGSTREAM_H
00377 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Thu Jun 28 2012 23:27:21 for Gaudi Framework, version v23r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004