#include "TMVA/MsgLogger.h"
#include "TMVA/Config.h"
#include <iomanip>
#include <stdlib.h>
#define PREFIX "--- "
#define SUFFIX ": "
ClassImp(TMVA::MsgLogger)
UInt_t TMVA::MsgLogger::fgMaxSourceSize = 15;
TMVA::MsgLogger::MsgLogger( const TObject* source, EMsgType minType )
: fObjSource( source ),
fStrSource( "" ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( const std::string& source, EMsgType minType )
: fObjSource( 0 ),
fStrSource( source ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( EMsgType minType )
: fObjSource( 0 ),
fStrSource( "Unknown" ),
fPrefix( PREFIX ),
fSuffix( SUFFIX ),
fActiveType( kINFO ),
fMinType( minType )
{
InitMaps();
}
TMVA::MsgLogger::MsgLogger( const MsgLogger& parent ) :
std::basic_ios< MsgLogger::char_type, MsgLogger::traits_type >(),
std::ostringstream(),
TObject(),
fPrefix( PREFIX ),
fSuffix( SUFFIX )
{
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;
}
std::string TMVA::MsgLogger::GetFormattedSource() const
{
std::string source_name;
if (fObjSource) source_name = fObjSource->GetName();
else source_name = fStrSource;
if (source_name.size() > fgMaxSourceSize) {
source_name = source_name.substr( 0, fgMaxSourceSize - 3 );
source_name += "...";
}
return source_name;
}
std::string TMVA::MsgLogger::GetPrintedSource() const
{
std::string source_name = GetFormattedSource();
if (source_name.size() < fgMaxSourceSize)
for (std::string::size_type i=source_name.size(); i<fgMaxSourceSize; i++) source_name.push_back( ' ' );
return fPrefix + source_name + fSuffix;
}
void TMVA::MsgLogger::Send()
{
std::string source_name = GetFormattedSource();
std::string message = this->str();
std::string::size_type previous_pos = 0, current_pos = 0;
while (kTRUE) {
current_pos = message.find( '\n', previous_pos );
std::string line = message.substr( previous_pos, current_pos - previous_pos );
std::ostringstream message_to_send;
message_to_send.setf( std::ios::adjustfield, std::ios::left );
message_to_send.width( fgMaxSourceSize );
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;
std::map<EMsgType, std::string>::const_iterator stype;
if ((stype = fTypeMap.find( type )) == fTypeMap.end()) return;
if (!gConfig().IsSilent()) {
if (gConfig().UseColor()) {
if (type == kINFO) std::cout << fPrefix << line << std::endl;
else std::cout << fColorMap.find( type )->second << fPrefix << "<"
<< stype->second << "> " << line << "\033[0m" << std::endl;
}
else {
if (type == kINFO) std::cout << fPrefix << line << std::endl;
else std::cout << fPrefix << "<" << stype->second << "> " << line << std::endl;
}
}
if (type == kFATAL) {
if (!gConfig().IsSilent()) std::cout << "***> abort program execution" << std::endl;
exit(1);
}
}
TMVA::MsgLogger& TMVA::MsgLogger::Endmsg( MsgLogger& logger )
{
logger.Send();
return logger;
}
void TMVA::MsgLogger::InitMaps()
{
fTypeMap[kVERBOSE] = std::string("VERBOSE");
fTypeMap[kDEBUG] = std::string("DEBUG");
fTypeMap[kINFO] = std::string("INFO");
fTypeMap[kWARNING] = std::string("WARNING");
fTypeMap[kERROR] = std::string("ERROR");
fTypeMap[kFATAL] = std::string("FATAL");
fTypeMap[kSILENT] = std::string("SILENT");
fColorMap[kVERBOSE] = std::string("\033[1;34m");
fColorMap[kDEBUG] = std::string("\033[34m");
fColorMap[kINFO] = std::string("");
fColorMap[kWARNING] = std::string("\033[1;31m");
fColorMap[kERROR] = std::string("\033[31m");
fColorMap[kFATAL] = std::string("\033[37;41;1m");
fColorMap[kSILENT] = std::string("\033[30m");
}
Last change: Sat Nov 1 10:21:58 2008
Last generated: 2008-11-01 10:21
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.