Logo ROOT   6.21/01
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 #if __cplusplus > 199711L
47 #include <atomic>
48 #endif
49 
50 // ROOT include(s)
51 #include "TObject.h"
52 
53 #include "TMVA/Types.h"
54 
55 // Local include(s):
56 
57 namespace TMVA {
58 
59  class MsgLogger : public std::ostringstream, public TObject {
60 
61  public:
62 
63  MsgLogger( const TObject* source, EMsgType minType = kINFO );
64  MsgLogger( const std::string& source, EMsgType minType = kINFO );
65  MsgLogger( EMsgType minType = kINFO );
66  MsgLogger( const MsgLogger& parent );
67  ~MsgLogger();
68 
69  // Accessors
70  void SetSource ( const std::string& source ) { fStrSource = source; }
71  EMsgType GetMinType() const { return fMinType; }
72  void SetMinType( EMsgType minType ) { fMinType = minType; }
73  std::string GetSource() const { return fStrSource; }
74  std::string GetPrintedSource() const;
75  std::string GetFormattedSource() const;
76 
77  static UInt_t GetMaxSourceSize() { return static_cast<UInt_t>(fgMaxSourceSize); }
78 
79  // Needed for copying
80  MsgLogger& operator= ( const MsgLogger& parent );
81 
82  // Stream modifier(s)
83  static MsgLogger& Endmsg( MsgLogger& logger );
84 
85  // Accept stream modifiers
86  MsgLogger& operator<< ( MsgLogger& ( *_f )( MsgLogger& ) );
87  MsgLogger& operator<< ( std::ostream& ( *_f )( std::ostream& ) );
88  MsgLogger& operator<< ( std::ios& ( *_f )( std::ios& ) );
89 
90  // Accept message type specification
91  MsgLogger& operator<< ( EMsgType type );
92 
93  // For all the "conventional" inputs
94  template <class T> MsgLogger& operator<< ( T arg ) {
95  *(std::ostringstream*)this << arg;
96  return *this;
97  }
98 
99  // Temporally disables all the loggers (Caution! Use with care !)
100  static void InhibitOutput();
101  static void EnableOutput();
102 
103  private:
104 
105  // private utility routines
106  void Send();
107  void InitMaps();
108  void WriteMsg( EMsgType type, const std::string& line ) const;
109 
110  const TObject* fObjSource; // the source TObject (used for name)
111  std::string fStrSource; // alternative string source
112  static const std::string fgPrefix; // the prefix of the source name
113  static const std::string fgSuffix; // suffix following source name
114  EMsgType fActiveType; // active type
115  static const UInt_t fgMaxSourceSize; // maximum length of source name
116 #if __cplusplus > 199711L
117  static std::atomic<Bool_t> fgOutputSupressed; // disable the output globally (used by generic booster)
118  static std::atomic<Bool_t> fgInhibitOutput; // flag to suppress all output
119 
120  static std::atomic<const std::map<EMsgType, std::string>*> fgTypeMap; // matches output types with strings
121  static std::atomic<const std::map<EMsgType, std::string>*> fgColorMap; // matches output types with terminal colors
122 #else
123  static Bool_t fgOutputSupressed; // disable the output globally (used by generic booster)
124  static Bool_t fgInhibitOutput; // flag to suppress all output
125 
126  static const std::map<EMsgType, std::string>* fgTypeMap; // matches output types with strings
127  static const std::map<EMsgType, std::string>* fgColorMap; // matches output types with terminal colors
128 #endif
129  EMsgType fMinType; // minimum type for output
130 
131  ClassDef(MsgLogger,0) // Ostringstream derivative to redirect and format logging output
132  }; // class MsgLogger
133 
135  {
136  return (_f)(*this);
137  }
138 
139  inline MsgLogger& MsgLogger::operator<< ( std::ostream& (*_f)( std::ostream& ) )
140  {
141  (_f)(*this);
142  return *this;
143  }
144 
145  inline MsgLogger& MsgLogger::operator<< ( std::ios& ( *_f )( std::ios& ) )
146  {
147  (_f)(*this);
148  return *this;
149  }
150 
152  {
153  fActiveType = type;
154  return *this;
155  }
156 
157  // Shortcut
158  inline MsgLogger& Endl(MsgLogger& ml) { return MsgLogger::Endmsg(ml); }
159 
160 }
161 
162 #endif // TMVA_MsgLogger
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
MsgLogger & operator<<(MsgLogger &(*_f)(MsgLogger &))
Definition: MsgLogger.h:134
TLine * line
~MsgLogger()
destructor
Definition: MsgLogger.cxx:128
double T(double x)
Definition: ChebyshevPol.h:34
EMsgType fMinType
Definition: MsgLogger.h:129
static const std::string fgPrefix
Definition: MsgLogger.h:112
EMsgType GetMinType() const
Definition: MsgLogger.h:71
bool Bool_t
Definition: RtypesCore.h:59
void Send()
activates the logger writer
Definition: MsgLogger.cxx:184
const TObject * fObjSource
Definition: MsgLogger.h:110
static Bool_t fgInhibitOutput
Definition: MsgLogger.h:124
static void InhibitOutput()
Definition: MsgLogger.cxx:74
#define ClassDef(name, id)
Definition: Rtypes.h:326
static const std::map< EMsgType, std::string > * fgTypeMap
Definition: MsgLogger.h:126
static const UInt_t fgMaxSourceSize
Definition: MsgLogger.h:115
void SetMinType(EMsgType minType)
Definition: MsgLogger.h:72
static UInt_t GetMaxSourceSize()
Definition: MsgLogger.h:77
MsgLogger(const TObject *source, EMsgType minType=kINFO)
constructor
Definition: MsgLogger.cxx:79
static Bool_t fgOutputSupressed
Definition: MsgLogger.h:123
EMsgType fActiveType
Definition: MsgLogger.h:114
std::string GetPrintedSource() const
the full logger prefix
Definition: MsgLogger.cxx:172
MsgLogger & operator=(const MsgLogger &parent)
assignment operator
Definition: MsgLogger.cxx:135
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:219
void InitMaps()
Create the message type and color maps.
Definition: MsgLogger.cxx:269
unsigned int UInt_t
Definition: RtypesCore.h:42
static const std::string fgSuffix
Definition: MsgLogger.h:113
std::string fStrSource
Definition: MsgLogger.h:111
int type
Definition: TGX11.cxx:120
static MsgLogger & Endmsg(MsgLogger &logger)
end line
Definition: MsgLogger.cxx:260
std::string GetFormattedSource() const
make sure the source name is no longer than fgMaxSourceSize:
Definition: MsgLogger.cxx:150
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
static const std::map< EMsgType, std::string > * fgColorMap
Definition: MsgLogger.h:127
Mother of all ROOT objects.
Definition: TObject.h:37
void SetSource(const std::string &source)
Definition: MsgLogger.h:70
create variable transformations
std::string GetSource() const
Definition: MsgLogger.h:73
static void EnableOutput()
Definition: MsgLogger.cxx:75