Logo ROOT  
Reference Guide
MsgLogger.h
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Attila Krasznahorkay, Andreas Hoecker, Joerg Stelzer, Eckhard von Toerne
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : MsgLogger *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * TMVA output logger class producing nicely formatted log messages *
12 * *
13 * Author: *
14 * Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN, Switzerland *
15 * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
16 * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
17 * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
18 * *
19 * Copyright (c) 2005-2011: *
20 * CERN, Switzerland *
21 * U. of Victoria, Canada *
22 * MPI-K Heidelberg, Germany *
23 * U. of Bonn, Germany *
24 * *
25 * Redistribution and use in source and binary forms, with or without *
26 * modification, are permitted according to the terms listed in LICENSE *
27 * (http://tmva.sourceforge.net/LICENSE) *
28 **********************************************************************************/
29
30#ifndef ROOT_TMVA_MsgLogger
31#define ROOT_TMVA_MsgLogger
32
33//////////////////////////////////////////////////////////////////////////
34// //
35// MsgLogger //
36// //
37// ostringstream derivative to redirect and format output //
38// //
39//////////////////////////////////////////////////////////////////////////
40
41// STL include(s):
42#include <string>
43#include <sstream>
44#include <iostream>
45#include <map>
46#include <atomic>
47
48// ROOT include(s)
49#include "TObject.h"
50
51#include "TMVA/Types.h"
52
53// Local include(s):
54
55namespace TMVA {
56
57 class MsgLogger : public std::ostringstream, public TObject {
58
59 public:
60
61 MsgLogger( const TObject* source, EMsgType minType = kINFO );
62 MsgLogger( const std::string& source, EMsgType minType = kINFO );
63 MsgLogger( EMsgType minType = kINFO );
64 MsgLogger( const MsgLogger& parent );
65 ~MsgLogger();
66
67 // Accessors
68 void SetSource ( const std::string& source ) { fStrSource = source; }
69 EMsgType GetMinType() const { return fMinType; }
70 void SetMinType( EMsgType minType ) { fMinType = minType; }
71 std::string GetSource() const { return fStrSource; }
72 std::string GetPrintedSource() const;
73 std::string GetFormattedSource() const;
74
75 static UInt_t GetMaxSourceSize();
76
77 // Needed for copying
78 MsgLogger& operator= ( const MsgLogger& parent );
79
80 // Stream modifier(s)
81 static MsgLogger& Endmsg( MsgLogger& logger );
82
83 // Accept stream modifiers
85 MsgLogger& operator<< ( std::ostream& ( *_f )( std::ostream& ) );
86 MsgLogger& operator<< ( std::ios& ( *_f )( std::ios& ) );
87
88 // Accept message type specification
90
91 // For all the "conventional" inputs
92 template <class T> MsgLogger& operator<< ( T arg ) {
93 *(std::ostringstream*)this << arg;
94 return *this;
95 }
96
97 // Temporally disables all the loggers (Caution! Use with care !)
98 static void InhibitOutput();
99 static void EnableOutput();
100
101 private:
102
103 // private utility routines
104 void Send();
105 void InitMaps();
106 void WriteMsg( EMsgType type, const std::string& line ) const;
107
108 const TObject* fObjSource; // the source TObject (used for name)
109 std::string fStrSource; // alternative string source
110 static const std::string fgPrefix; // the prefix of the source name
111 static const std::string fgSuffix; // suffix following source name
112 EMsgType fActiveType; // active type
113 static const UInt_t fgMaxSourceSize; // maximum length of source name
114 static std::atomic<Bool_t> fgOutputSupressed; // disable the output globally (used by generic booster)
115 static std::atomic<Bool_t> fgInhibitOutput; // flag to suppress all output
116
117 static std::atomic<const std::map<EMsgType, std::string>*> fgTypeMap; // matches output types with strings
118 static std::atomic<const std::map<EMsgType, std::string>*> fgColorMap; // matches output types with terminal colors
119 EMsgType fMinType; // minimum type for output
120
121 ClassDef(MsgLogger,0) // Ostringstream derivative to redirect and format logging output
122 }; // class MsgLogger
123
125 {
126 return (_f)(*this);
127 }
128
129 inline MsgLogger& MsgLogger::operator<< ( std::ostream& (*_f)( std::ostream& ) )
130 {
131 (_f)(*this);
132 return *this;
133 }
134
135 inline MsgLogger& MsgLogger::operator<< ( std::ios& ( *_f )( std::ios& ) )
136 {
137 (_f)(*this);
138 return *this;
139 }
140
142 {
144 return *this;
145 }
146
147 // Shortcut
148 inline MsgLogger& Endl(MsgLogger& ml) { return MsgLogger::Endmsg(ml); }
149
150}
151
152#endif // TMVA_MsgLogger
unsigned int UInt_t
Definition: RtypesCore.h:46
#define ClassDef(name, id)
Definition: Rtypes.h:325
int type
Definition: TGX11.cxx:121
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:57
void SetMinType(EMsgType minType)
Definition: MsgLogger.h:70
static const UInt_t fgMaxSourceSize
Definition: MsgLogger.h:113
static const std::string fgPrefix
Definition: MsgLogger.h:110
~MsgLogger()
destructor
Definition: MsgLogger.cxx:121
MsgLogger(const TObject *source, EMsgType minType=kINFO)
constructor
Definition: MsgLogger.cxx:72
void SetSource(const std::string &source)
Definition: MsgLogger.h:68
MsgLogger & operator=(const MsgLogger &parent)
assignment operator
Definition: MsgLogger.cxx:128
std::string GetPrintedSource() const
the full logger prefix
Definition: MsgLogger.cxx:173
std::string fStrSource
Definition: MsgLogger.h:109
static std::atomic< Bool_t > fgOutputSupressed
Definition: MsgLogger.h:114
static std::atomic< const std::map< EMsgType, std::string > * > fgColorMap
Definition: MsgLogger.h:118
MsgLogger & operator<<(MsgLogger &(*_f)(MsgLogger &))
Definition: MsgLogger.h:124
static UInt_t GetMaxSourceSize()
returns the maximum source size
Definition: MsgLogger.cxx:165
static void InhibitOutput()
Definition: MsgLogger.cxx:67
const TObject * fObjSource
Definition: MsgLogger.h:108
static const std::string fgSuffix
Definition: MsgLogger.h:111
static void EnableOutput()
Definition: MsgLogger.cxx:68
EMsgType fMinType
Definition: MsgLogger.h:119
static std::atomic< const std::map< EMsgType, std::string > * > fgTypeMap
Definition: MsgLogger.h:117
std::string GetSource() const
Definition: MsgLogger.h:71
std::string GetFormattedSource() const
make sure the source name is no longer than fgMaxSourceSize:
Definition: MsgLogger.cxx:143
void InitMaps()
Create the message type and color maps.
Definition: MsgLogger.cxx:270
EMsgType fActiveType
Definition: MsgLogger.h:112
void WriteMsg(EMsgType type, const std::string &line) const
putting the output string, the message type, and the color switcher together into a single string
Definition: MsgLogger.cxx:220
static MsgLogger & Endmsg(MsgLogger &logger)
end line
Definition: MsgLogger.cxx:261
static std::atomic< Bool_t > fgInhibitOutput
Definition: MsgLogger.h:115
void Send()
activates the logger writer
Definition: MsgLogger.cxx:185
EMsgType GetMinType() const
Definition: MsgLogger.h:69
EMsgType
Definition: Types.h:55
@ kINFO
Definition: Types.h:58
Mother of all ROOT objects.
Definition: TObject.h:37
TLine * line
double T(double x)
Definition: ChebyshevPol.h:34
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:148