Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Logging.hh
Go to the documentation of this file.
1 ///
2 /// @file Logging.hh
3 /// @brief Message logging routines
4 /// @brief based on Logging from Rivet v.2.1.1
5 ///
6 
7 //**** This file is a part of the HAMMER library
8 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
9 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
10 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
11 
12 // -*- C++ -*-
13 #ifndef HAMMER_LOGGING_HH
14 #define HAMMER_LOGGING_HH
15 
16 #include <string>
17 #include <map>
18 #include <iostream>
19 #include <mutex>
20 #include <memory>
21 
22 #include "Hammer/Exceptions.hh"
23 
24 namespace Hammer {
25 
26 
27  /// @brief Logging class
28  ///
29  /// Defines basic interface for logging and displaying messages
30  /// Manages a static dictionary of different named loggers
31  ///
32  /// @ingroup Tools
33  class Log {
34 
35  public:
36 
37  friend std::ostream& operator<<(Log& log, int level);
38 
39  public:
40 
41  /// @name Type definitions
42  //@{
43 
44  /// Log priority levels.
45  enum Level {
46  TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
47  };
48 
49  /// Typedef for a collection of named logs.
50  using LogMap = std::map<std::string, std::unique_ptr<Log>>;
51 
52  /// Typedef for a collection of named log levels.
53  using LevelMap = std::map<std::string, int>;
54 
55  /// Typedef for a counting the number of warnings in order to turn off warnings above a certain threshold
56  using WarningCountMap = std::map<std::string, int>;
57 
58  /// Typedef for a collection of shell color codes, accessed by log level.
59  using ColorCodes = std::map<int, std::string>;
60 
61  //@}
62 
63  private:
64 
65  /// @name Static data members access functions
66  //@{
67 
68  /// A static map of existing logs: we don't make more loggers than necessary.
69  /// @return the existing logs
70  static LogMap& existingLogs();
71 
72  /// A static map of default log levels.
73  /// @return the default levels
74  static LevelMap& defaultLevels();
75 
76  /// A static map for counting how many warnings have been issued for each logger
77  /// @return the warning counts
79 
80  /// A static map of shell color codes for the log levels.
81  /// @return the color codes
82  static ColorCodes& colorCodes();
83 
84  /// Shell color code for the end of the log levels.
85  /// @return the string
86  static std::string& endColorCode();
87 
88  /// Show timestamp?
89  static bool showTimestamp;
90 
91  /// Show log level?
92  static bool showLogLevel;
93 
94  /// Show logger name?
95  static bool showLoggerName;
96 
97  /// Use shell colour escape codes?
98  static bool useShellColors;
99 
100  protected:
101 
102  /// Mutex to access screen (protected because external streaming operator uses it)
103  /// @return the mutex
104  static std::mutex& displayLock();
105 
106  /// Mutex to modify global elements (protected because external streaming operator uses it)
107  /// @return the mutex
108  static std::mutex& lock();
109 
110  //@}
111 
112  public:
113 
114  /// @name Public static interface
115  //@{
116 
117  /// Get a logger with the given name. The level will be taken from the
118  /// "requestedLevels" static map or will be INFO by default.
119  /// @param[in] name the name of the logger
120  /// @return a reference to the logger
121  static Log& getLog(const std::string& name);
122 
123  /// Set the log levels
124  /// @param[in] name the name of the logger
125  /// @param[in] level the verbosity level
126  static void setLevel(const std::string& name, int level);
127 
128  /// Set the log levels all at once
129  /// @param[in] logLevels the verbosity level map
130  static void setLevels(const LevelMap& logLevels);
131 
132  /// toggle whether to include the timestamp in a logging message
133  /// @param[in] showTime the flag value
134  static void setShowTimestamp(bool showTime = true) {
135  std::lock_guard<std::mutex> guard(lock());
136  showTimestamp = showTime;
137  }
138 
139  /// toggle whether to display the verbosity level in a logging message
140  /// @param[in] showLevel the flag value
141  static void setShowLevel(bool showLevel = true) {
142  std::lock_guard<std::mutex> guard(lock());
143  showLogLevel = showLevel;
144  }
145 
146  /// toggle whether to display the logger name in a logging message
147  /// @param[in] showName the flag value
148  static void setShowLoggerName(bool showName = true) {
149  std::lock_guard<std::mutex> guard(lock());
150  showLoggerName = showName;
151  }
152 
153  /// toggle whether to display colorful logging messages
154  /// @param[in] useColors the flag value
155  static void setUseColors(bool useColors = true) {
156  std::lock_guard<std::mutex> guard(lock());
157  useShellColors = useColors;
158  }
159 
160  /// set the maximum number of warnings for a given logger
161  /// @param[in] name the logger name
162  /// @param[in] maxCount the maximum number of warnings
163  static void setWarningMaxCount(const std::string& name, int maxCount);
164 
165  /// reset the warning counters for all loggers
166  static void resetWarningCounters();
167 
168  //@}
169 
170  protected:
171 
172  /// @name Constructors
173  //@{
174 
175  /// Constructor by name
176  /// @param[in] name logger name
177  Log(const std::string& name);
178 
179  /// Constructor by name and verbosity level
180  /// @param[in] name logger name
181  /// @param[in] level verbosity level
182  Log(const std::string& name, int level);
183 
184  /// Constructor by name, verbosity level and warning count
185  /// @param[in] name logger name
186  /// @param[in] level verbosity level
187  /// @param[in] maxCount upper limit on the number of warnings
188  Log(const std::string& name, int level, int maxCount);
189 
190  //@}
191 
192  protected:
193 
194  /// @name Internal methods
195  //@{
196 
197  /// updates the verbosity levels of all the loggers
198  static void updateLevels();
199 
200  /// updates the max warning numbers of all the loggers
201  static void updateCounters();
202 
203  /// provide the escape string for the color associated to a give log level
204  /// @param[in] level the log level
205  /// @return the escape string
206  std::string getColorCode(int level);
207 
208  /// Get the maximum number of warnings for this logger.
209  /// @return the warning cutoff
210  int getMaxWarning() const {
211  return _maxWarning;
212  }
213 
214  /// Set the priority level of this logger.
215  /// @param[in] level the verbosity level
216  /// @return a reference to itself
217  Log& setLevel(int level) {
218  std::lock_guard<std::mutex> guard(lock());
219  _level = level;
220  return *this;
221  }
222 
223  /// Set the maximum number of warnings of this logger.
224  /// @param[in] numCount the number of warnings
225  /// @return a reference to itself
226  Log& setWarnCount(int numCount) {
227  std::lock_guard<std::mutex> guard(lock());
228  _maxWarning = numCount;
229  return *this;
230  }
231 
232  /// Get a log level enum from a string.
233  /// @param[in] level the level name
234  /// @return the level enum
235  static Level getLevelFromName(const std::string& level);
236 
237  /// Get the std::string representation of a log level.
238  /// @param[in] level the level enum
239  /// @return the level name
240  static std::string getLevelName(int level);
241 
242  /// Get the name of this logger.
243  /// @return the logger name
244  std::string getName() const {
245  return _name;
246  }
247 
248  /// Set the name of this logger.
249  /// @param[in] name the logger name
250  /// @return a reference to itself
251  Log& setName(const std::string& name) {
252  std::lock_guard<std::mutex> guard(lock());
253  _name = name;
254  return *this;
255  }
256 
257  /// Write a message at a particular level.
258  /// @param[in] level the log level
259  /// @param[in] message the log message
260  void log(int level, const std::string& message);
261 
262  /// Turn a message string into the current log format.
263  /// @param[in] level the log level
264  /// @param[in] message the log message
265  /// @return the formatted message
266  std::string formatMessage(int level, const std::string& message);
267 
268  //@}
269 
270  public:
271 
272  /// @name Non-static public interface
273  //@{
274 
275  /// Will this log level produce output on this logger at the moment?
276  bool isActive(int level) const {
277  return (level >= _level);
278  }
279 
280  /// log a trace message
281  /// @param[in] message the message text
282  void trace(const std::string& message) {
283  log(TRACE, message);
284  }
285 
286  /// log a debug message
287  /// @param[in] message the message text
288  void debug(const std::string& message) {
289  log(DEBUG, message);
290  }
291 
292  /// log an information message
293  /// @param[in] message the message text
294  void info(const std::string& message) {
295  log(INFO, message);
296  }
297 
298  /// log a warning message
299  /// @param[in] message the message text
300  void warn(const std::string& message) {
301  log(WARN, message);
302  }
303 
304  /// log an error message
305  /// @param[in] message the message text
306  void error(const std::string& message) {
307  log(ERROR, message);
308  }
309 
310  /// Get the priority level of this logger.
311  /// @return the verbosity level
312  int getLevel() const {
313  return _level;
314  }
315 
316  //@}
317 
318  protected:
319 
320  /// A null output stream, used for piping discarded output to nowhere.
321  std::unique_ptr<std::ostream> const _nostream;
322 
323  private:
324 
325  /// @name Data members
326  //@{
327 
328  /// Threshold level for this logger.
329  int _level;
330 
331  /// number of warnings issued by this logger.
333 
334  /// maximum number of warnings for this logger.
336 
337  /// This logger's name
338  std::string _name;
339 
340  //@}
341 
342  };
343 
344  /// Streaming output to a logger must have a Level/int as its first argument.
345  /// @param[in] log the logger
346  /// @param[in] level the log level
347  /// @return the output stream
348  /// @ingroup Tools
349  std::ostream& operator<<(Log& log, int level);
350 
351 }
352 
353 
354 // Neat CPU-conserving logging macros. Use by preference!
355 // NB. Only usable in classes where a getLog() method is provided
356 #define MSG_LVL(lvl, x) \
357  do { \
358  if (getLog().isActive(lvl)) { \
359  getLog() << lvl << x << std::endl; \
360  } \
361  } while (0)
362 
363 #define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
364 #define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
365 #define MSG_INFO(x) MSG_LVL(Log::INFO, x)
366 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
367 #define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
368 
369 
370 #endif
Log & setLevel(int level)
Set the priority level of this logger.
Definition: Logging.hh:217
void error(const std::string &message)
log an error message
Definition: Logging.hh:306
static void setShowLevel(bool showLevel=true)
toggle whether to display the verbosity level in a logging message
Definition: Logging.hh:141
static std::string & endColorCode()
Shell color code for the end of the log levels.
Definition: Logging.cc:44
int _level
Threshold level for this logger.
Definition: Logging.hh:329
static WarningCountMap & defaultMaxWarnings()
A static map for counting how many warnings have been issued for each logger.
Definition: Logging.cc:34
std::map< std::string, std::unique_ptr< Log >> LogMap
Typedef for a collection of named logs.
Definition: Logging.hh:50
static void setShowTimestamp(bool showTime=true)
toggle whether to include the timestamp in a logging message
Definition: Logging.hh:134
Log & setWarnCount(int numCount)
Set the maximum number of warnings of this logger.
Definition: Logging.hh:226
void trace(const std::string &message)
log a trace message
Definition: Logging.hh:282
std::unique_ptr< std::ostream > const _nostream
A null output stream, used for piping discarded output to nowhere.
Definition: Logging.hh:321
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
static void setShowLoggerName(bool showName=true)
toggle whether to display the logger name in a logging message
Definition: Logging.hh:148
static bool showTimestamp
Show timestamp?
Definition: Logging.hh:89
static void updateLevels()
updates the verbosity levels of all the loggers
Definition: Logging.cc:66
void warn(const std::string &message)
log a warning message
Definition: Logging.hh:300
static void resetWarningCounters()
reset the warning counters for all loggers
Definition: Logging.cc:132
std::string getColorCode(int level)
provide the escape string for the color associated to a give log level
Definition: Logging.cc:216
static LevelMap & defaultLevels()
A static map of default log levels.
Definition: Logging.cc:29
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
Definition: Logging.cc:249
std::string getName() const
Get the name of this logger.
Definition: Logging.hh:244
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition: Logging.hh:276
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
Hammer exception definitions.
int getMaxWarning() const
Get the maximum number of warnings for this logger.
Definition: Logging.hh:210
Log & setName(const std::string &name)
Set the name of this logger.
Definition: Logging.hh:251
static bool showLogLevel
Show log level?
Definition: Logging.hh:92
int _warnCounter
number of warnings issued by this logger.
Definition: Logging.hh:332
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition: Logging.hh:53
void info(const std::string &message)
log an information message
Definition: Logging.hh:294
Logging class.
Definition: Logging.hh:33
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
Definition: Logging.cc:274
static void updateCounters()
updates the max warning numbers of all the loggers
Definition: Logging.cc:76
void log(int level, const std::string &message)
Write a message at a particular level.
Definition: Logging.cc:310
static std::mutex & lock()
Mutex to modify global elements (protected because external streaming operator uses it) ...
Definition: Logging.cc:49
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition: Logging.hh:59
static void setLevels(const LevelMap &logLevels)
Set the log levels all at once.
Definition: Logging.cc:113
static std::string getLevelName(int level)
Get the std::string representation of a log level.
Definition: Logging.cc:190
std::map< std::string, int > WarningCountMap
Typedef for a counting the number of warnings in order to turn off warnings above a certain threshold...
Definition: Logging.hh:56
Level
Log priority levels.
Definition: Logging.hh:45
static void setWarningMaxCount(const std::string &name, int maxCount)
set the maximum number of warnings for a given logger
Definition: Logging.cc:124
int _maxWarning
maximum number of warnings for this logger.
Definition: Logging.hh:335
static bool useShellColors
Use shell colour escape codes?
Definition: Logging.hh:98
void debug(const std::string &message)
log a debug message
Definition: Logging.hh:288
static ColorCodes & colorCodes()
A static map of shell color codes for the log levels.
Definition: Logging.cc:39
static LogMap & existingLogs()
A static map of existing logs: we don&#39;t make more loggers than necessary.
Definition: Logging.cc:24
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:312
friend std::ostream & operator<<(Log &log, int level)
Streaming output to a logger must have a Level/int as its first argument.
Definition: Logging.cc:334
static bool showLoggerName
Show logger name?
Definition: Logging.hh:95
static void setUseColors(bool useColors=true)
toggle whether to display colorful logging messages
Definition: Logging.hh:155
std::string _name
This logger&#39;s name.
Definition: Logging.hh:338
static std::mutex & displayLock()
Mutex to access screen (protected because external streaming operator uses it)
Definition: Logging.cc:54
static void setLevel(const std::string &name, int level)
Set the log levels.
Definition: Logging.cc:105
Log(const std::string &name)
Constructor by name.
Definition: Logging.cc:95