Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 10:18:59

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