rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
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 
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 
27  private:
28 
30  static LogMap existingLogs;
31 
33  static LevelMap defaultLevels;
34 
36  static ColorCodes colorCodes;
37 
39  static std::string endColorCode;
40 
42  static bool showTimestamp;
43 
45  static bool showLogLevel;
46 
48  static bool showLoggerName;
49 
51  static bool useShellColors;
52 
53 
54  public:
55 
57  static void setLevel(const std::string& name, int level);
58  static void setLevels(const LevelMap& logLevels);
59 
60  static void setShowTimestamp(bool showTime=true) {
61  showTimestamp = showTime;
62  }
63 
64  static void setShowLevel(bool showLevel=true) {
65  showLogLevel = showLevel;
66  }
67 
68  static void setShowLoggerName(bool showName=true) {
69  showLoggerName = showName;
70  }
71 
72  static void setUseColors(bool useColors=true) {
73  useShellColors = useColors;
74  }
75 
76 
77  protected:
78 
80 
81 
83  Log(const std::string& name);
84 
86  Log(const std::string& name, int level);
87 
89 
90  static std::string getColorCode(int level);
91 
92 
93  public:
94 
97  static Log& getLog(const std::string& name);
98 
100  int getLevel() const {
101  return _level;
102  }
103 
105  Log& setLevel(int level) {
106  _level = level;
107  return *this;
108  }
109 
111  static Level getLevelFromName(const std::string& level);
112 
114  static std::string getLevelName(int level);
115 
117  std::string getName() const {
118  return _name;
119  }
120 
122  Log& setName(const std::string& name) {
123  _name = name;
124  return *this;
125  }
126 
128  bool isActive(int level) const {
129  return (level >= _level);
130  }
131 
133 
134  void trace(const std::string& message) { log(TRACE, message); }
135 
136  void debug(const std::string& message) { log(DEBUG, message); }
137 
138  void info(const std::string& message) { log(INFO, message); }
139 
140  void warn(const std::string& message) { log(WARN, message); }
141 
142  void error(const std::string& message) { log(ERROR, message); }
144 
145 
146  private:
147 
149  std::string _name;
150 
152  int _level;
153 
154  protected:
155 
157  void log(int level, const std::string& message);
158 
160  std::string formatMessage(int level, const std::string& message);
161 
162  public:
163 
165  friend std::ostream& operator<<(Log& log, int level);
166 
167  };
168 
169 
171  std::ostream& operator<<(Log& log, int level);
172 
173 
174 }
175 
176 
177 // Neat CPU-conserving logging macros. Use by preference!
178 // NB. Only usable in classes where a getLog() method is provided
179 #define MSG_LVL(lvl, x) \
180  do { \
181  if (getLog().isActive(lvl)) { \
182  getLog() << lvl << x << endl; \
183  } \
184  } while (0)
185 
186 #define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
187 #define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
188 #define MSG_INFO(x) MSG_LVL(Log::INFO, x)
189 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
190 #define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
191 
192 
193 #endif
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
Definition: Logging.cc:140
Definition: ALICE_2010_I880049.cc:13
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:93
static std::string getColorCode(int level)
Definition: Logging.cc:113
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:186
Definition: Logging.hh:9
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition: Logging.hh:128
Log & setLevel(int level)
Set the priority level of this logger.
Definition: Logging.hh:105
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log&#39;s internals.
Definition: Logging.cc:193
Log & setName(const std::string &name)
Set the name of this logger.
Definition: Logging.hh:122
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:100
std::map< std::string, Log > LogMap
Typedef for a collection of named logs.
Definition: Logging.hh:18
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
Definition: Logging.cc:150
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
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition: Logging.hh:21
std::string getName() const
Get the name of this logger.
Definition: Logging.hh:117