Module: check_mk
Branch: master
Commit: 6b72e7d6a4fecc2ae0bfc7a47a84af759c9e13ba
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6b72e7d6a4fecc…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Aug 29 15:51:21 2016 +0200
No format string for logging anymore...
---
livestatus/src/Logger.h | 2 +-
livestatus/src/logger.cc | 25 ++++++++-----------------
livestatus/src/logger.h | 5 ++---
livestatus/src/module.cc | 1 +
4 files changed, 12 insertions(+), 21 deletions(-)
diff --git a/livestatus/src/Logger.h b/livestatus/src/Logger.h
index 859bedc..890c6de 100644
--- a/livestatus/src/Logger.h
+++ b/livestatus/src/Logger.h
@@ -32,7 +32,7 @@
class Logger {
public:
explicit Logger(int priority) : _priority(priority) {}
- virtual ~Logger() { logger(_priority, "%s", _os.str().c_str()); }
+ virtual ~Logger() { logger(_priority, _os.str()); }
template <typename T>
std::ostream &operator<<(const T &t) {
diff --git a/livestatus/src/logger.cc b/livestatus/src/logger.cc
index 5ec97df..cdd8882 100644
--- a/livestatus/src/logger.cc
+++ b/livestatus/src/logger.cc
@@ -28,7 +28,6 @@
#include "logger.h"
#include <pthread.h>
#include <cerrno>
-#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <ctime>
@@ -45,8 +44,8 @@ void open_logfile(const string &path) {
fl_logfile = fopen(path.c_str(), "a");
if (fl_logfile == nullptr) {
- logger(LOG_WARNING, "Cannot open logfile %s: %s", path.c_str(),
- strerror(errno));
+ logger(LOG_WARNING,
+ "Cannot open logfile " + path + ": " +
strerror(errno));
}
}
@@ -57,28 +56,20 @@ void close_logfile() {
}
}
-void logger(int /*priority*/, const char *loginfo, ...) {
- va_list ap;
- va_start(ap, loginfo);
-
+void logger(int /*priority*/, const string &message) {
// Only the main process may use the Nagios log methods
if (fl_logfile == nullptr || g_mainthread_id == pthread_self()) {
- char buffer[8192];
- snprintf(buffer, 20, "livestatus: ");
- vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
- loginfo, ap);
- write_to_all_logs(buffer, NSLOG_INFO_MESSAGE);
+ // TODO(sp) The Nagios headers are (once again) not const-correct...
+ write_to_all_logs(
+ const_cast<char *>(("livestatus: " + message).c_str()),
+ NSLOG_INFO_MESSAGE);
} 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, fl_logfile);
-
- vfprintf(fl_logfile, loginfo, ap);
- fputc('\n', fl_logfile);
+ fputs((timestring + message + "\n").c_str(), fl_logfile);
fflush(fl_logfile);
}
- va_end(ap);
}
diff --git a/livestatus/src/logger.h b/livestatus/src/logger.h
index f0bf6a5..d297c3c 100644
--- a/livestatus/src/logger.h
+++ b/livestatus/src/logger.h
@@ -26,11 +26,10 @@
#define logger_h
#include "config.h" // IWYU pragma: keep
-#include <syslog.h>
+#include <syslog.h> // IWYU pragma: keep
#include <string>
-void logger(int priority, const char *loginfo, ...)
- __attribute__((format(printf, 2, 3)));
+void logger(int priority, const std::string &message);
void open_logfile(const std::string &path);
void close_logfile();
diff --git a/livestatus/src/module.cc b/livestatus/src/module.cc
index 1e5f692..6b8b761 100644
--- a/livestatus/src/module.cc
+++ b/livestatus/src/module.cc
@@ -57,6 +57,7 @@
#include "data_encoding.h"
#include "global_counters.h"
#include "livestatus.h"
+#include "logger.h"
#include "nagios.h"
#include "strutil.h"
#include "waittriggers.h"