Logging.hh
#ifndef RIVET_LOGGING_HH
#define RIVET_LOGGING_HH
#include "Rivet/Rivet.hh"
namespace Rivet {
class Log {
public:
enum Level {
TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
};
typedef std::map<std::string, Log*> LogMap;
typedef std::map<std::string, int> LevelMap;
typedef std::map<int, std::string> ColorCodes;
private:
static LogMap existingLogs;
static LevelMap defaultLevels;
static ColorCodes colorCodes;
static std::string endColorCode;
static bool showTimestamp;
static bool showLogLevel;
static bool showLoggerName;
static bool useShellColors;
public:
static void setLevel(const std::string& name, int level);
static void setLevels(const LevelMap& logLevels);
static void setShowTimestamp(bool showTime=true) {
showTimestamp = showTime;
}
static void setShowLevel(bool showLevel=true) {
showLogLevel = showLevel;
}
static void setShowLoggerName(bool showName=true) {
showLoggerName = showName;
}
static void setUseColors(bool useColors=true) {
useShellColors = useColors;
}
protected:
Log(const std::string& name);
Log(const std::string& name, int level);
static std::string getColorCode(int level);
public:
static Log& getLog(const std::string& name);
public:
int getLevel() const {
return _level;
}
Log& setLevel(int level) {
_level = level;
return *this;
}
static Level getLevelFromName(const std::string& level);
static std::string getLevelName(int level);
std::string getName() const {
return _name;
}
Log& setName(const std::string& name) {
_name = name;
return *this;
}
bool isActive(int level) const {
return (level >= _level);
}
void trace(const std::string& message) { log(TRACE, message); }
void debug(const std::string& message) { log(DEBUG, message); }
void info(const std::string& message) { log(INFO, message); }
void warn(const std::string& message) { log(WARN, message); }
void error(const std::string& message) { log(ERROR, message); }
private:
std::string _name;
int _level;
protected:
void log(int level, const std::string& message);
std::string formatMessage(int level, const std::string& message);
public:
std::ostream* const _nostream;
friend std::ostream& operator<<(Log& log, int level);
};
std::ostream& operator<<(Log& log, int level);
}
// Neat CPU-conserving logging macros. Use by preference!
// NB. Only usable in classes where a getLog() method is provided
#define MSG_LVL(lvl, x) \
do { \
if (getLog().isActive(lvl)) { \
getLog() << lvl << x << endl; \
} \
} while (0)
#define MSG_TRACE(x) MSG_LVL(Log::TRACE, x)
#define MSG_DEBUG(x) MSG_LVL(Log::DEBUG, x)
#define MSG_INFO(x) MSG_LVL(Log::INFO, x)
#define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
#define MSG_ERROR(x) MSG_LVL(Log::ERROR, x)
#endif
Rivet::Log::getLevelFromName
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
Definition: Logging.cc:136
Rivet
Definition: MC_JetAnalysis.hh:9
Rivet::Log::getName
std::string getName() const
Get the name of this logger.
Definition: Logging.hh:112
Rivet::Log::ColorCodes
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
Definition: Logging.hh:24
Rivet::Log::Log
Log(const std::string &name)
Constructor 1.
Definition: Logging.cc:19
Rivet::Log::getLevelName
static std::string getLevelName(int level)
Get the std::string representation of a log level.
Definition: Logging.cc:89
Rivet::Log::getColorCode
static std::string getColorCode(int level)
Definition: Logging.cc:109
Rivet::Log::getLog
static Log & getLog(const std::string &name)
Definition: Logging.cc:55
Rivet::Log::log
void log(int level, const std::string &message)
Write a message at a particular level.
Definition: Logging.cc:182
Rivet::Log
Definition: Logging.hh:9
Rivet::Log::setLevel
Log & setLevel(int level)
Set the priority level of this logger.
Definition: Logging.hh:100
Rivet::Log::operator<<
friend std::ostream & operator<<(Log &log, int level)
The streaming operator can use Log's internals.
Definition: Logging.cc:189
Rivet::Log::setName
Log & setName(const std::string &name)
Set the name of this logger.
Definition: Logging.hh:117
Rivet::Log::formatMessage
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
Definition: Logging.cc:146
Rivet::Log::setLevel
static void setLevel(const std::string &name, int level)
Set the log levels.
Definition: Logging.cc:40
Rivet::Log::Level
Level
Log priority levels.
Definition: Logging.hh:13
Rivet::Log::getLevel
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:95
Rivet::Log::LevelMap
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
Definition: Logging.hh:21
Rivet::operator<<
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition: AnalysisInfo.hh:239
Rivet::Log::isActive
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
Definition: Logging.hh:123
Rivet::Log::LogMap
std::map< std::string, Log * > LogMap
Typedef for a collection of named logs.
Definition: Logging.hh:18