From 0818f2dec28732cd55071474c27732cc3196d772 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 28 Feb 2016 18:42:10 +0100 Subject: Protect cerr by mutex in Log, and minor things --- src/Log.cpp | 5 ++++- src/Log.h | 15 ++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Log.cpp b/src/Log.cpp index 7db1d44..6bbc7a9 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -72,7 +72,10 @@ void Logger::logstr(log_level_t level, std::string message) backend->log(level, message); } - std::cerr << levels_as_str[level] << " " << message << std::endl; + { + std::lock_guard guard(m_cerr_mutex); + std::cerr << levels_as_str[level] << " " << message << std::endl; + } } diff --git a/src/Log.h b/src/Log.h index 01afa75..f3b4409 100644 --- a/src/Log.h +++ b/src/Log.h @@ -39,13 +39,14 @@ #include #include #include +#include #define SYSLOG_IDENT "ODR-DabMod" #define SYSLOG_FACILITY LOG_LOCAL0 enum log_level_t {debug = 0, info, warn, error, alert, emerg}; -const std::string levels_as_str[] = +static const std::string levels_as_str[] = { " ", " ", "WARN ", "ERROR", "ALERT", "EMERG"} ; /** Abstract class all backends must inherit from */ @@ -72,17 +73,19 @@ class LogToSyslog : public LogBackend { int syslog_level = LOG_EMERG; switch (level) { case debug: syslog_level = LOG_DEBUG; break; - case alert: syslog_level = LOG_ALERT; break; case info: syslog_level = LOG_INFO; break; + /* we don't have the notice level */ case warn: syslog_level = LOG_WARNING; break; case error: syslog_level = LOG_ERR; break; + default: syslog_level = LOG_CRIT; break; + case alert: syslog_level = LOG_ALERT; break; case emerg: syslog_level = LOG_EMERG; break; } syslog(syslog_level, SYSLOG_IDENT " %s", message.c_str()); } - std::string get_name() { return name; }; + std::string get_name() { return name; } private: std::string name; @@ -117,7 +120,7 @@ class LogToFile : public LogBackend { fflush(log_file); } - std::string get_name() { return name; }; + std::string get_name() { return name; } private: std::string name; @@ -128,7 +131,7 @@ class LogLine; class Logger { public: - Logger() {}; + Logger() {} void register_backend(LogBackend* backend); @@ -143,6 +146,8 @@ class Logger { private: std::list backends; + + std::mutex m_cerr_mutex; }; extern Logger etiLog; -- cgit v1.2.3