21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wexit-time-destructors"
44 string& Log::endColorCode() {
54 mutex& Log::displayLock() {
59 #pragma clang diagnostic pop
61 bool Log::showTimestamp =
false;
62 bool Log::showLogLevel =
true;
63 bool Log::showLoggerName =
true;
64 bool Log::useShellColors =
true;
66 void Log::updateLevels() {
67 for (LevelMap::const_iterator lev = defaultLevels().
begin(); lev != defaultLevels().end(); ++lev) {
68 for (LogMap::iterator log = existingLogs().begin(); log != existingLogs().end(); ++log) {
69 if (log->first.find(lev->first) == 0) {
70 log->second->setLevel(lev->second);
76 void Log::updateCounters() {
77 if(defaultMaxWarnings().size() == 0) {
78 defaultMaxWarnings()[
"Default"] = 10;
80 for (WarningCountMap::const_iterator lev = defaultMaxWarnings().
begin(); lev != defaultMaxWarnings().end(); ++lev) {
81 if (lev->first.find(
"Default") == 0) {
84 for (LogMap::iterator log = existingLogs().begin(); log != existingLogs().end(); ++log) {
85 if (log->first.find(lev->first) == 0) {
86 log->second->setWarnCount(lev->second);
89 log->second->setWarnCount(10);
95 Log::Log(
const string& name)
96 : _nostream(new ostream(nullptr)), _level(INFO), _warnCounter(0), _maxWarning(10), _name(name) { }
100 : _nostream(new ostream(nullptr)), _level(level), _warnCounter(0), _maxWarning(10), _name(name) { }
102 Log::Log(
const string& name,
int level,
int maxCount)
103 : _nostream(new ostream(nullptr)), _level(level), _warnCounter(0), _maxWarning(maxCount), _name(name) { }
107 lock_guard<mutex> guard(
lock());
115 lock_guard<mutex> guard(
lock());
116 for (LevelMap::const_iterator lev = logLevels.begin(); lev != logLevels.end(); ++lev) {
126 lock_guard<mutex> guard(
lock());
133 lock_guard<mutex> guard(
lock());
135 log->second->_warnCounter = 0;
143 string tmpname = name;
144 bool triedAllParents =
false;
146 while (! triedAllParents) {
155 level =
existingLogs().find(tmpname)->second->getLevel();
160 size_t lastDot = tmpname.find_last_of(
".");
162 if (lastDot != string::npos) {
163 tmpname = tmpname.substr(0, lastDot);
166 triedAllParents =
true;
171 lock_guard<mutex> guard(
lock());
182 existingLogs().insert(make_pair(name, unique_ptr<Log>(
new Log(name, level, maxCount))));
223 lock_guard<mutex> guard(
lock());
250 if (level ==
"TRACE") {
254 if (level ==
"DEBUG") {
258 if (level ==
"INFO") {
262 if (level ==
"WARN") {
266 if (level ==
"ERROR") {
270 throw Error(
"Couldn't create a log level from string '" + level +
"'");
294 char* timestr = ctime(&rawtime);
315 lock_guard<mutex> guard(
lock());
327 cout <<
formatMessage(level,
"Maximum number of warnings reached. Further warning output will be suppressed for this class.") << endl;
339 lock_guard<mutex> guard(log.
lock());
345 cout << log.
formatMessage(level,
"Maximum number of warnings reached. Further warning output will be suppressed for this class.") << endl;
static std::string & endColorCode()
Shell color code for the end of the log levels.
static WarningCountMap & defaultMaxWarnings()
A static map for counting how many warnings have been issued for each logger.
std::map< std::string, std::unique_ptr< Log >> LogMap
Typedef for a collection of named logs.
std::unique_ptr< std::ostream > const _nostream
A null output stream, used for piping discarded output to nowhere.
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
static bool showTimestamp
Show timestamp?
static void updateLevels()
updates the verbosity levels of all the loggers
static void resetWarningCounters()
reset the warning counters for all loggers
Message logging routines.
std::string getColorCode(int level)
provide the escape string for the color associated to a give log level
static LevelMap & defaultLevels()
A static map of default log levels.
static Level getLevelFromName(const std::string &level)
Get a log level enum from a string.
std::string getName() const
Get the name of this logger.
bool isActive(int level) const
Will this log level produce output on this logger at the moment?
static Log & getLog(const std::string &name)
Get a logger with the given name.
auto begin(reversion_wrapper< T > w)
int getMaxWarning() const
Get the maximum number of warnings for this logger.
static bool showLogLevel
Show log level?
int _warnCounter
number of warnings issued by this logger.
std::map< std::string, int > LevelMap
Typedef for a collection of named log levels.
std::string formatMessage(int level, const std::string &message)
Turn a message string into the current log format.
static void updateCounters()
updates the max warning numbers of all the loggers
void log(int level, const std::string &message)
Write a message at a particular level.
static std::mutex & lock()
Mutex to modify global elements (protected because external streaming operator uses it) ...
std::map< int, std::string > ColorCodes
Typedef for a collection of shell color codes, accessed by log level.
static void setLevels(const LevelMap &logLevels)
Set the log levels all at once.
static std::string getLevelName(int level)
Get the std::string representation of a log level.
std::map< std::string, int > WarningCountMap
Typedef for a counting the number of warnings in order to turn off warnings above a certain threshold...
Level
Log priority levels.
static void setWarningMaxCount(const std::string &name, int maxCount)
set the maximum number of warnings for a given logger
static bool useShellColors
Use shell colour escape codes?
static ColorCodes & colorCodes()
A static map of shell color codes for the log levels.
static LogMap & existingLogs()
A static map of existing logs: we don't make more loggers than necessary.
auto end(reversion_wrapper< T > w)
static bool showLoggerName
Show logger name?
static std::mutex & displayLock()
Mutex to access screen (protected because external streaming operator uses it)
static void setLevel(const std::string &name, int level)
Set the log levels.
Log(const std::string &name)
Constructor by name.