SFrame 3.6
core/src/SLogWriter.cxx
Go to the documentation of this file.
00001 // $Id: SLogWriter.cxx 245 2011-02-21 10:23:10Z 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 // System include(s):
00014 extern "C" {
00015 #   include <unistd.h>
00016 }
00017 
00018 // STL include(s):
00019 #include <iostream>
00020 
00021 // Local include(s):
00022 #include "../include/SLogWriter.h"
00023 
00024 SLogWriter* SLogWriter::m_instance = 0;
00025 
00032 SLogWriter* SLogWriter::Instance() {
00033 
00034    if( ! m_instance ) {
00035       m_instance = new SLogWriter();
00036    }
00037 
00038    return m_instance;
00039 }
00040 
00044 SLogWriter::~SLogWriter() {
00045 
00046 }
00047 
00056 void SLogWriter::Write( SMsgType type, const std::string& line ) const {
00057 
00058    if( type < m_minType ) return;
00059    std::map< SMsgType, std::string >::const_iterator stype;
00060    if( ( stype = m_typeMap.find( type ) ) == m_typeMap.end() ) return;
00061 
00062    // Print the output in colours only if it's printed to the console. If it's
00063    // redirected to a logfile, then produce simple black on while output.
00064    if( isatty( STDOUT_FILENO ) ) {
00065       std::cout << m_colorMap.find( type )->second << " (" << stype->second << ")  "
00066                 << line << "\033[0m" << std::endl;
00067    } else {
00068       std::cout << " (" << stype->second << ")  " << line << std::endl;
00069    }
00070 
00071    return;
00072 }
00073 
00082 void SLogWriter::SetMinType( SMsgType type ) {
00083 
00084    m_minType = type;
00085    return;
00086 }
00087 
00093 SMsgType SLogWriter::GetMinType() const {
00094 
00095    return m_minType;
00096 }
00097 
00102 SLogWriter::SLogWriter()
00103    : m_minType( INFO ) {
00104 
00105    m_typeMap[ VERBOSE ] = "VERBOSE";
00106    m_typeMap[ DEBUG ]   = " DEBUG ";
00107    m_typeMap[ INFO ]    = " INFO  ";
00108    m_typeMap[ WARNING ] = "WARNING";
00109    m_typeMap[ ERROR ]   = " ERROR ";
00110    m_typeMap[ FATAL ]   = " FATAL ";
00111    m_typeMap[ ALWAYS ]  = "ALWAYS ";
00112 
00113    m_colorMap[ VERBOSE ] = "\033[1;34m";
00114    m_colorMap[ DEBUG ]   = "\033[34m";
00115    m_colorMap[ INFO ]    = "\033[32m";
00116    m_colorMap[ WARNING ] = "\033[35m";
00117    m_colorMap[ ERROR ]   = "\033[31m";
00118    m_colorMap[ FATAL ]   = "\033[1;31;40m";
00119    m_colorMap[ ALWAYS ]  = ""; // Used to be: "\033[30m";
00120 
00121 }