Rivet  1.8.3
Logging.hh
1 #ifndef RIVET_LOGGING_HH
2 #define RIVET_LOGGING_HH
3 
4 #include "Rivet/Rivet.hh"
5 
6 namespace Rivet {
7 
8 
9  class Log {
10  public:
11 
13  enum Level {
14  TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
15  };
16 
18  typedef std::map<std::string, Log*> LogMap;
19 
21  typedef std::map<std::string, int> LevelMap;
22 
24  typedef std::map<int, std::string> ColorCodes;
25 
26  private:
28  static LogMap existingLogs;
29 
31  static LevelMap defaultLevels;
32 
34  static ColorCodes colorCodes;
35 
37  static std::string endColorCode;
38 
40  static bool showTimestamp;
41 
43  static bool showLogLevel;
44 
46  static bool showLoggerName;
47 
49  static bool useShellColors;
50 
51  public:
53  static void setLevel(const std::string& name, int level);
54  static void setLevels(const LevelMap& logLevels);
55 
56  static void setShowTimestamp(bool showTime=true) {
57  showTimestamp = showTime;
58  }
59 
60  static void setShowLevel(bool showLevel=true) {
61  showLogLevel = showLevel;
62  }
63 
64  static void setShowLoggerName(bool showName=true) {
65  showLoggerName = showName;
66  }
67 
68  static void setUseColors(bool useColors=true) {
69  useShellColors = useColors;
70  }
71 
72  protected:
73 
75 
76 
78  Log(const std::string& name);
79 
81  Log(const std::string& name, int level);
82 
84 
85  static std::string getColorCode(int level);
86 
87  public:
88 
91  static Log& getLog(const std::string& name);
92 
93  public:
95  int getLevel() const {
96  return _level;
97  }
98 
100  Log& setLevel(int level) {
101  _level = level;
102  return *this;
103  }
104 
106  static Level getLevelFromName(const std::string& level);
107 
109  static std::string getLevelName(int level);
110 
112  std::string getName() const {
113  return _name;
114  }
115 
117  Log& setName(const std::string& name) {
118  _name = name;
119  return *this;
120  }
121 
123  bool isActive(int level) const {
124  return (level >= _level);
125  }
126 
128 
129  void trace(const std::string& message) { log(TRACE, message); }
130 
131  void debug(const std::string& message) { log(DEBUG, message); }
132 
133  void info(const std::string& message) { log(INFO, message); }
134 
135  void warn(const std::string& message) { log(WARN, message); }
136 
137  void error(const std::string& message) { log(ERROR, message); }
139 
140  private:
142  std::string _name;
143 
145  int _level;
146 
147  protected:
149  void log(int level, const std::string& message);
150 
152  std::string formatMessage(int level, const std::string& message);
153 
154  public:
155 
158  std::ostream* const _nostream;
159 
161  friend std::ostream& operator<<(Log& log, int level);
162 
163  };
164 
166  std::ostream& operator<<(Log& log, int level);
167 
168 }
169 
170 
171 // Neat CPU-conserving logging macros. Use by preference!
172 // NB. Only usable in classes where a getLog() method is provided
173 #define MSG_LVL(lvl, x) \
174  do { \
175  if (getLog().isActive(lvl)) { \
176  getLog() << lvl << x << endl; \
177  } \
178  } while (0)
179 
180 #define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
181 #define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
182 #define MSG_INFO(x) MSG_LVL(Log::INFO, x)
183 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
184 #define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
185 
186 
187 #endif
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
Definition: Logging.cc:136
std::string getName() const
Get the name of this logger.
Definition: Logging.hh:112
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition: Logging.hh:24
Log(const std::string &name)
Constructor 1.
Definition: Logging.cc:19
static std::string getLevelName(int level)
Get the std::string representation of a log level.
Definition: Logging.cc:89
static std::string getColorCode(int level)
Definition: Logging.cc:109
static Log & getLog(const std::string &name)
Definition: Logging.cc:55
void log(int level, const std::string &message)
Write a message at a particular level.
Definition: Logging.cc:182
Definition: Logging.hh:9
Log & setLevel(int level)
Set the priority level of this logger.
Definition: Logging.hh:100
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:117
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
Definition: Logging.cc:146
static void setLevel(const std::string &name, int level)
Set the log levels.
Definition: Logging.cc:40
Level
Log priority levels.
Definition: Logging.hh:13
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:95
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition: Logging.hh:21
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition: AnalysisInfo.hh:239
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition: Logging.hh:123
std::map< std::string, Log * > LogMap
Typedef for a collection of named logs.
Definition: Logging.hh:18