Rivet  3.1.4
Logging.hh
1 #ifndef RIVET_LOGGING_HH
2 #define RIVET_LOGGING_HH
3 
4 #include "Rivet/Config/RivetCommon.hh"
5 
6 namespace Rivet {
7 
8 
10  class Log {
11  public:
12 
14  enum Level {
15  TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
16  };
17 
19  typedef std::map<std::string, Log> LogMap;
20 
22  typedef std::map<std::string, int> LevelMap;
23 
25  typedef std::map<int, std::string> ColorCodes;
26 
27 
28  private:
29 
31  static LogMap existingLogs;
32 
34  static LevelMap defaultLevels;
35 
37  static ColorCodes colorCodes;
38 
40  static std::string endColorCode;
41 
43  static bool showTimestamp;
44 
46  static bool showLogLevel;
47 
49  static bool showLoggerName;
50 
52  static bool useShellColors;
53 
54 
55  public:
56 
58  static void setLevel(const std::string& name, int level);
59  static void setLevels(const LevelMap& logLevels);
60 
61  static void setShowTimestamp(bool showTime=true) {
62  showTimestamp = showTime;
63  }
64 
65  static void setShowLevel(bool showLevel=true) {
66  showLogLevel = showLevel;
67  }
68 
69  static void setShowLoggerName(bool showName=true) {
70  showLoggerName = showName;
71  }
72 
73  static void setUseColors(bool useColors=true) {
74  useShellColors = useColors;
75  }
76 
77 
78  protected:
79 
81 
82 
84  Log(const std::string& name);
85 
87  Log(const std::string& name, int level);
88 
90 
91  static std::string getColorCode(int level);
92 
93 
94  public:
95 
98  static Log& getLog(const std::string& name);
99 
101  int getLevel() const {
102  return _level;
103  }
104 
106  Log& setLevel(int level) {
107  _level = level;
108  return *this;
109  }
110 
112  static Level getLevelFromName(const std::string& level);
113 
115  static std::string getLevelName(int level);
116 
118  std::string getName() const {
119  return _name;
120  }
121 
123  Log& setName(const std::string& name) {
124  _name = name;
125  return *this;
126  }
127 
129  bool isActive(int level) const {
130  return (level >= _level);
131  }
132 
134 
135  void trace(const std::string& message) { log(TRACE, message); }
136 
137  void debug(const std::string& message) { log(DEBUG, message); }
138 
139  void info(const std::string& message) { log(INFO, message); }
140 
141  void warn(const std::string& message) { log(WARN, message); }
142 
143  void error(const std::string& message) { log(ERROR, message); }
145 
146 
147  private:
148 
150  std::string _name;
151 
153  int _level;
154 
155  protected:
156 
158  void log(int level, const std::string& message);
159 
161  std::string formatMessage(int level, const std::string& message);
162 
163  public:
164 
166  friend std::ostream& operator<<(Log& log, int level);
167 
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)
194 #define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
196 #define MSG_INFO(x) MSG_LVL(Log::INFO, x)
199 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
201 #define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
203 
205 
206 
207 #endif
Definition: MC_Cent_pPb.hh:10
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition: Logging.hh:25
static Log & getLog(const std::string &name)
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.
void log(int level, const std::string &message)
Write a message at a particular level.
Logging system for controlled & formatted writing to stdout.
Definition: Logging.hh:10
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition: Logging.hh:129
static std::string getLevelName(int level)
Get the std::string representation of a log level.
Log & setLevel(int level)
Set the priority level of this logger.
Definition: Logging.hh:106
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log&#39;s internals.
Log & setName(const std::string &name)
Set the name of this logger.
Definition: Logging.hh:123
static void setLevel(const std::string &name, int level)
Set the log levels.
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:101
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
Definition: Logging.hh:19
Level
Log priority levels.
Definition: Logging.hh:14
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition: Logging.hh:22
std::string getName() const
Get the name of this logger.
Definition: Logging.hh:118
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.