| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

# MsgStream.h

Go to the documentation of this file.

    00001 // $Id: MsgStream.h,v 1.42 2007/05/22 09:20:48 hmd Exp $
    00002 #ifndef GAUDIKERNEL_MSGSTREAM_H
    00003 #define GAUDIKERNEL_MSGSTREAM_H
    00004
    00005 // Include files
    00006 #include "GaudiKernel/IMessageSvc.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;
    00048 public:
    00050   MsgStream(IMessageSvc* svc, int buffer_length=128);
    00052   MsgStream(IMessageSvc* svc, const std::string& source, int buffer_length=128);
    00054   MsgStream(const MsgStream& msg)
    00055     : m_service(msg.m_service),
    00056       m_source(msg.m_source),
    00057       m_active(msg.m_active),
    00058       m_level(msg.m_level),
    00059       m_useColors(msg.m_useColors)
    00060   {
    00061   }
    00063   virtual ~MsgStream();
    00065   MsgStream& report(int lvl)   {
    00066     lvl = (lvl >= MSG::NUM_LEVELS) ?
    00067       MSG::ALWAYS : (lvl<MSG::NIL) ? MSG::NIL : lvl;
    00068     ((m_currLevel=MSG::Level(lvl)) >= level()) ? activate() : deactivate();
    00069     return *this;
    00070   }
    00072   virtual MsgStream& doOutput();
    00074   const std::string& buffer() const {
    00075     return m_buffer;
    00076   }
    00078   std::ostringstream& stream()   {
    00079     return m_stream;
    00080   }
    00082   void setMsgSvc( IMessageSvc* svc ) {
    00083     m_service = svc;
    00084   }
    00086   void setLevel(int level)    {
    00087     level = (level >= MSG::NUM_LEVELS) ?
    00088       MSG::ALWAYS : (level<MSG::NIL) ? MSG::NIL : level;
    00089     m_level = MSG::Level(level);
    00090   }
    00092   MSG::Level level()   {
    00093     return m_level;
    00094   }
    00096   MSG::Level currentLevel()   {
    00097     return m_currLevel;
    00098   }
    00100   void activate()     {
    00101     m_active = true;
    00102   }
    00104   void deactivate()     {
    00105     m_active = false;
    00106   }
    00108   bool isActive()  const   {
    00109     return m_active;
    00110   }
    00111   // oMsgStream flush emulation
    00112   MsgStream& flush()    {
    00113     if ( isActive() ) m_stream.flush();
    00114     return *this;
    00115   }
    00116   // oMsgStream write emulation
    00117   MsgStream& write(const char* buff,int len)  {
    00118     if ( isActive() ) m_stream.write(buff, len);
    00119     return *this;
    00120   }
    00122   MsgStream& operator<<(MsgStream& (*_f)(MsgStream&))    {
    00123     if ( isActive() ) _f(*this);
    00124     return *this;
    00125   }
    00127   MsgStream& operator<<(std::ostream& (*_f)(std::ostream&))    {
    00128     if ( isActive() ) _f(m_stream);
    00129     return *this;
    00130   }
    00132   MsgStream& operator<<(std::ios& (*_f)(std::ios&))    {
    00133     if ( isActive() ) _f(m_stream);
    00134     return *this;
    00135   }
    00137   MsgStream& operator<< (MSG::Level level)  {
    00138     return report(level);
    00139   }
    00140   MsgStream& operator<<(longlong arg) {
    00141     if(isActive()) {
    00142 #ifdef _WIN32
    00143       int flg = m_stream.flags();
    00144       char buf[128];
    00145       (flg & std::ios::hex) ?
    00146         ::sprintf(buf,"%I64x",arg) : ::sprintf(buf,"%I64d",arg);
    00147       m_stream << buf;
    00148 #else
    00149       m_stream << arg;
    00150 #endif
    00151     }
    00152     return *this;
    00153   }
    00154
    00156   MsgStream& operator<<(std::ios_base& (*_f)(std::ios_base&))    {
    00157     if ( isActive() ) _f(m_stream);
    00158     return *this;
    00159   }
    00160
    00162   long flags() const {
    00163     return isActive() ? m_stream.flags()    : 0;
    00164   }
    00165   long flags(FLAG_TYPE v) {
    00166     return isActive() ? m_stream.flags(v)  :  0;
    00167   }
    00168   long setf(FLAG_TYPE v) {
    00169     return isActive() ? m_stream.setf(v)  :  0;
    00170   }
    00171   int width() const {
    00172     return isActive() ? m_stream.width()    : 0;
    00173   }
    00174   int width(int v) {
    00175     return isActive() ? m_stream.width(v)    : 0;
    00176   }
    00177   char fill() const {
    00178     return isActive() ? m_stream.fill()     : -1;
    00179   }
    00180   char fill(char v) {
    00181     return isActive() ? m_stream.fill(v)     : -1;
    00182   }
    00183   int precision() const  {
    00184     return isActive() ? m_stream.precision(): 0;
    00185   }
    00186   int precision(int v) {
    00187     return isActive() ? m_stream.precision(v): 0;
    00188   }
    00189   int rdstate() const  {
    00190     return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
    00191   }
    00192   int good() const  {
    00193     return isActive() ? m_stream.good ()    : 0;
    00194   }
    00195   int eof() const  {
    00196     return isActive() ? m_stream.eof ()     : 0;
    00197   }
    00198   int bad() const  {
    00199     return isActive() ? m_stream.bad()      : 0;
    00200   }
    00201   long setf(FLAG_TYPE _f, FLAG_TYPE _m) {
    00202     return isActive() ? m_stream.setf(_f, _m)   : 0;
    00203   }
    00204   void unsetf(FLAG_TYPE _l)    {
    00205     if ( isActive() ) m_stream.unsetf(_l);
    00206   }
    00207   void clear(STATE_TYPE _i = std::ios_base::failbit)  {
    00208     if ( isActive() ) m_stream.clear(_i);
    00209   }
    00210
    00212   void setColor(MSG::Color col);
    00214   void setColor(MSG::Color fg, MSG::Color bg);
    00215
    00217   void resetColor();
    00218
    00219 };
    00220
    00222 inline MsgStream& endreq(MsgStream& s)   {
    00223   return s.doOutput();
    00224 }
    00226 inline MsgStream& endmsg(MsgStream& s)   {
    00227   return s.doOutput();
    00228 }
    00229
    00231 std::string format(const char*, ... );
    00232
    00233 #ifdef _WIN32
    00234 template<class _E> inline
    00235 MsgStream& operator<<( MsgStream& s, const std::_Fillobj<_E>& obj)    {
    00236 #if _MSC_VER > 1300
    00237   if ( s.isActive() ) s.stream().fill(obj._Fill);
    00238 #else
    00239   if ( s.isActive() ) s.stream().fill(obj._Ch);
    00240 #endif
    00241   return s;
    00242 }
    00243 template<class _Tm> inline
    00244 MsgStream& operator << (MsgStream& s, const std::_Smanip<_Tm>& manip) {
    00245 #if _MSC_VER > 1300
    00246   if ( s.isActive() ) (*manip._Pfun)(s.stream(), manip._Manarg);
    00247 #else
    00248   if ( s.isActive() ) (*manip._Pf)(s.stream(), manip._Manarg);
    00249 #endif
    00250   return s;
    00251 }
    00252 #elif defined (__GNUC__)
    00253 inline MsgStream& operator << (MsgStream& s,
    00254                                const std::_Setiosflags &manip) {
    00255   if ( s.isActive() ) s.stream() << manip;
    00256   return s;
    00257 }
    00258 inline MsgStream& operator << (MsgStream& s,
    00259                                const std::_Resetiosflags &manip)      {
    00260   if ( s.isActive() ) s.stream() << manip;
    00261   return s;
    00262 }
    00263 inline MsgStream& operator << (MsgStream& s,
    00264                                const std::_Setbase &manip)    {
    00265   if ( s.isActive() ) s.stream() << manip;
    00266   return s;
    00267 }
    00268 inline MsgStream& operator << (MsgStream& s,
    00269                                const std::_Setprecision &manip)       {
    00270   if ( s.isActive() ) s.stream() << manip;
    00271   return s;
    00272 }
    00273 inline MsgStream& operator << (MsgStream& s,
    00274                                const std::_Setw &manip)       {
    00275   if ( s.isActive() ) s.stream() << manip;
    00276   return s;
    00277 }
    00278
    00279 namespace MSG {
    00280   inline
    00281   MsgStream& dec(MsgStream& log) {
    00282     log.setf(std::ios_base::dec, std::ios_base::basefield);
    00283     return log;
    00284   }
    00285   inline
    00286   MsgStream& hex(MsgStream& log) {
    00287     log.setf(std::ios_base::hex, std::ios_base::basefield);
    00288     return log;
    00289   }
    00290 }
    00291
    00292 #else // GCC, version << 3
    00294 template<class _Tm> inline
    00295 MsgStream& operator << (MsgStream& s, const std::smanip<_Tm>& manip)  {
    00296   if ( s.isActive() ) s.stream() << manip;
    00297   return s;
    00298 }
    00299 #endif    // WIN32 or (__GNUC__)
    00300
    00302 template <typename T>
    00303 MsgStream& operator<< (MsgStream& lhs, const T& arg)  {
    00304   if(lhs.isActive()) lhs.stream() << arg;
    00305   return lhs;
    00306 }
    00308 template <typename T>
    00309 MsgStream& operator<< (MsgStream& lhs, const std::vector<T>& v ) {
    00310   if(lhs.isActive())
    00311     for ( typename std::vector<T>::const_iterator i = v.begin();
    00312           i != v.end(); ++i ) { lhs.stream() << *i << " "; }
    00313   return lhs;
    00314 }
    00316 template <typename T, typename A>
    00317 MsgStream& operator<< (MsgStream& lhs, const std::vector<T,A>& v ) {
    00318   if(lhs.isActive())
    00319     for ( typename std::vector<T,A>::const_iterator i = v.begin();
    00320           i != v.end(); ++i ) { lhs.stream() << *i << " "; }
    00321   return lhs;
    00322 }
    00323
    00324 #ifdef __GNUC__
    00326 template<typename T>
    00327 MsgStream& operator << (MsgStream& lhs, const std::_Setfill<T> &manip) {
    00328   if ( lhs.isActive() ) lhs.stream() << manip;
    00329   return lhs;
    00330 }
    00331 #endif
    00332
    00333 #endif    // GAUDIKERNEL_MSGSTREAM_H
    00334