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
0010 class Log {
0011 public:
0012
0013
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;
0023
0024
0025
0026 typedef std::map<std::string, Log> LogMap;
0027
0028
0029 typedef std::map<std::string, int> LevelMap;
0030
0031
0032 typedef std::map<int, std::string> ColorCodes;
0033
0034
0035 private:
0036
0037
0038 thread_local static LogMap existingLogs;
0039
0040
0041 thread_local static LevelMap defaultLevels;
0042
0043
0044 static bool showTimestamp;
0045
0046
0047 static bool showLogLevel;
0048
0049
0050 static bool showLoggerName;
0051
0052
0053 static bool useShellColors;
0054
0055
0056 public:
0057
0058
0059 static void setLevel(const std::string& name, int level);
0060 static void setLevels(const LevelMap& logLevels);
0061
0062 protected:
0063
0064
0065
0066
0067
0068 Log(const std::string& name);
0069
0070
0071 Log(const std::string& name, int level);
0072
0073
0074
0075
0076 static std::string getColorCode(int level);
0077
0078
0079 public:
0080
0081
0082
0083 static Log& getLog(const std::string& name);
0084
0085
0086 int getLevel() const {
0087 return _level;
0088 }
0089
0090
0091 Log& setLevel(int level) {
0092 _level = level;
0093 return *this;
0094 }
0095
0096
0097 static Level getLevelFromName(const std::string& level);
0098
0099
0100 static std::string getLevelName(int level);
0101
0102
0103 std::string getName() const {
0104 return _name;
0105 }
0106
0107
0108 Log& setName(const std::string& name) {
0109 _name = name;
0110 return *this;
0111 }
0112
0113
0114 bool isActive(int level) const {
0115 return (level >= _level);
0116 }
0117
0118
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
0137 std::string _name;
0138
0139
0140 int _level;
0141
0142 protected:
0143
0144
0145 void log(int level, const std::string& message);
0146
0147
0148 std::string formatMessage(int level, const std::string& message);
0149
0150 public:
0151
0152
0153 friend std::ostream& operator<<(Log& log, int level);
0154
0155 };
0156
0157
0158
0159 std::ostream& operator<<(Log& log, int level);
0160
0161
0162 }
0163
0164
0165
0166
0167
0168
0169
0170
0171 #define MSG_LVL(lvl, x) \
0172 do { \
0173 if (getLog().isActive(lvl)) { \
0174 getLog() << lvl << x << '\n'; \
0175 } \
0176 } while (0)
0177
0178
0179
0180 #define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
0181
0182 #define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
0183
0184
0185 #define MSG_INFO(x) MSG_LVL(Log::INFO, x)
0186
0187 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
0188
0189 #define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
0190
0191
0192
0193
0194 #endif