| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# 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   MsgStream(IMessageSvc* svc, int buffer_length=128);
    00053   MsgStream(IMessageSvc* svc, const std::string& source, int buffer_length=128);
    00055   MsgStream(const MsgStream& msg)
    00056     : m_service(msg.m_service),
    00057       m_source(msg.m_source),
    00058       m_active(msg.m_active),
    00059       m_level(msg.m_level),
    00060       m_useColors(msg.m_useColors)
    00061   {
    00062   }
    00064   virtual ~MsgStream();
    00066   MsgStream& report(int lvl)   {
    00067     lvl = (lvl >= MSG::NUM_LEVELS) ?
    00068       MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
    00069     ((m_currLevel=MSG::Level(lvl)) >= level()) ? activate() : deactivate();
    00070     return *this;
    00071   }
    00073   virtual MsgStream& doOutput();
    00075   const std::string& buffer() const {
    00076     return m_buffer;
    00077   }
    00079   std::ostringstream& stream()   {
    00080     return m_stream;
    00081   }
    00083   void setMsgSvc( IMessageSvc* svc ) {
    00084     m_service = svc;
    00085   }
    00087   void setLevel(int level)    {
    00088     level = (level >= MSG::NUM_LEVELS) ?
    00089       MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
    00090     m_level = MSG::Level(level);
    00091   }
    00093   MSG::Level level()   {
    00094     return m_level;
    00095   }
    00097   MSG::Level currentLevel()   {
    00098     return m_currLevel;
    00099   }
    00101   void activate()     {
    00102     m_active = true;
    00103   }
    00105   void deactivate()     {
    00106     m_active = false;
    00107   }
    00109   bool isActive()  const   {
    00110     return m_active;
    00111   }
    00112   // oMsgStream flush emulation
    00113   MsgStream& flush()    {
    00114     if ( isActive() ) m_stream.flush();
    00115     return *this;
    00116   }
    00117   // oMsgStream write emulation
    00118   MsgStream& write(const char* buff,int len)  {
    00119     if ( isActive() ) m_stream.write(buff, len);
    00120     return *this;
    00121   }
    00123   MsgStream& operator<<(MsgStream& (*_f)(MsgStream&))    {
    00124     if ( isActive() ) _f(*this);
    00125     return *this;
    00126   }
    00128   MsgStream& operator<<(std::ostream& (*_f)(std::ostream&))    {
    00129     if ( isActive() ) _f(m_stream);
    00130     return *this;
    00131   }
    00133   MsgStream& operator<<(std::ios& (*_f)(std::ios&))    {
    00134     if ( isActive() ) _f(m_stream);
    00135     return *this;
    00136   }
    00138   MsgStream& operator<< (MSG::Level level)  {
    00139     return report(level);
    00140   }
    00141   MsgStream& operator<<(longlong arg) {
    00142     if(isActive()) {
    00143 #ifdef _WIN32
    00144       int flg = m_stream.flags();
    00145       char buf[128];
    00146       (flg & std::ios::hex) ?
    00147         ::sprintf(buf,"%I64x",arg) : ::sprintf(buf,"%I64d",arg);
    00148       m_stream << buf;
    00149 #else
    00150       m_stream << arg;
    00151 #endif
    00152     }
    00153     return *this;
    00154   }
    00155
    00157   MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&))    {
    00158     if ( isActive() ) _f(m_stream);
    00159     return *this;
    00160   }
    00161
    00163   long flags() const {
    00164     return isActive() ? m_stream.flags()    : 0;
    00165   }
    00166   long flags(FLAG_TYPE v) {
    00167     return isActive() ? m_stream.flags(v)  :  0;
    00168   }
    00169   long setf(FLAG_TYPE v) {
    00170     return isActive() ? m_stream.setf(v)  :  0;
    00171   }
    00172   int width() const {
    00173     return isActive() ? m_stream.width()    : 0;
    00174   }
    00175   int width(int v) {
    00176     return isActive() ? m_stream.width(v)    : 0;
    00177   }
    00178   char fill() const {
    00179     return isActive() ? m_stream.fill()     : -1;
    00180   }
    00181   char fill(char v) {
    00182     return isActive() ? m_stream.fill(v)     : -1;
    00183   }
    00184   int precision() const  {
    00185     return isActive() ? m_stream.precision(): 0;
    00186   }
    00187   int precision(int v) {
    00188     return isActive() ? m_stream.precision(v): 0;
    00189   }
    00190   int rdstate() const  {
    00191     return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
    00192   }
    00193   int good() const  {
    00194     return isActive() ? m_stream.good ()    : 0;
    00195   }
    00196   int eof() const  {
    00197     return isActive() ? m_stream.eof ()     : 0;
    00198   }
    00199   int bad() const  {
    00200     return isActive() ? m_stream.bad()      : 0;
    00201   }
    00202   long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
    00203     return isActive() ? m_stream.setf(_f, _m)   : 0;
    00204   }
    00205   void unsetf(FLAG_TYPE _l)    {
    00206     if ( isActive() ) m_stream.unsetf(_l);
    00207   }
    00208   void clear(STATE_TYPE _i = std::ios_base::failbit)  {
    00209     if ( isActive() ) m_stream.clear(_i);
    00210   }
    00211
    00213   void setColor(MSG::Color col);
    00215   void setColor(MSG::Color fg, MSG::Color bg);
    00216
    00218   void resetColor();
    00219
    00220 };
    00221
    00223 inline MsgStream& endreq(MsgStream& s)   {
    00224   return s.doOutput();
    00225 }
    00227 inline MsgStream& endmsg(MsgStream& s)   {
    00228   return s.doOutput();
    00229 }
    00230
    00232 std::string format(const char*, ... );
    00233
    00234 #ifdef _WIN32
    00235 template<class _E> inline
    00236 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj)    {
    00237 #if _MSC_VER > 1300
    00238   if ( s.isActive() ) s.stream().fill(obj._Fill);
    00239 #else
    00240   if ( s.isActive() ) s.stream().fill(obj._Ch);
    00241 #endif
    00242   return s;
    00243 }
    00244 template<class _Tm> inline
    00245 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
    00246 #if _MSC_VER > 1300
    00247   if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
    00248 #else
    00249   if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
    00250 #endif
    00251   return s;
    00252 }
    00253 #elif defined (__GNUC__)
    00254 inline MsgStream& operator << (MsgStream& s,
    00255                                const std::_Setiosflags &manip) {
    00256   if ( s.isActive() ) s.stream() << manip;
    00257   return s;
    00258 }
    00259 inline MsgStream& operator << (MsgStream& s,
    00260                                const std::_Resetiosflags &manip)      {
    00261   if ( s.isActive() ) s.stream() << manip;
    00262   return s;
    00263 }
    00264 inline MsgStream& operator << (MsgStream& s,
    00265                                const std::_Setbase &manip)    {
    00266   if ( s.isActive() ) s.stream() << manip;
    00267   return s;
    00268 }
    00269 inline MsgStream& operator << (MsgStream& s,
    00270                                const std::_Setprecision &manip)       {
    00271   if ( s.isActive() ) s.stream() << manip;
    00272   return s;
    00273 }
    00274 inline MsgStream& operator << (MsgStream& s,
    00275                                const std::_Setw &manip)       {
    00276   if ( s.isActive() ) s.stream() << manip;
    00277   return s;
    00278 }
    00279
    00280 namespace MSG {
    00281   inline
    00282   MsgStream& dec(MsgStream& log) {
    00283     log.setf(std::ios_base::dec, std::ios_base::basefield);
    00284     return log;
    00285   }
    00286   inline
    00287   MsgStream& hex(MsgStream& log) {
    00288     log.setf(std::ios_base::hex, std::ios_base::basefield);
    00289     return log;
    00290   }
    00291 }
    00292
    00293 #else // GCC, version << 3
    00295 template<class _Tm> inline
    00296 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip)  {
    00297   if ( s.isActive() ) s.stream() << manip;
    00298   return s;
    00299 }
    00300 #endif    // WIN32 or (__GNUC__)
    00301
    00303 template <typename T>
    00304 MsgStream& operator<< (MsgStream& lhs, const T& arg)  {
    00305   using namespace GaudiUtils;
    00306   if(lhs.isActive()) lhs.stream() << arg;
    00307   return lhs;
    00308 }
    00309
    00310 #ifdef __GNUC__
    00312 template<typename T>
    00313 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
    00314   if ( lhs.isActive() ) lhs.stream() << manip;
    00315   return lhs;
    00316 }
    00317 #endif
    00318
    00319 #endif    // GAUDIKERNEL_MSGSTREAM_H
    00320