| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# MsgStream.h

Go to the documentation of this file.

    00001 // $Id: MsgStream.h,v 1.43 2008/10/30 23:38:46 marcocle Exp $
    00002 #ifndef GAUDIKERNEL_MSGSTREAM_H
    00003 #define GAUDIKERNEL_MSGSTREAM_H
    00004
    00005 // Include files
    00006 #include "GaudiKernel/IMessageSvc.h"
    00007 #include "GaudiKernel/SerializeSTL.h"
    00008 // Standard C++ classes
    00009 #include <cstdio>
    00010 #include <string>
    00011 #include <iomanip>
    00012 #include <vector>
    00013 #include <sstream>
    00014
    00025 class MsgStream {
    00026 private:
    00030   typedef std::ios_base::fmtflags FLAG_TYPE;
    00031   typedef std::ios_base::iostate  STATE_TYPE;
    00032 protected:
    00034   IMessageSvc*    m_service;
    00036   std::string     m_buffer;
    00038   std::string     m_source;
    00040   std::ostringstream m_stream;
    00042   bool            m_active;
    00044   MSG::Level      m_level;
    00046   MSG::Level      m_currLevel;
    00048   bool m_useColors;
    00049 public:
    00051   GAUDI_API MsgStream(IMessageSvc* svc, int buffer_length=128);
    00053   GAUDI_API MsgStream(IMessageSvc* svc, const std::string& source, int buffer_length=128);
    00055   MsgStream(const MsgStream& msg)
    00056     : m_service(msg.m_service),
    00057       m_active(msg.m_active),
    00058       m_level(msg.m_level),
    00059       m_useColors(msg.m_useColors)
    00060   {
    00061     try { // ignore exception if we cannot copy the string
    00062       m_source = msg.m_source;
    00063     }
    00064     catch (...) {}
    00065   }
    00067   GAUDI_API virtual ~MsgStream();
    00069   MsgStream& report(int lvl)   {
    00070     lvl = (lvl >= MSG::NUM_LEVELS) ?
    00071       MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
    00072     ((m_currLevel=MSG::Level(lvl)) >= level()) ? activate() : deactivate();
    00073     return *this;
    00074   }
    00076   virtual GAUDI_API MsgStream& doOutput();
    00078   const std::string& buffer() const {
    00079     return m_buffer;
    00080   }
    00082   std::ostringstream& stream()   {
    00083     return m_stream;
    00084   }
    00086   void setMsgSvc( IMessageSvc* svc ) {
    00087     m_service = svc;
    00088   }
    00090   void setLevel(int level)    {
    00091     level = (level >= MSG::NUM_LEVELS) ?
    00092       MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
    00093     m_level = MSG::Level(level);
    00094   }
    00096   MSG::Level level()   {
    00097     return m_level;
    00098   }
    00100   MSG::Level currentLevel()   {
    00101     return m_currLevel;
    00102   }
    00104   void activate()     {
    00105     m_active = true;
    00106   }
    00108   void deactivate()     {
    00109     m_active = false;
    00110   }
    00112   bool isActive()  const   {
    00113     return m_active;
    00114   }
    00115   // oMsgStream flush emulation
    00116   MsgStream& flush()    {
    00117     if ( isActive() ) m_stream.flush();
    00118     return *this;
    00119   }
    00120   // oMsgStream write emulation
    00121   MsgStream& write(const char* buff,int len)  {
    00122     if ( isActive() ) m_stream.write(buff, len);
    00123     return *this;
    00124   }
    00126   MsgStream& operator<<(MsgStream& (*_f)(MsgStream&))    {
    00127     if ( isActive() ) _f(*this);
    00128     return *this;
    00129   }
    00131   MsgStream& operator<<(std::ostream& (*_f)(std::ostream&))    {
    00132     if ( isActive() ) _f(m_stream);
    00133     return *this;
    00134   }
    00136   MsgStream& operator<<(std::ios& (*_f)(std::ios&))    {
    00137     if ( isActive() ) _f(m_stream);
    00138     return *this;
    00139   }
    00141   MsgStream& operator<< (MSG::Level level)  {
    00142     return report(level);
    00143   }
    00144   MsgStream& operator<<(longlong arg) {
    00145     try {
    00146       // this may throw, and we cannot afford it if the stream is used in a catch block
    00147       if(isActive()) {
    00148 #ifdef _WIN32
    00149         int flg = m_stream.flags();
    00150         char buf[128];
    00151         (flg & std::ios::hex) ?
    00152           ::sprintf(buf,"%I64x",arg) : ::sprintf(buf,"%I64d",arg);
    00153         m_stream << buf;
    00154 #else
    00155         m_stream << arg;
    00156 #endif
    00157       }
    00158     } catch (...) {}
    00159     return *this;
    00160   }
    00161
    00163   MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&))    {
    00164     if ( isActive() ) _f(m_stream);
    00165     return *this;
    00166   }
    00167
    00169   long flags() const {
    00170     return isActive() ? m_stream.flags()    : 0;
    00171   }
    00172   long flags(FLAG_TYPE v) {
    00173     return isActive() ? m_stream.flags(v)  :  0;
    00174   }
    00175   long setf(FLAG_TYPE v) {
    00176     return isActive() ? m_stream.setf(v)  :  0;
    00177   }
    00178   int width() const {
    00179     return isActive() ? m_stream.width()    : 0;
    00180   }
    00181   int width(int v) {
    00182     return isActive() ? m_stream.width(v)    : 0;
    00183   }
    00184   char fill() const {
    00185     return isActive() ? m_stream.fill()     : -1;
    00186   }
    00187   char fill(char v) {
    00188     return isActive() ? m_stream.fill(v)     : -1;
    00189   }
    00190   int precision() const  {
    00191     return isActive() ? m_stream.precision(): 0;
    00192   }
    00193   int precision(int v) {
    00194     return isActive() ? m_stream.precision(v): 0;
    00195   }
    00196   int rdstate() const  {
    00197     return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
    00198   }
    00199   int good() const  {
    00200     return isActive() ? m_stream.good ()    : 0;
    00201   }
    00202   int eof() const  {
    00203     return isActive() ? m_stream.eof ()     : 0;
    00204   }
    00205   int bad() const  {
    00206     return isActive() ? m_stream.bad()      : 0;
    00207   }
    00208   long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
    00209     return isActive() ? m_stream.setf(_f, _m)   : 0;
    00210   }
    00211   void unsetf(FLAG_TYPE _l)    {
    00212     if ( isActive() ) m_stream.unsetf(_l);
    00213   }
    00214   void clear(STATE_TYPE _i = std::ios_base::failbit)  {
    00215     if ( isActive() ) m_stream.clear(_i);
    00216   }
    00217
    00219   GAUDI_API void setColor(MSG::Color col);
    00221   GAUDI_API void setColor(MSG::Color fg, MSG::Color bg);
    00222
    00224   GAUDI_API void resetColor();
    00225
    00226 };
    00227
    00229 inline MsgStream& endmsg(MsgStream& s) {
    00230   return s.doOutput();
    00231 }
    00232 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_ENDREQ)
    00234 #define endreq endmsg
    00235 #endif
    00236
    00238 GAUDI_API std::string format(const char*, ... );
    00239
    00240 #ifdef _WIN32
    00241 template<class _E> inline
    00242 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj)    {
    00243 #if _MSC_VER > 1300
    00244   if ( s.isActive() ) s.stream().fill(obj._Fill);
    00245 #else
    00246   if ( s.isActive() ) s.stream().fill(obj._Ch);
    00247 #endif
    00248   return s;
    00249 }
    00250 template<class _Tm> inline
    00251 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
    00252 #if _MSC_VER > 1300
    00253   if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
    00254 #else
    00255   if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
    00256 #endif
    00257   return s;
    00258 }
    00259 #elif defined (__GNUC__)
    00260 inline MsgStream& operator << (MsgStream& s,
    00261                                const std::_Setiosflags &manip) {
    00262   try {
    00263     // this may throw, and we cannot afford it if the stream is used in a catch block
    00264     if ( s.isActive() ) s.stream() << manip;
    00265   } catch(...) {}
    00266   return s;
    00267 }
    00268 inline MsgStream& operator << (MsgStream& s,
    00269                                const std::_Resetiosflags &manip)      {
    00270   try {
    00271     // this may throw, and we cannot afford it if the stream is used in a catch block
    00272     if ( s.isActive() ) s.stream() << manip;
    00273   } catch (...) {}
    00274   return s;
    00275 }
    00276 inline MsgStream& operator << (MsgStream& s,
    00277                                const std::_Setbase &manip)    {
    00278   try {
    00279     // this may throw, and we cannot afford it if the stream is used in a catch block
    00280     if ( s.isActive() ) s.stream() << manip;
    00281   } catch (...) {}
    00282   return s;
    00283 }
    00284 inline MsgStream& operator << (MsgStream& s,
    00285                                const std::_Setprecision &manip)       {
    00286   try {
    00287     // this may throw, and we cannot afford it if the stream is used in a catch block
    00288     if ( s.isActive() ) s.stream() << manip;
    00289   } catch (...) {}
    00290   return s;
    00291 }
    00292 inline MsgStream& operator << (MsgStream& s,
    00293                                const std::_Setw &manip)       {
    00294   try {
    00295     // this may throw, and we cannot afford it if the stream is used in a catch block
    00296     if ( s.isActive() ) s.stream() << manip;
    00297   } catch (...) {}
    00298   return s;
    00299 }
    00300
    00301 namespace MSG {
    00302   inline
    00303   MsgStream& dec(MsgStream& log) {
    00304     log.setf(std::ios_base::dec, std::ios_base::basefield);
    00305     return log;
    00306   }
    00307   inline
    00308   MsgStream& hex(MsgStream& log) {
    00309     log.setf(std::ios_base::hex, std::ios_base::basefield);
    00310     return log;
    00311   }
    00312 }
    00313
    00314 #else // GCC, version << 3
    00316 template<class _Tm> inline
    00317 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip)  {
    00318   try {
    00319     // this may throw, and we cannot afford it if the stream is used in a catch block
    00320     if ( s.isActive() ) s.stream() << manip;
    00321   } catch (...) {}
    00322   return s;
    00323 }
    00324 #endif    // WIN32 or (__GNUC__)
    00325
    00327 template <typename T>
    00328 MsgStream& operator<< (MsgStream& lhs, const T& arg)  {
    00329   using namespace GaudiUtils;
    00330   if(lhs.isActive())
    00331     try {
    00332       // this may throw, and we cannot afford it if the stream is used in a catch block
    00333       lhs.stream() << arg;
    00334     }
    00335     catch (...) {}
    00336   return lhs;
    00337 }
    00338
    00339 #ifdef __GNUC__
    00341 template<typename T>
    00342 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
    00343   if ( lhs.isActive() )
    00344     try {
    00345       // this may throw, and we cannot afford it if the stream is used in a catch block
    00346       lhs.stream() << manip;
    00347     } catch(...) {}
    00348   return lhs;
    00349 }
    00350 #endif
    00351
    00352 #endif    // GAUDIKERNEL_MSGSTREAM_H
    00353