| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

# 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
    00341 template <typename T>
    00342 MsgStream& operator<< (MsgStream& lhs, const T& arg)  {
    00343   using namespace GaudiUtils;
    00344   if(lhs.isActive())
    00345     try {
    00346       // this may throw, and we cannot afford it if the stream is used in a catch block
    00347       lhs.stream() << arg;
    00348     }
    00349     catch (...) {}
    00350   return lhs;
    00351 }
    00352
    00353 #ifdef __GNUC__
    00354
    00355 template<typename T>
    00356 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
    00357   if ( lhs.isActive() )
    00358     try {
    00359       // this may throw, and we cannot afford it if the stream is used in a catch block
    00360       lhs.stream() << manip;
    00361     } catch(...) {}
    00362   return lhs;
    00363 }
    00364 #endif
    00365
    00366 #endif    // GAUDIKERNEL_MSGSTREAM_H
    00367

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines