Module: check_mk
Branch: master
Commit: 06a22a211d569f8054d5e0ba7cd89df35da5d984
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=06a22a211d569f…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 30 11:40:07 2016 +0200
Fetched Livestatus from downstream.
---
livestatus/src/{logger.cc => Logger.cc} | 90 ++++++++++++++++++++++++++++++-
livestatus/src/Logger.h | 2 +
livestatus/src/Makefile.am | 2 +-
3 files changed, 92 insertions(+), 2 deletions(-)
diff --git a/livestatus/src/logger.cc b/livestatus/src/Logger.cc
similarity index 57%
rename from livestatus/src/logger.cc
rename to livestatus/src/Logger.cc
index 9302840..27df544 100644
--- a/livestatus/src/logger.cc
+++ b/livestatus/src/Logger.cc
@@ -25,6 +25,93 @@
// Needed for localtime_r
#define _XOPEN_SOURCE 500
+#include "Logger.h"
+
+#ifdef CMC
+
+#include <sys/time.h>
+#include <syslog.h>
+#include <cerrno>
+#include <cstdio>
+#include <cstring>
+#include <ctime>
+#include <mutex>
+#include <string>
+
+using std::lock_guard;
+using std::mutex;
+using std::string;
+
+namespace {
+FILE *g_logfile = nullptr;
+std::mutex g_logfile_mutex;
+int g_log_level = 5;
+bool g_log_microtime = false;
+} // namespace
+
+void set_log_config(int log_level, bool log_microtime) {
+ g_log_level = log_level;
+ g_log_microtime = log_microtime;
+}
+
+void open_logfile(const string &path) {
+ g_logfile = fopen(path.c_str(), "a");
+ if (g_logfile == nullptr) {
+ logger(LOG_WARNING,
+ "Cannot open logfile " + path + ": " +
strerror(errno));
+ }
+}
+
+void close_logfile() {
+ if (g_logfile != nullptr) {
+ fclose(g_logfile);
+ g_logfile = nullptr;
+ }
+}
+
+// Called during a logfile rotation, triggered by an external command.
+// This should only do somehting in case the logfile is really open.
+void reopen_logfile(const string &path) {
+ if (g_logfile != nullptr) {
+ close_logfile();
+ open_logfile(path);
+ logger(LOG_NOTICE, "Reopened logfile.");
+ }
+}
+
+bool should_log(int priority) { return priority <= g_log_level; }
+
+void logger(int priority, const string &message) {
+ if (!should_log(priority)) {
+ return; // msg not important enough
+ }
+
+ FILE *logfile = get_logfile();
+
+ // Make sure that loglines are not garbled up, Livestatus threads also
+ // log...
+ lock_guard<mutex> lg(g_logfile_mutex);
+ struct timeval tv;
+ gettimeofday(&tv, nullptr);
+ time_t t = tv.tv_sec;
+ struct tm lt;
+ localtime_r(&t, <);
+ char datestring[32];
+ strftime(datestring, sizeof(datestring), "%Y-%m-%d %H:%M:%S ", <);
+ fputs(datestring, logfile);
+ if (g_log_microtime) {
+ fprintf(logfile, "%03ld.%03ld ", tv.tv_usec / 1000, tv.tv_usec %
1000);
+ }
+ fprintf(logfile, "[%d] ", priority);
+ fputs(message.c_str(), logfile);
+ fputc('\n', logfile);
+ fflush(logfile);
+}
+
+FILE *get_logfile() { return g_logfile != nullptr ? g_logfile : stdout; }
+
+#else
+
#include <pthread.h>
#include <cstdio>
#include <syslog.h>
@@ -32,7 +119,6 @@
#include <cstring>
#include <ctime>
#include <string>
-#include "Logger.h" // IWYU pragma: keep
#include "nagios.h"
using std::string;
@@ -75,3 +161,5 @@ void logger(int /*priority*/, const string &message) {
fflush(fl_logfile);
}
}
+
+#endif
diff --git a/livestatus/src/Logger.h b/livestatus/src/Logger.h
index 8fd7479..c0e1d5e 100644
--- a/livestatus/src/Logger.h
+++ b/livestatus/src/Logger.h
@@ -27,7 +27,9 @@
#include "config.h" // IWYU pragma: keep
#include <syslog.h>
+#ifdef CMC
#include <cstdio>
+#endif
#include <sstream>
#include <string>
diff --git a/livestatus/src/Makefile.am b/livestatus/src/Makefile.am
index 1fbbccf..090a1fa 100644
--- a/livestatus/src/Makefile.am
+++ b/livestatus/src/Makefile.am
@@ -81,6 +81,7 @@ livestatus_so_SOURCES = \
LogCache.cc \
LogEntry.cc \
Logfile.cc \
+ Logger.cc \
LogwatchListColumn.cc \
MetricsColumn.cc \
NullColumn.cc \
@@ -146,7 +147,6 @@ livestatus_so_SOURCES = \
WorldNagios.cc \
auth.cc \
global_counters.cc \
- logger.cc \
mk_inventory.cc \
mk_logwatch.cc \
module.cc \