ATLAS Offline Software
Loading...
Searching...
No Matches
TMsgLogger.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5/**********************************************************************************
6 * Class : TMsgLogger *
7 * *
8 * Authors (alphabetical): *
9 * Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN, Switzerland *
10 **********************************************************************************/
11
12#ifndef ROOT_Root_TMsgLogger
13#define ROOT_Root_TMsgLogger
14
16// //
17// TMsgLogger //
18// //
19// ostreamstream derivative to redirect and format output //
20// //
22
23// STL include(s):
24#include <string>
25#include <sstream>
26#include <map>
27
28// ROOT include(s)
29#include "TObject.h"
30#include "TString.h"
31
32// Local include(s):
33
34namespace Root {
35
36 // define outside of class to facilite access
37 enum TMsgLevel {
39 kDEBUG = 2,
40 kINFO = 3,
42 kERROR = 5,
43 kFATAL = 6,
45 };
46
47 class TMsgLogger : public std::ostringstream, public TObject {
48
49 public:
50
51 TMsgLogger( const TObject* source, TMsgLevel minLevel = kINFO );
52 TMsgLogger( const std::string& source, TMsgLevel minLevel = kINFO );
53 TMsgLogger( TMsgLevel minLevel = kINFO );
54 TMsgLogger( const TMsgLogger& parent );
55 virtual ~TMsgLogger();
56
57 // Accessors
58 void SetSource( const char* source ) { m_strSource = source; }
59 void SetSource( const std::string& source ) { m_strSource = source; }
60 void SetSource( const TString& source ) { m_strSource = source.Data(); }
61 const std::string& GetSource() const { return m_strSource; }
62
63 UInt_t GetMaxSourceSize() const { return (UInt_t)m_maxSourceSize; }
64 std::string GetPrintedSource() const;
65 std::string GetFormattedSource() const;
66
67 TMsgLevel GetMinLevel() const { return m_minLevel; }
68 const std::string& GetMinLevelStr() const { return m_levelMap.find( m_minLevel )->second; }
69
70 TMsgLevel MapLevel( const TString& instr ) const;
71
72 // Needed for copying
73 TMsgLogger& operator= ( const TMsgLogger& parent );
74
75 // Stream modifier(s)
77
78 // Accept stream modifiers
80 TMsgLogger& operator<< ( std::ostream& ( *_f )( std::ostream& ) );
81 TMsgLogger& operator<< ( std::ios& ( *_f )( std::ios& ) );
82
83 // Accept message level specification
85
86 // For all the "conventional" inputs
87 template <class T> TMsgLogger& operator<< ( T arg ) {
88 *static_cast<std::ostringstream*>(this) << arg;
89 return *this;
90 }
91
92 static void SetMinLevel( TMsgLevel minLevel ) { m_minLevel = minLevel; }
93
94 private:
95
96 // the current minimum level is global for the whole Root
97 // it can only be changed by the central storage singleton object
98 // we make this atomic to suppress thread-checker warnings
99 static std::atomic<TMsgLevel> m_minLevel; // minimum level for logging output
100
101 // private utility routines
102 void Send();
103 void InitMaps();
104 void WriteMsg( TMsgLevel level, const std::string& line ) const;
105
106 const TObject* m_objSource; // the source TObject (used for name)
107 std::string m_strSource; // alternative string source
108 const std::string m_prefix; // the prefix of the source name
109 const std::string m_suffix; // suffix following source name
110 TMsgLevel m_activeLevel; // active level
111 const std::string::size_type m_maxSourceSize; // maximum length of source name
112
113 std::map<TMsgLevel, std::string> m_levelMap; // matches output levels with strings
114 std::map<TMsgLevel, std::string> m_colorMap; // matches output levels with terminal colors
115
116 //ClassDef(TMsgLogger,0)
117
118 }; // class TMsgLogger
119
121 {
122 return (_f)(*this);
123 }
124
125 inline TMsgLogger& TMsgLogger::operator<< ( std::ostream& (*_f)( std::ostream& ) )
126 {
127 (_f)(*this);
128 return *this;
129 }
130
131 inline TMsgLogger& TMsgLogger::operator<< ( std::ios& ( *_f )( std::ios& ) )
132 {
133 (_f)(*this);
134 return *this;
135 }
136
138 {
139 m_activeLevel = level;
140 return *this;
141 }
142
143 // Although the proper definition of "Endl" as a function pointer
144 // would be nicer C++-wise, it introduces some "unused variable"
145 // warnings so let's use the #define definition after all...
146 // [ static TMsgLogger& ( *Endl )( TMsgLogger& ) = &TMsgLogger::endmsg; ]
147#define GEndl TMsgLogger::endmsg
148
149}
150
151#endif // Root_TMsgLogger
#define endmsg
virtual ~TMsgLogger()
static std::atomic< TMsgLevel > m_minLevel
Definition TMsgLogger.h:99
TMsgLogger(const TObject *source, TMsgLevel minLevel=kINFO)
const std::string m_prefix
Definition TMsgLogger.h:108
std::string GetFormattedSource() const
TMsgLevel MapLevel(const TString &instr) const
const std::string & GetSource() const
Definition TMsgLogger.h:61
const std::string & GetMinLevelStr() const
Definition TMsgLogger.h:68
TMsgLogger & operator=(const TMsgLogger &parent)
UInt_t GetMaxSourceSize() const
Definition TMsgLogger.h:63
void WriteMsg(TMsgLevel level, const std::string &line) const
TMsgLogger & operator<<(TMsgLogger &(*_f)(TMsgLogger &))
Definition TMsgLogger.h:120
std::map< TMsgLevel, std::string > m_levelMap
Definition TMsgLogger.h:113
const std::string m_suffix
Definition TMsgLogger.h:109
void SetSource(const TString &source)
Definition TMsgLogger.h:60
void SetSource(const std::string &source)
Definition TMsgLogger.h:59
std::map< TMsgLevel, std::string > m_colorMap
Definition TMsgLogger.h:114
static void SetMinLevel(TMsgLevel minLevel)
Definition TMsgLogger.h:92
const TObject * m_objSource
Definition TMsgLogger.h:106
std::string m_strSource
Definition TMsgLogger.h:107
TMsgLevel GetMinLevel() const
Definition TMsgLogger.h:67
std::string GetPrintedSource() const
TMsgLevel m_activeLevel
Definition TMsgLogger.h:110
const std::string::size_type m_maxSourceSize
Definition TMsgLogger.h:111
void SetSource(const char *source)
Definition TMsgLogger.h:58
static Root::TMsgLogger logger("iLumiCalc")
TMsgLevel
Definition TMsgLogger.h:37
@ kALWAYS
Definition TMsgLogger.h:44
@ kERROR
Definition TMsgLogger.h:42
@ kWARNING
Definition TMsgLogger.h:41
@ kDEBUG
Definition TMsgLogger.h:39
@ kFATAL
Definition TMsgLogger.h:43
@ kVERBOSE
Definition TMsgLogger.h:38
@ kINFO
Definition TMsgLogger.h:40