rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.0
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, WARNING = 30,
19 ERROR = 40,
20 CRITICAL = 50, ALWAYS = 50
21 };
22 static const int END_COLOR = -10;
23
24
26 typedef std::map<std::string, Log> LogMap;
27
29 typedef std::map<std::string, int> LevelMap;
30
32 typedef std::map<int, std::string> ColorCodes;
33
34
35 private:
36
38 thread_local static LogMap existingLogs;
39
41 thread_local static LevelMap defaultLevels;
42
44 static bool showTimestamp;
45
47 static bool showLogLevel;
48
50 static bool showLoggerName;
51
53 static bool useShellColors;
54
55
56 public:
57
59 static void setLevel(const std::string& name, int level);
60 static void setLevels(const LevelMap& logLevels);
61
62 protected:
63
66
68 Log(const std::string& name);
69
71 Log(const std::string& name, int level);
72
74
76 static std::string getColorCode(int level);
77
78
79 public:
80
83 static Log& getLog(const std::string& name);
84
86 int getLevel() const {
87 return _level;
88 }
89
91 Log& setLevel(int level) {
92 _level = level;
93 return *this;
94 }
95
97 static Level getLevelFromName(const std::string& level);
98
100 static std::string getLevelName(int level);
101
103 std::string getName() const {
104 return _name;
105 }
106
108 Log& setName(const std::string& name) {
109 _name = name;
110 return *this;
111 }
112
114 bool isActive(int level) const {
115 return (level >= _level);
116 }
117
120 void trace(const std::string& message) { log(TRACE, message); }
121
122 void debug(const std::string& message) { log(DEBUG, message); }
123
124 void info(const std::string& message) { log(INFO, message); }
125
126 void warn(const std::string& message) { log(WARN, message); }
127
128 void error(const std::string& message) { log(ERROR, message); }
129
130 void critical(const std::string& message) { log(CRITICAL, message); }
132
133
134 private:
135
137 std::string _name;
138
140 int _level;
141
142 protected:
143
145 void log(int level, const std::string& message);
146
148 std::string formatMessage(int level, const std::string& message);
149
150 public:
151
153 friend std::ostream& operator<<(Log& log, int level);
154
155 };
156
157
159 std::ostream& operator<<(Log& log, int level);
160
161
162}
163
164
167
171#define MSG_LVL(lvl, x) \
172 do { \
173 if (getLog().isActive(lvl)) { \
174 getLog() << lvl << x << '\n'; \
175 } \
176 } while (0)
177
180#define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
182#define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
185#define MSG_INFO(x) MSG_LVL(Log::INFO, x)
187#define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
189#define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
190
192
193
194#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:114
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition Logging.hh:32
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:103
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:108
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:91
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
Definition Logging.hh:26
static Log & getLog(const std::string &name)
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition Logging.hh:29
static const int END_COLOR
Special "level-like" code to end coloring.
Definition Logging.hh:22
int getLevel() const
Get the priority level of this logger.
Definition Logging.hh:86
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 MC_CENT_PPB_Projections.hh:10
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:362