SFrame 3.6
core/include/SLogger.h
Go to the documentation of this file.
00001 // Dear emacs, this is -*- c++ -*-
00002 // $Id: SLogger.h 331 2012-11-20 17:12:44Z krasznaa $
00003 /***************************************************************************
00004  * @Project: SFrame - ROOT-based analysis framework for ATLAS
00005  * @Package: Core
00006  *
00007  * @author Stefan Ask       <Stefan.Ask@cern.ch>           - Manchester
00008  * @author David Berge      <David.Berge@cern.ch>          - CERN
00009  * @author Johannes Haller  <Johannes.Haller@cern.ch>      - Hamburg
00010  * @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen
00011  *
00012  ***************************************************************************/
00013 
00014 #ifndef SFRAME_CORE_SLogger_H
00015 #define SFRAME_CORE_SLogger_H
00016 
00017 // STL include(s):
00018 #include <string>
00019 #include <sstream>
00020 
00021 // Local include(s):
00022 #include "SMsgType.h"
00023 #include "SLogWriter.h"
00024 
00025 // Forward declaration(s):
00026 class TObject;
00027 class SLogWriter;
00028 
00045 class SLogger : public std::ostringstream {
00046 
00047 public:
00049    SLogger( const TObject* source );
00051    SLogger( const std::string& source );
00053    SLogger( const SLogger& parent );
00055    virtual ~SLogger();
00056 
00058    void SetSource( const TObject* source );
00060    void SetSource( const std::string& source );
00061 
00063    const char* GetSource() const;
00064 
00066    SLogger& operator= ( const SLogger& parent );
00067 
00069    static SLogger& endmsg( SLogger& logger );
00070 
00072    SLogger& operator<< ( SLogger& ( *_f )( SLogger& ) );
00074    SLogger& operator<< ( std::ostream& ( *_f )( std::ostream& ) );
00076    SLogger& operator<< ( std::ios& ( *_f )( std::ios& ) );
00077 
00079    SLogger& operator<< ( SMsgType type );
00080 
00082 
00087    template < class T > SLogger& operator<< ( T arg ) {
00088       if( m_activeType >= m_logWriter->GetMinType() ) {
00089          ( * ( std::ostringstream* ) this ) << arg;
00090       }
00091       return *this;
00092    }
00093 
00095    void Send( SMsgType type, const std::string& message ) const;
00096 
00097 private:
00098    void Send();
00099 
00100    const TObject* m_objSource;
00101    std::string    m_strSource;
00102    SLogWriter*    m_logWriter;
00103    SMsgType       m_activeType;
00104 
00105 }; // class SLogger
00106 
00108 //                                                                  //
00109 //   To speed up the code a bit, the following operators are        //
00110 //   declared 'inline'.                                             //
00111 //                                                                  //
00113 
00119 inline SLogger& SLogger::operator<< ( SLogger& ( *_f )( SLogger& ) ) {
00120 
00121    return ( _f )( *this );
00122 }
00123 
00129 inline SLogger& SLogger::operator<< ( std::ostream& ( *_f )( std::ostream& ) ) {
00130 
00131    if( m_activeType >= m_logWriter->GetMinType() ) {
00132       ( _f )( *this );
00133    }
00134    return *this;
00135 }
00136 
00142 inline SLogger& SLogger::operator<< ( std::ios& ( *_f )( std::ios& ) ) {
00143 
00144    if( m_activeType >= m_logWriter->GetMinType() ) {
00145       ( _f )( *this );
00146    }
00147    return *this;
00148 }
00149 
00158 inline SLogger& SLogger::operator<< ( SMsgType type ) {
00159 
00160    m_activeType = type;
00161    return *this;
00162 }
00163 
00164 // This is a GCC extension for getting the name of the current function.
00165 #if defined( __GNUC__ )
00166 #   define SLOGGER_FNAME __PRETTY_FUNCTION__
00167 #else
00168 #   define SLOGGER_FNAME ""
00169 #endif
00170 
00172 
00182 #define SLOGGER_REPORT_PREFIX \
00183    __FILE__ << ":" << __LINE__ << " (" << SLOGGER_FNAME << "): "
00184 
00186 
00196 #define REPORT_VERBOSE( MESSAGE ) \
00197    m_logger << VERBOSE << SLOGGER_REPORT_PREFIX << MESSAGE << SLogger::endmsg
00198 
00200 
00210 #define REPORT_ERROR( MESSAGE ) \
00211    m_logger << ERROR << SLOGGER_REPORT_PREFIX << MESSAGE << SLogger::endmsg
00212 
00214 
00224 #define REPORT_FATAL( MESSAGE ) \
00225    m_logger << FATAL << SLOGGER_REPORT_PREFIX << MESSAGE << SLogger::endmsg
00226 
00227 #endif // SFRAME_CORE_SLogger_H