Module: check_mk
Branch: master
Commit: 63ad7bd6475bfaed4fc3d6b132f2100a8a16e463
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=63ad7bd6475bfa…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 30 15:45:48 2016 +0200
Fetched Livestatus from downstream.
---
livestatus/src/Logger.cc | 31 ++++++++++++++++---------------
livestatus/src/Logger.h | 20 +++++++++++++-------
2 files changed, 29 insertions(+), 22 deletions(-)
diff --git a/livestatus/src/Logger.cc b/livestatus/src/Logger.cc
index fb9c489..db0c9d5 100644
--- a/livestatus/src/Logger.cc
+++ b/livestatus/src/Logger.cc
@@ -40,8 +40,7 @@ FILE *fl_logfile = nullptr;
void open_logfile(const string &path) {
fl_logfile = fopen(path.c_str(), "a");
if (fl_logfile == nullptr) {
- logger(LogLevel::warning,
- "Cannot open logfile " + path + ": " +
strerror(errno));
+ Warning() << "Cannot open logfile " << path <<
": " << strerror(errno);
}
}
@@ -63,7 +62,7 @@ using std::mutex;
namespace {
std::mutex fl_logfile_mutex;
-LogLevel fl_log_level = LogLevel::notice;
+LogLevel fl_level = LogLevel::notice;
bool fl_log_microtime = false;
} // namespace
@@ -73,21 +72,21 @@ void reopen_logfile(const string &path) {
if (fl_logfile != nullptr) {
close_logfile();
open_logfile(path);
- logger(LogLevel::notice, "Reopened logfile.");
+ Notice() << "Reopened logfile.";
}
}
-void set_log_config(LogLevel log_level, bool log_microtime) {
- fl_log_level = log_level;
+void set_log_config(LogLevel level, bool log_microtime) {
+ fl_level = level;
fl_log_microtime = log_microtime;
}
-bool should_log(LogLevel log_level) { return log_level <= fl_log_level; }
+bool isLoggable(LogLevel level) { return level <= fl_level; }
FILE *get_logfile() { return fl_logfile != nullptr ? fl_logfile : stdout; }
-void logger(LogLevel log_level, const string &message) {
- if (!should_log(log_level)) {
+void logger(const LogRecord &record) {
+ if (!isLoggable(record.getLevel())) {
return; // msg not important enough
}
@@ -107,9 +106,9 @@ void logger(LogLevel log_level, const string &message) {
if (fl_log_microtime) {
fprintf(logfile, "%03ld.%03ld ", tv.tv_usec / 1000, tv.tv_usec %
1000);
}
- fprintf(logfile, "[%d] ", log_level);
- fputs(message.c_str(), logfile);
- fputc('\n', logfile);
+ fprintf(logfile, "[%d] ", static_cast<int>(record.getLevel()));
+ fputs(record.getMessage().c_str(), logfile);
+ fputs("\n", logfile);
fflush(logfile);
}
@@ -117,19 +116,21 @@ void logger(LogLevel log_level, const string &message) {
#include <ctime>
-void logger(LogLevel /*log_level*/, const string &message) {
+void logger(const LogRecord &record) {
extern bool runningInLivestatusMainThread();
extern void writeToAllLogs(const string &message);
// Only the main process may use the Nagios log methods
if (fl_logfile == nullptr || runningInLivestatusMainThread()) {
- writeToAllLogs("livestatus: " + message);
+ writeToAllLogs("livestatus: " + record.getMessage());
} else if (fl_logfile != nullptr) {
char timestring[64];
time_t now_t = time(nullptr);
struct tm now;
localtime_r(&now_t, &now);
strftime(timestring, 64, "%F %T ", &now);
- fputs((timestring + message + "\n").c_str(), fl_logfile);
+ fputs(timestring, fl_logfile);
+ fputs(record.getMessage().c_str(), fl_logfile);
+ fputs("\n", fl_logfile);
fflush(fl_logfile);
}
}
diff --git a/livestatus/src/Logger.h b/livestatus/src/Logger.h
index e76e755..3bdd854 100644
--- a/livestatus/src/Logger.h
+++ b/livestatus/src/Logger.h
@@ -32,6 +32,8 @@
#include <sstream>
#include <string>
+class LogRecord;
+
// values must be in sync with config
enum class LogLevel {
emergency = 0,
@@ -46,21 +48,25 @@ enum class LogLevel {
void open_logfile(const std::string &path);
void close_logfile();
-void logger(LogLevel log_level, const std::string &message);
+void logger(const LogRecord &record);
#ifdef CMC
-void set_log_config(LogLevel log_level, bool log_microtime);
+void set_log_config(LogLevel level, bool log_microtime);
void reopen_logfile(const std::string &path);
-bool should_log(LogLevel log_level);
+bool isLoggable(LogLevel level);
FILE *get_logfile();
#endif
class LogRecord {
public:
- explicit LogRecord(LogLevel log_level) : _log_level(log_level) {}
- virtual ~LogRecord() { logger(_log_level, _os.str()); }
+ explicit LogRecord(LogLevel level) : _level(level) {}
+ virtual ~LogRecord() { logger(*this); }
+
+ LogLevel getLevel() const { return _level; }
+ std::string getMessage() const { return _os.str(); }
+
#ifdef CMC
- bool isEnabled() const { return should_log(_log_level); }
+ bool isLoggable() const { return ::isLoggable(getLevel()); }
#endif
template <typename T>
@@ -69,7 +75,7 @@ public:
}
private:
- LogLevel _log_level;
+ LogLevel _level;
std::ostringstream _os;
};