#include <iomanip>
#include <iostream>
#include "TObject.h"
#include "TMVA/MsgLogger.h"
using namespace std;
#define USE_COLORED_CONSOLE
ClassImp(TMVA::MsgLogger)
;
static const string::size_type MAXIMUM_SOURCE_NAME_LENGTH = 13;
static const string PREFIX = "--- ";
static const string SUFFIX = ": ";
TMVA::MsgLogger::MsgLogger( const TObject* source, EMsgType minType )
: fObjSource( source ),
fStrSource( "" ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMaxSourceSize( MAXIMUM_SOURCE_NAME_LENGTH ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( const string& source, EMsgType minType )
: fObjSource( 0 ),
fStrSource( source ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMaxSourceSize( MAXIMUM_SOURCE_NAME_LENGTH ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( EMsgType minType )
: fObjSource( 0 ),
fStrSource( "Unknown" ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMaxSourceSize( MAXIMUM_SOURCE_NAME_LENGTH ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( const MsgLogger& parent ) :
basic_ios< MsgLogger::char_type, MsgLogger::traits_type >(),
ostringstream(),
TObject(),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fMaxSourceSize( MAXIMUM_SOURCE_NAME_LENGTH )
{
InitMaps();
*this = parent;
}
TMVA::MsgLogger::~MsgLogger()
{
}
TMVA::MsgLogger& TMVA::MsgLogger::operator= ( const MsgLogger& parent )
{
if( &parent != this) {
fObjSource = parent.fObjSource;
fStrSource = parent.fStrSource;
fActiveType = parent.fActiveType;
fMinType = parent.fMinType;
}
return *this;
}
string TMVA::MsgLogger::GetFormattedSource() const
{
string source_name;
if (fObjSource) source_name = fObjSource->GetName();
else source_name = fStrSource;
if (source_name.size() > fMaxSourceSize) {
source_name = source_name.substr( 0, fMaxSourceSize - 3 );
source_name += "...";
}
return source_name;
}
string TMVA::MsgLogger::GetPrintedSource() const
{
string source_name = GetFormattedSource();
if (source_name.size() < fMaxSourceSize)
for (string::size_type i=source_name.size(); i<fMaxSourceSize; i++) source_name.push_back( ' ' );
return fPrefix + source_name + fSuffix;
}
void TMVA::MsgLogger::Send()
{
string source_name = GetFormattedSource();
string message = this->str();
string::size_type previous_pos = 0, current_pos = 0;
for (;;) {
current_pos = message.find( '\n', previous_pos );
string line = message.substr( previous_pos, current_pos - previous_pos );
ostringstream message_to_send;
message_to_send.setf( ios::adjustfield, ios::left );
message_to_send.width( fMaxSourceSize );
message_to_send << source_name << fSuffix << line;
this->WriteMsg( fActiveType, message_to_send.str() );
if (current_pos == message.npos) break;
previous_pos = current_pos + 1;
}
this->str( "" );
return;
}
void TMVA::MsgLogger::WriteMsg( EMsgType type, const std::string& line ) const
{
if (type < fMinType) return;
map<EMsgType, std::string>::const_iterator stype;
if ((stype = fTypeMap.find( type )) == fTypeMap.end()) return;
#ifdef USE_COLORED_CONSOLE
if (type == kINFO)
cout << fPrefix << line << endl;
else
cout << fColorMap.find( type )->second << fPrefix << "<" << stype->second << "> " << line << "\033[0m" << endl;
#else
if (type == kINFO)
cout << fPrefix << line << endl;
else
cout << fPrefix << "<" << stype->second << "> " << line << endl;
#endif // USE_COLORED_CONSOLE
if (type == kFATAL) { cout << "***> abort program execution" << endl; exit(1); }
}
TMVA::MsgLogger& TMVA::MsgLogger::Endmsg( MsgLogger& logger )
{
logger.Send();
return logger;
}
void TMVA::MsgLogger::InitMaps()
{
fTypeMap[kVERBOSE] = "VERBOSE";
fTypeMap[kDEBUG] = "DEBUG";
fTypeMap[kINFO] = "INFO";
fTypeMap[kWARNING] = "WARNING";
fTypeMap[kERROR] = "ERROR";
fTypeMap[kFATAL] = "FATAL";
fTypeMap[kALWAYS] = "ALWAYS";
fColorMap[kVERBOSE] = "\033[1;34m";
fColorMap[kDEBUG] = "\033[34m";
fColorMap[kINFO] = "";
fColorMap[kWARNING] = "\033[31m";
fColorMap[kERROR] = "\033[31m";
fColorMap[kFATAL] = "\033[1;31;40m";
fColorMap[kALWAYS] = "\033[30m";
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.