Module: check_mk
Branch: master
Commit: 9cf790828bf5ac74ef42412c1406145c2deeeda3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9cf790828bf5ac…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Aug 30 13:23:41 2016 +0200
Fetched Livestatus from downstream.
---
livestatus/src/Logger.cc | 94 ++++++++++++++++++----------------------------
livestatus/src/module.cc | 7 +++-
2 files changed, 42 insertions(+), 59 deletions(-)
diff --git a/livestatus/src/Logger.cc b/livestatus/src/Logger.cc
index 27df544..aaed892 100644
--- a/livestatus/src/Logger.cc
+++ b/livestatus/src/Logger.cc
@@ -26,60 +26,68 @@
#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;
+FILE *fl_logfile = nullptr;
+int fl_log_level = 5;
+bool fl_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) {
+ fl_logfile = fopen(path.c_str(), "a");
+ if (fl_logfile == nullptr) {
logger(LOG_WARNING,
"Cannot open logfile " + path + ": " +
strerror(errno));
}
}
void close_logfile() {
- if (g_logfile != nullptr) {
- fclose(g_logfile);
- g_logfile = nullptr;
+ if (fl_logfile != nullptr) {
+ fclose(fl_logfile);
+ fl_logfile = nullptr;
}
}
+#ifdef CMC
// 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) {
+ if (fl_logfile != nullptr) {
close_logfile();
open_logfile(path);
logger(LOG_NOTICE, "Reopened logfile.");
}
}
-bool should_log(int priority) { return priority <= g_log_level; }
+void set_log_config(int 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; }
+
+FILE *get_logfile() { return fl_logfile != nullptr ? fl_logfile : stdout; }
+#endif
+
+#ifdef CMC
+
+#include <sys/time.h>
+#include <ctime>
+#include <mutex>
+
+using std::lock_guard;
+using std::mutex;
+
+namespace {
+std::mutex fl_logfile_mutex;
+} // namespace
void logger(int priority, const string &message) {
if (!should_log(priority)) {
@@ -90,7 +98,7 @@ void logger(int priority, const string &message) {
// Make sure that loglines are not garbled up, Livestatus threads also
// log...
- lock_guard<mutex> lg(g_logfile_mutex);
+ lock_guard<mutex> lg(fl_logfile_mutex);
struct timeval tv;
gettimeofday(&tv, nullptr);
time_t t = tv.tv_sec;
@@ -99,7 +107,7 @@ void logger(int priority, const string &message) {
char datestring[32];
strftime(datestring, sizeof(datestring), "%Y-%m-%d %H:%M:%S ", <);
fputs(datestring, logfile);
- if (g_log_microtime) {
+ if (fl_log_microtime) {
fprintf(logfile, "%03ld.%03ld ", tv.tv_usec / 1000, tv.tv_usec %
1000);
}
fprintf(logfile, "[%d] ", priority);
@@ -108,45 +116,15 @@ void logger(int priority, const string &message) {
fflush(logfile);
}
-FILE *get_logfile() { return g_logfile != nullptr ? g_logfile : stdout; }
-
#else
-#include <pthread.h>
-#include <cstdio>
-#include <syslog.h>
-#include <cerrno>
-#include <cstring>
#include <ctime>
-#include <string>
#include "nagios.h"
-using std::string;
-
-pthread_t g_mainthread_id;
-static FILE *fl_logfile = nullptr;
-
-void open_logfile(const string &path) {
- // needed to determine main thread later
- g_mainthread_id = pthread_self();
-
- fl_logfile = fopen(path.c_str(), "a");
- if (fl_logfile == nullptr) {
- logger(LOG_WARNING,
- "Cannot open logfile " + path + ": " +
strerror(errno));
- }
-}
-
-void close_logfile() {
- if (fl_logfile != nullptr) {
- fclose(fl_logfile);
- fl_logfile = nullptr;
- }
-}
-
void logger(int /*priority*/, const string &message) {
+ extern bool runningInLivestatusMainThread();
// Only the main process may use the Nagios log methods
- if (fl_logfile == nullptr || g_mainthread_id == pthread_self()) {
+ if (fl_logfile == nullptr || runningInLivestatusMainThread()) {
// TODO(sp) The Nagios headers are (once again) not const-correct...
write_to_all_logs(
const_cast<char *>(("livestatus: " + message).c_str()),
diff --git a/livestatus/src/module.cc b/livestatus/src/module.cc
index 1e5f692..54e462d 100644
--- a/livestatus/src/module.cc
+++ b/livestatus/src/module.cc
@@ -101,7 +101,7 @@ static char fl_logfile_path[4096];
char g_mkeventd_socket_path[4096];
int g_debug_level = 0;
int g_should_terminate = false;
-extern pthread_t g_mainthread_id;
+pthread_t g_mainthread_id;
pthread_t *g_clientthread_id;
unsigned long g_max_cached_messages = 500000;
unsigned long g_max_lines_per_logfile =
@@ -179,7 +179,12 @@ void livestatus_cleanup_after_fork() {
}
}
+bool runningInLivestatusMainThread() {
+ return g_mainthread_id == pthread_self();
+}
+
void *main_thread(void *data __attribute__((__unused__))) {
+ g_mainthread_id = pthread_self();
g_thread_pid = getpid();
while (g_should_terminate == 0) {
do_statistics();