Rivet API documentation

Rivet 4.1.3
Logging.hh
1#ifndef RIVET_LOGGING_HH
2#define RIVET_LOGGING_HH
3
4#include "Rivet/Config/RivetCommon.hh"
5
6namespace Rivet {
7
8
10 class Log {
11 public:
12
14 enum Level {
15 TRACE = 0,
16 DEBUG = 10,
17 INFO = 20,
18 WARN = 30,
19 WARNING = 30,
20 ERROR = 40,
21 CRITICAL = 50,
22 ALWAYS = 50
23 };
24 static const int END_COLOR = -10;
25
26
28 typedef std::map<std::string, Log> LogMap;
29
31 typedef std::map<std::string, int> LevelMap;
32
34 typedef std::map<int, std::string> ColorCodes;
35
36
37 private:
38
40 thread_local static LogMap existingLogs;
41
43 thread_local static LevelMap defaultLevels;
44
46 static bool showTimestamp;
47
49 static bool showLogLevel;
50
52 static bool showLoggerName;
53
55 static bool useShellColors;
56
57
58 public:
59
61 static void setLevel(const std::string& name, int level);
62 static void setLevels(const LevelMap& logLevels);
63
64 protected:
65
68
70 Log(const std::string& name);
71
73 Log(const std::string& name, int level);
74
76
78 static std::string getColorCode(int level);
79
80
81 public:
82
85 static Log& getLog(const std::string& name);
86
88 int getLevel() const {
89 return _level;
90 }
91
93 Log& setLevel(int level) {
94 _level = level;
95 return *this;
96 }
97
99 static Level getLevelFromName(const std::string& level);
100
102 static std::string getLevelName(int level);
103
105 std::string getName() const {
106 return _name;
107 }
108
110 Log& setName(const std::string& name) {
111 _name = name;
112 return *this;
113 }
114
116 bool isActive(int level) const {
117 return (level >= _level);
118 }
119
122 void trace(const std::string& message) {
123 log(TRACE, message);
124 }
125
126 void debug(const std::string& message) {
127 log(DEBUG, message);
128 }
129
130 void info(const std::string& message) {
131 log(INFO, message);
132 }
133
134 void warn(const std::string& message) {
135 log(WARN, message);
136 }
137
138 void error(const std::string& message) {
139 log(ERROR, message);
140 }
141
142 void critical(const std::string& message) {
143 log(CRITICAL, message);
144 }
146
147
148 private:
149
151 std::string _name;
152
154 int _level;
155
156 protected:
157
159 void log(int level, const std::string& message);
160
162 std::string formatMessage(int level, const std::string& message);
163
164 public:
165
167 friend std::ostream& operator<<(Log& log, int level);
168 };
169
170
172 std::ostream& operator<<(Log& log, int level);
173
174
175}
176
177
180
184#define MSG_LVL(lvl, x) \
185 do { \
186 if (getLog().isActive(lvl)) { \
187 getLog() << lvl << x << '\n'; \
188 } \
189 } while (0)
190
193#define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
195#define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
198#define MSG_INFO(x) MSG_LVL(Log::INFO, x)
200#define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
202#define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
203
205
206
207#endif
Logging system for controlled & formatted writing to stdout.
Definition Logging.hh:10
void log(int level, const std::string &message)
Write a message at a particular level.
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition Logging.hh:116
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition Logging.hh:34
Log(const std::string &name)
Constructor 1.
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
std::string getName() const
Get the name of this logger.
Definition Logging.hh:105
Level
Log priority levels.
Definition Logging.hh:14
Log(const std::string &name, int level)
Constructor 2.
Log & setName(const std::string &name)
Set the name of this logger.
Definition Logging.hh:110
static void setLevel(const std::string &name, int level)
Set the log levels.
static std::string getLevelName(int level)
Get the std::string representation of a log level.
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
Log & setLevel(int level)
Set the priority level of this logger.
Definition Logging.hh:93
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
Definition Logging.hh:28
static Log & getLog(const std::string &name)
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition Logging.hh:31
static const int END_COLOR
Special "level-like" code to end coloring.
Definition Logging.hh:24
int getLevel() const
Get the priority level of this logger.
Definition Logging.hh:88
static std::string getColorCode(int level)
Get the TTY code string for coloured messages.
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log's internals.
Definition LHCbCommon.hh:9
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:463