Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:52

0001 #ifndef RIVET_LOGGING_HH
0002 #define RIVET_LOGGING_HH
0003 
0004 #include "Rivet/Config/RivetCommon.hh"
0005 
0006 namespace Rivet {
0007 
0008 
0009   /// @brief Logging system for controlled & formatted writing to stdout
0010   class Log {
0011   public:
0012 
0013     /// Log priority levels.
0014     enum Level {
0015       TRACE = 0,
0016       DEBUG = 10,
0017       INFO = 20,
0018       WARN = 30, WARNING = 30,
0019       ERROR = 40,
0020       CRITICAL = 50, ALWAYS = 50
0021     };
0022     static const int END_COLOR = -10; ///< Special "level-like" code to end coloring
0023 
0024 
0025     /// Typedef for a collection of named logs.
0026     typedef std::map<std::string, Log> LogMap;
0027 
0028     /// Typedef for a collection of named log levels.
0029     typedef std::map<std::string, int> LevelMap;
0030 
0031     /// @brief Typedef for a collection of shell color codes, accessed by log level.
0032     typedef std::map<int, std::string> ColorCodes;
0033 
0034 
0035   private:
0036 
0037     /// A static map of existing logs: we don't make more loggers than necessary.
0038     thread_local static LogMap existingLogs;
0039 
0040     /// A static map of default log levels.
0041     thread_local static LevelMap defaultLevels;
0042 
0043     /// Show timestamp?
0044     static bool showTimestamp;
0045 
0046     /// Show log level?
0047     static bool showLogLevel;
0048 
0049     /// Show logger name?
0050     static bool showLoggerName;
0051 
0052     /// Use shell colour escape codes?
0053     static bool useShellColors;
0054 
0055 
0056   public:
0057 
0058     /// Set the log levels
0059     static void setLevel(const std::string& name, int level);
0060     static void setLevels(const LevelMap& logLevels);
0061 
0062   protected:
0063 
0064     /// @name Hidden constructors etc.
0065     /// @{
0066 
0067     /// Constructor 1
0068     Log(const std::string& name);
0069 
0070     /// Constructor 2
0071     Log(const std::string& name, int level);
0072 
0073     /// @}
0074 
0075     /// @brief Get the TTY code string for coloured messages
0076     static std::string getColorCode(int level);
0077 
0078 
0079   public:
0080 
0081     /// Get a logger with the given name. The level will be taken from the
0082     /// "requestedLevels" static map or will be INFO by default.
0083     static Log& getLog(const std::string& name);
0084 
0085     /// Get the priority level of this logger.
0086     int getLevel() const {
0087       return _level;
0088     }
0089 
0090     /// Set the priority level of this logger.
0091     Log& setLevel(int level) {
0092       _level = level;
0093       return *this;
0094     }
0095 
0096     /// Get a log level enum from a string.
0097     static Level getLevelFromName(const std::string& level);
0098 
0099     /// Get the std::string representation of a log level.
0100     static std::string getLevelName(int level);
0101 
0102     /// Get the name of this logger.
0103     std::string getName() const {
0104       return _name;
0105     }
0106 
0107     /// Set the name of this logger.
0108     Log& setName(const std::string& name) {
0109       _name = name;
0110       return *this;
0111     }
0112 
0113     /// Will this log level produce output on this logger at the moment?
0114     bool isActive(int level) const {
0115       return (level >= _level);
0116     }
0117 
0118     /// @name Explicit log methods
0119     /// @{
0120     void trace(const std::string& message) { log(TRACE, message); }
0121 
0122     void debug(const std::string& message) { log(DEBUG, message); }
0123 
0124     void info(const std::string& message) { log(INFO, message); }
0125 
0126     void warn(const std::string& message) { log(WARN, message); }
0127 
0128     void error(const std::string& message) { log(ERROR, message); }
0129 
0130     void critical(const std::string& message) { log(CRITICAL, message); }
0131     /// @}
0132 
0133 
0134   private:
0135 
0136     /// This logger's name
0137     std::string _name;
0138 
0139     /// Threshold level for this logger.
0140     int _level;
0141 
0142   protected:
0143 
0144     /// Write a message at a particular level.
0145     void log(int level, const std::string& message);
0146 
0147     /// Turn a message string into the current log format.
0148     std::string formatMessage(int level, const std::string& message);
0149 
0150   public:
0151 
0152     /// The streaming operator can use Log's internals.
0153     friend std::ostream& operator<<(Log& log, int level);
0154 
0155   };
0156 
0157 
0158   /// Streaming output to a logger must have a Log::Level/int as its first argument.
0159   std::ostream& operator<<(Log& log, int level);
0160 
0161 
0162 }
0163 
0164 
0165 /// @defgroup logmacros Logging macros
0166 /// @{
0167 
0168 /// @def MSG_LVL
0169 /// @brief Neat CPU-conserving logging macros. Use by preference!
0170 /// @note Only usable in classes where a getLog() method is provided
0171 #define MSG_LVL(lvl, x) \
0172   do { \
0173     if (getLog().isActive(lvl)) { \
0174       getLog() << lvl << x << '\n';   \
0175     } \
0176   } while (0)
0177 
0178 /// @def MSG_TRACE
0179 /// @brief Lowest-level, most verbose messaging, using MSG_LVL
0180 #define MSG_TRACE(x)   MSG_LVL(Log::TRACE, x)
0181 /// @brief Debug messaging, not enabled by default, using MSG_LVL
0182 #define MSG_DEBUG(x)   MSG_LVL(Log::DEBUG, x)
0183 /// @brief Key-information messging, enabled by default, using MSG_LVL
0184 /// @note Silence is golden: don't emit default-visible messages unless you *need* the user to see them
0185 #define MSG_INFO(x)    MSG_LVL(Log::INFO, x)
0186 /// @brief Warning messages for non-fatal bad things, using MSG_LVL
0187 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
0188 /// @brief Highest level messaging for serious problems, using MSG_LVL
0189 #define MSG_ERROR(x)   MSG_LVL(Log::ERROR, x)
0190 
0191 /// @}
0192 
0193 
0194 #endif