SFrame 3.6
core/src/SLogger.cxx
Go to the documentation of this file.
00001 // $Id: SLogger.cxx 331 2012-11-20 17:12:44Z krasznaa $
00002 /***************************************************************************
00003  * @Project: SFrame - ROOT-based analysis framework for ATLAS
00004  * @Package: Core
00005  *
00006  * @author Stefan Ask       <Stefan.Ask@cern.ch>           - Manchester
00007  * @author David Berge      <David.Berge@cern.ch>          - CERN
00008  * @author Johannes Haller  <Johannes.Haller@cern.ch>      - Hamburg
00009  * @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen
00010  *
00011  ***************************************************************************/
00012 
00013 // STL include(s):
00014 #include <iomanip>
00015 #include <iostream>
00016 
00017 // ROOT include(s):
00018 #include "TObject.h"
00019 
00020 // Local include(s):
00021 #include "../include/SLogger.h"
00022 
00023 using namespace std;
00024 
00026 static const string::size_type MAXIMUM_SOURCE_NAME_LENGTH = 18;
00027 
00039 SLogger::SLogger( const TObject* source )
00040    : m_objSource( source ), m_strSource( "" ), m_activeType( INFO ) {
00041 
00042    m_logWriter = SLogWriter::Instance();
00043 
00044 }
00045 
00053 SLogger::SLogger( const string& source )
00054    : m_objSource( 0 ), m_strSource( source ), m_activeType( INFO ) {
00055 
00056    m_logWriter = SLogWriter::Instance();
00057 
00058 }
00059 
00067 SLogger::SLogger( const SLogger& parent )
00068    : std::basic_ios< SLogger::char_type, SLogger::traits_type >(),
00069      ostringstream() {
00070 
00071    *this = parent;
00072 
00073 }
00074 
00078 SLogger::~SLogger() {
00079 
00080 }
00081 
00082 void SLogger::SetSource( const TObject* source ) {
00083 
00084    m_objSource = source;
00085    m_strSource = "";
00086    return;
00087 
00088 }
00089 
00090 void SLogger::SetSource( const std::string& source ) {
00091 
00092    m_objSource = 0;
00093    m_strSource = source;
00094    return;
00095 
00096 }
00097 
00098 const char* SLogger::GetSource() const {
00099 
00100    if( m_objSource ) {
00101       return m_objSource->GetName();
00102    } else {
00103       return m_strSource.c_str();
00104    }
00105 }
00106 
00114 SLogger& SLogger::operator= ( const SLogger& parent ) {
00115 
00116    m_objSource = parent.m_objSource;
00117    m_strSource = parent.m_strSource;
00118    m_logWriter = SLogWriter::Instance();
00119 
00120    return *this;
00121 
00122 }
00123 
00135 void SLogger::Send( const SMsgType type, const string& message ) const {
00136 
00137    if( type < m_logWriter->GetMinType() ) return;
00138 
00139    string::size_type previous_pos = 0, current_pos = 0;
00140 
00141    //
00142    // Make sure the source name is no longer than MAXIMUM_SOURCE_NAME_LENGTH:
00143    //
00144    string source_name;
00145    if( m_objSource ) {
00146       source_name = m_objSource->GetName();
00147    } else {
00148       source_name = m_strSource;
00149    }
00150    if( source_name.size() > MAXIMUM_SOURCE_NAME_LENGTH ) {
00151       source_name = source_name.substr( 0, MAXIMUM_SOURCE_NAME_LENGTH - 3 );
00152       source_name += "...";
00153    }
00154 
00155    //
00156    // Slice the recieved message into lines:
00157    //
00158    for( ; ; ) {
00159 
00160       current_pos = message.find( '\n', previous_pos );
00161       string line = message.substr( previous_pos, current_pos - previous_pos );
00162 
00163       ostringstream message_to_send;
00164       // I have to call the modifiers like this, otherwise g++ get's confused
00165       // with the operators...
00166       message_to_send.setf( ios::adjustfield, ios::left );
00167       message_to_send.width( MAXIMUM_SOURCE_NAME_LENGTH );
00168       message_to_send << source_name << " : " << line;
00169       m_logWriter->Write( type, message_to_send.str() );
00170 
00171       if( current_pos == message.npos ) break;
00172       previous_pos = current_pos + 1;
00173 
00174    }
00175 
00176    return;
00177 
00178 }
00179 
00186 void SLogger::Send() {
00187 
00188    //
00189    // Call the "other" send(...) function:
00190    //
00191    this->Send( m_activeType, this->str() );
00192 
00193    //
00194    // Reset the stream buffer:
00195    //
00196    this->str( "" );
00197 
00198    return;
00199 
00200 }
00201 
00211 SLogger& SLogger::endmsg( SLogger& logger ) {
00212 
00213    logger.Send();
00214    return logger;
00215 
00216 }