Module: check_mk
Branch: master
Commit: 076435aa71da308d63fa07f35aeaef8adf30d28a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=076435aa71da30…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 30 15:03:03 2016 +0200
Improve typing of log levels.
---
livestatus/src/Logger.cc | 20 ++++++++++----------
livestatus/src/Logger.h | 43 +++++++++++++++++++++++++++----------------
2 files changed, 37 insertions(+), 26 deletions(-)
diff --git a/livestatus/src/Logger.cc b/livestatus/src/Logger.cc
index 7d41390..fb9c489 100644
--- a/livestatus/src/Logger.cc
+++ b/livestatus/src/Logger.cc
@@ -26,7 +26,6 @@
#define _XOPEN_SOURCE 500
#include "Logger.h"
-#include <syslog.h>
#include <cerrno>
#include <cstdio>
#include <cstring>
@@ -41,7 +40,7 @@ FILE *fl_logfile = nullptr;
void open_logfile(const string &path) {
fl_logfile = fopen(path.c_str(), "a");
if (fl_logfile == nullptr) {
- logger(LOG_WARNING,
+ logger(LogLevel::warning,
"Cannot open logfile " + path + ": " +
strerror(errno));
}
}
@@ -64,7 +63,7 @@ using std::mutex;
namespace {
std::mutex fl_logfile_mutex;
-int fl_log_level = 5;
+LogLevel fl_log_level = LogLevel::notice;
bool fl_log_microtime = false;
} // namespace
@@ -74,20 +73,21 @@ void reopen_logfile(const string &path) {
if (fl_logfile != nullptr) {
close_logfile();
open_logfile(path);
- logger(LOG_NOTICE, "Reopened logfile.");
+ logger(LogLevel::notice, "Reopened logfile.");
}
}
-void set_log_config(int log_level, bool log_microtime) {
+void set_log_config(LogLevel log_level, bool log_microtime) {
fl_log_level = log_level;
fl_log_microtime = log_microtime;
}
-bool should_log(int priority) { return priority <= fl_log_level; }
+bool should_log(LogLevel log_level) { return log_level <= fl_log_level; }
FILE *get_logfile() { return fl_logfile != nullptr ? fl_logfile : stdout; }
-void logger(int priority, const string &message) {
- if (!should_log(priority)) {
+
+void logger(LogLevel log_level, const string &message) {
+ if (!should_log(log_level)) {
return; // msg not important enough
}
@@ -107,7 +107,7 @@ void logger(int priority, const string &message) {
if (fl_log_microtime) {
fprintf(logfile, "%03ld.%03ld ", tv.tv_usec / 1000, tv.tv_usec %
1000);
}
- fprintf(logfile, "[%d] ", priority);
+ fprintf(logfile, "[%d] ", log_level);
fputs(message.c_str(), logfile);
fputc('\n', logfile);
fflush(logfile);
@@ -117,7 +117,7 @@ void logger(int priority, const string &message) {
#include <ctime>
-void logger(int /*priority*/, const string &message) {
+void logger(LogLevel /*log_level*/, const string &message) {
extern bool runningInLivestatusMainThread();
extern void writeToAllLogs(const string &message);
// Only the main process may use the Nagios log methods
diff --git a/livestatus/src/Logger.h b/livestatus/src/Logger.h
index c0e1d5e..8106bbf 100644
--- a/livestatus/src/Logger.h
+++ b/livestatus/src/Logger.h
@@ -26,30 +26,41 @@
#define Logger_h
#include "config.h" // IWYU pragma: keep
-#include <syslog.h>
#ifdef CMC
#include <cstdio>
#endif
#include <sstream>
#include <string>
+// values must be in sync with config
+enum class LogLevel {
+ emergency = 0,
+ alert = 1,
+ critical = 2,
+ error = 3,
+ warning = 4,
+ notice = 5,
+ informational = 6,
+ debug = 7
+};
+
void open_logfile(const std::string &path);
void close_logfile();
-void logger(int priority, const std::string &message);
+void logger(LogLevel log_level, const std::string &message);
#ifdef CMC
-void set_log_config(int log_level, bool log_microtime);
+void set_log_config(LogLevel log_level, bool log_microtime);
void reopen_logfile(const std::string &path);
-bool should_log(int priority);
+bool should_log(LogLevel log_level);
FILE *get_logfile();
#endif
class Logger {
public:
- explicit Logger(int priority) : _priority(priority) {}
- virtual ~Logger() { logger(_priority, _os.str()); }
+ explicit Logger(LogLevel log_level) : _log_level(log_level) {}
+ virtual ~Logger() { logger(_log_level, _os.str()); }
#ifdef CMC
- bool isEnabled() const { return should_log(_priority); }
+ bool isEnabled() const { return should_log(_log_level); }
#endif
template <typename T>
@@ -58,40 +69,40 @@ public:
}
private:
- int _priority;
+ LogLevel _log_level;
std::ostringstream _os;
};
struct Emergency : public Logger {
- Emergency() : Logger(LOG_EMERG) {}
+ Emergency() : Logger(LogLevel::emergency) {}
};
struct Alert : public Logger {
- Alert() : Logger(LOG_ALERT) {}
+ Alert() : Logger(LogLevel::alert) {}
};
struct Critical : public Logger {
- Critical() : Logger(LOG_CRIT) {}
+ Critical() : Logger(LogLevel::critical) {}
};
struct Error : public Logger {
- Error() : Logger(LOG_ERR) {}
+ Error() : Logger(LogLevel::error) {}
};
struct Warning : public Logger {
- Warning() : Logger(LOG_WARNING) {}
+ Warning() : Logger(LogLevel::warning) {}
};
struct Notice : public Logger {
- Notice() : Logger(LOG_NOTICE) {}
+ Notice() : Logger(LogLevel::notice) {}
};
struct Informational : public Logger {
- Informational() : Logger(LOG_INFO) {}
+ Informational() : Logger(LogLevel::informational) {}
};
struct Debug : public Logger {
- Debug() : Logger(LOG_DEBUG) {}
+ Debug() : Logger(LogLevel::debug) {}
};
#endif // Logger_h