From c8a331c8000004ff68422e034332890c002b04c3 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 22 Apr 2018 21:54:56 +0200 Subject: Take some Log improvements from ODR-DabMux --- src/ConfigParser.cpp | 9 +++------ src/Log.cpp | 19 +++++++++---------- src/Log.h | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 350c278..c90b150 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -123,8 +123,7 @@ static void parse_configfile( // log parameters: if (pt.GetInteger("log.syslog", 0) == 1) { - LogToSyslog* log_syslog = new LogToSyslog(); - etiLog.register_backend(log_syslog); + etiLog.register_backend(make_shared()); } if (pt.GetInteger("log.filelog", 0) == 1) { @@ -138,14 +137,12 @@ static void parse_configfile( throw std::runtime_error("Configuration error"); } - LogToFile* log_file = new LogToFile(logfilename); - etiLog.register_backend(log_file); + etiLog.register_backend(make_shared(logfilename)); } std::string trace_filename = pt.Get("log.trace", ""); if (not trace_filename.empty()) { - LogTracer* tracer = new LogTracer(trace_filename); - etiLog.register_backend(tracer); + etiLog.register_backend(make_shared(trace_filename)); } diff --git a/src/Log.cpp b/src/Log.cpp index 81f7955..15c02d0 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -3,10 +3,10 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2016 + Copyright (C) 2018 Matthias P. Braendli, matthias.braendli@mpb.li - http://opendigitalradio.org + http://www.opendigitalradio.org */ /* This file is part of ODR-DabMod. @@ -26,7 +26,7 @@ */ #include -#include +#include #include #include "Log.h" @@ -41,10 +41,9 @@ using namespace std; */ Logger etiLog; - -void Logger::register_backend(LogBackend* backend) { +void Logger::register_backend(std::shared_ptr backend) +{ backends.push_back(backend); - //log(info, "Registered new logger " + backend->get_name()); } @@ -68,13 +67,13 @@ void Logger::log(log_level_t level, const char* fmt, ...) size *= 2; } - logstr(level, str); + logstr(level, move(str)); } -void Logger::logstr(log_level_t level, std::string message) +void Logger::logstr(log_level_t level, std::string&& message) { - log_message_t m(level, message); - m_message_queue.push(std::move(m)); + log_message_t m(level, move(message)); + m_message_queue.push(move(m)); } void Logger::io_process() diff --git a/src/Log.h b/src/Log.h index df6e07b..1253635 100644 --- a/src/Log.h +++ b/src/Log.h @@ -3,10 +3,10 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2016 + Copyright (C) 2018 Matthias P. Braendli, matthias.braendli@mpb.li - http://opendigitalradio.org + http://www.opendigitalradio.org */ /* This file is part of ODR-DabMod. @@ -42,6 +42,7 @@ #include #include #include +#include #include #include "ThreadsafeQueue.h" @@ -56,6 +57,7 @@ static const std::string levels_as_str[] = /** Abstract class all backends must inherit from */ class LogBackend { public: + virtual ~LogBackend() {}; virtual void log(log_level_t level, const std::string& message) = 0; virtual std::string get_name() const = 0; }; @@ -67,7 +69,7 @@ class LogToSyslog : public LogBackend { openlog(SYSLOG_IDENT, LOG_PID, SYSLOG_FACILITY); } - ~LogToSyslog() { + virtual ~LogToSyslog() { closelog(); } @@ -117,9 +119,9 @@ class LogTracer : public LogBackend { class LogLine; struct log_message_t { - log_message_t(log_level_t _level, const std::string& _message) : + log_message_t(log_level_t _level, std::string&& _message) : level(_level), - message(_message) {} + message(move(_message)) {} log_message_t() : level(debug), @@ -142,12 +144,12 @@ class Logger { m_io_thread.join(); } - void register_backend(LogBackend* backend); + void register_backend(std::shared_ptr backend); /* Log the message to all backends */ void log(log_level_t level, const char* fmt, ...); - void logstr(log_level_t level, std::string message); + void logstr(log_level_t level, std::string&& message); /* All logging IO is done in another thread */ void io_process(void); @@ -157,7 +159,7 @@ class Logger { LogLine level(log_level_t level); private: - std::list backends; + std::list > backends; ThreadsafeQueue m_message_queue; std::thread m_io_thread; -- cgit v1.2.3