00001 // @(#)root/tmva $Id: MsgLogger.h 36966 2010-11-26 09:50:13Z evt $ 00002 // Author: Attila Krasznahorkay 00003 00004 /********************************************************************************** 00005 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis * 00006 * Package: TMVA * 00007 * Class : MsgLogger * 00008 * Web : http://tmva.sourceforge.net * 00009 * * 00010 * Description: * 00011 * TMVA output logger class producing nicely formatted log messages * 00012 * * 00013 * Author: * 00014 * Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN, Switzerland * 00015 * * 00016 * Copyright (c) 2005: * 00017 * CERN, Switzerland * 00018 * MPI-K Heidelberg, Germany * 00019 * * 00020 * Redistribution and use in source and binary forms, with or without * 00021 * modification, are permitted according to the terms listed in LICENSE * 00022 * (http://tmva.sourceforge.net/LICENSE) * 00023 **********************************************************************************/ 00024 00025 #ifndef ROOT_TMVA_MsgLogger 00026 #define ROOT_TMVA_MsgLogger 00027 00028 ////////////////////////////////////////////////////////////////////////// 00029 // // 00030 // MsgLogger // 00031 // // 00032 // ostringstream derivative to redirect and format output // 00033 // // 00034 ////////////////////////////////////////////////////////////////////////// 00035 00036 // STL include(s): 00037 #include <string> 00038 #include <sstream> 00039 #include <iostream> 00040 #include <map> 00041 00042 // ROOT include(s) 00043 #ifndef ROOT_TObject 00044 #include "TObject.h" 00045 #endif 00046 00047 #ifndef ROOT_TMVA_Types 00048 #include "TMVA/Types.h" 00049 #endif 00050 00051 // Local include(s): 00052 00053 namespace TMVA { 00054 00055 class MsgLogger : public std::ostringstream, public TObject { 00056 00057 public: 00058 00059 MsgLogger( const TObject* source, EMsgType minType = kINFO ); 00060 MsgLogger( const std::string& source, EMsgType minType = kINFO ); 00061 MsgLogger( EMsgType minType = kINFO ); 00062 MsgLogger( const MsgLogger& parent ); 00063 ~MsgLogger(); 00064 00065 // Accessors 00066 void SetSource ( const std::string& source ) { fStrSource = source; } 00067 EMsgType GetMinType() const { return fMinType; } 00068 void SetMinType( EMsgType minType ) { fMinType = minType; } 00069 std::string GetSource() const { return fStrSource; } 00070 std::string GetPrintedSource() const; 00071 std::string GetFormattedSource() const; 00072 00073 static UInt_t GetMaxSourceSize() { return (UInt_t)fgMaxSourceSize; } 00074 00075 // Needed for copying 00076 MsgLogger& operator= ( const MsgLogger& parent ); 00077 00078 // Stream modifier(s) 00079 static MsgLogger& Endmsg( MsgLogger& logger ); 00080 00081 // Accept stream modifiers 00082 MsgLogger& operator<< ( MsgLogger& ( *_f )( MsgLogger& ) ); 00083 MsgLogger& operator<< ( std::ostream& ( *_f )( std::ostream& ) ); 00084 MsgLogger& operator<< ( std::ios& ( *_f )( std::ios& ) ); 00085 00086 // Accept message type specification 00087 MsgLogger& operator<< ( EMsgType type ); 00088 00089 // For all the "conventional" inputs 00090 template <class T> MsgLogger& operator<< ( T arg ) { 00091 *(std::ostringstream*)this << arg; 00092 return *this; 00093 } 00094 00095 // Temporaly disables all the loggers (Caution! Use with care !) 00096 static void InhibitOutput(); 00097 static void EnableOutput(); 00098 00099 private: 00100 00101 // private utility routines 00102 void Send(); 00103 void InitMaps(); 00104 void WriteMsg( EMsgType type, const std::string& line ) const; 00105 00106 const TObject* fObjSource; // the source TObject (used for name) 00107 std::string fStrSource; // alternative string source 00108 static const std::string fgPrefix; // the prefix of the source name 00109 static const std::string fgSuffix; // suffix following source name 00110 EMsgType fActiveType; // active type 00111 static UInt_t fgMaxSourceSize; // maximum length of source name 00112 static Bool_t fgOutputSupressed; // disable the output globaly (used by generic booster) 00113 static Bool_t fgInhibitOutput; // flag to suppress all output 00114 static Int_t fgInstanceCounter; // counts open MsgLogger instances 00115 00116 static std::map<EMsgType, std::string>* fgTypeMap; // matches output types with strings 00117 static std::map<EMsgType, std::string>* fgColorMap; // matches output types with terminal colors 00118 EMsgType fMinType; // minimum type for output 00119 00120 ClassDef(MsgLogger,0) // Ostringstream derivative to redirect and format logging output 00121 }; // class MsgLogger 00122 00123 inline MsgLogger& MsgLogger::operator<< ( MsgLogger& (*_f)( MsgLogger& ) ) 00124 { 00125 return (_f)(*this); 00126 } 00127 00128 inline MsgLogger& MsgLogger::operator<< ( std::ostream& (*_f)( std::ostream& ) ) 00129 { 00130 (_f)(*this); 00131 return *this; 00132 } 00133 00134 inline MsgLogger& MsgLogger::operator<< ( std::ios& ( *_f )( std::ios& ) ) 00135 { 00136 (_f)(*this); 00137 return *this; 00138 } 00139 00140 inline MsgLogger& MsgLogger::operator<< ( EMsgType type ) 00141 { 00142 fActiveType = type; 00143 return *this; 00144 } 00145 00146 // Although the proper definition of "Endl" as a function pointer 00147 // would be nicer C++-wise, it introduces some "unused variable" 00148 // warnings so let's use the #define definition after all... 00149 // static MsgLogger& ( *Endl )( MsgLogger& ) = &MsgLogger::Endmsg; 00150 #define Endl MsgLogger::Endmsg 00151 00152 } 00153 00154 #endif // TMVA_MsgLogger