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