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