From 29d602422cea4e9f2a59b8cefdb17b3862642e01 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 2 Sep 2019 14:26:04 +0200 Subject: Apply b1fc144 from common --- contrib/Globals.cpp | 36 ++++++++++++++++++++++++++++++++++++ contrib/Log.cpp | 33 +++++++++++++++++++++++---------- contrib/Log.h | 14 +++++--------- contrib/RemoteControl.cpp | 2 -- contrib/RemoteControl.h | 2 ++ 5 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 contrib/Globals.cpp (limited to 'contrib') diff --git a/contrib/Globals.cpp b/contrib/Globals.cpp new file mode 100644 index 0000000..6be26ec --- /dev/null +++ b/contrib/Globals.cpp @@ -0,0 +1,36 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2019 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + */ +/* + This file is part of the ODR-mmbTools. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +/* Ensure construction and destruction of static globals in the right order */ + +#include "Log.h" +#include "RemoteControl.h" + +// the RC needs logging, and needs to be initialised later. +Logger etiLog; +RemoteControllers rcs; + diff --git a/contrib/Log.cpp b/contrib/Log.cpp index 2417f3a..abbd69a 100644 --- a/contrib/Log.cpp +++ b/contrib/Log.cpp @@ -34,12 +34,23 @@ using namespace std; -/* etiLog is a singleton used in all parts of the program to output log messages. - */ -Logger etiLog; + +Logger::Logger() +{ + m_io_thread = std::thread(&Logger::io_process, this); +} + +Logger::~Logger() { + m_message_queue.trigger_wakeup(); + m_io_thread.join(); + + std::lock_guard guard(m_backend_mutex); + backends.clear(); +} void Logger::register_backend(std::shared_ptr backend) { + std::lock_guard guard(m_backend_mutex); backends.push_back(backend); } @@ -75,7 +86,7 @@ void Logger::logstr(log_level_t level, std::string&& message) { if (level == discard) { return; - } + } log_message_t m(level, move(message)); m_message_queue.push(move(m)); @@ -101,13 +112,15 @@ void Logger::io_process() message.resize(message.length()-1); } - for (auto &backend : backends) { - backend->log(m.level, message); - } + { + std::lock_guard guard(m_backend_mutex); + for (auto &backend : backends) { + backend->log(m.level, message); + } - if (m.level != log_level_t::trace) { - std::lock_guard guard(m_cerr_mutex); - std::cerr << levels_as_str[m.level] << " " << message << std::endl; + if (m.level != log_level_t::trace) { + std::cerr << levels_as_str[m.level] << " " << message << std::endl; + } } } } diff --git a/contrib/Log.h b/contrib/Log.h index d5c39e0..f20e698 100644 --- a/contrib/Log.h +++ b/contrib/Log.h @@ -133,16 +133,10 @@ struct log_message_t { class Logger { public: - Logger() { - m_io_thread = std::thread(&Logger::io_process, this); - } - + Logger(); Logger(const Logger& other) = delete; const Logger& operator=(const Logger& other) = delete; - ~Logger() { - m_message_queue.trigger_wakeup(); - m_io_thread.join(); - } + ~Logger(); void register_backend(std::shared_ptr backend); @@ -163,9 +157,11 @@ class Logger { ThreadsafeQueue m_message_queue; std::thread m_io_thread; - std::mutex m_cerr_mutex; + std::mutex m_backend_mutex; }; +/* etiLog is a singleton used in all parts of the program to output log messages. + * It is constructed in Globals.cpp */ extern Logger etiLog; // Accumulate a line of logs, using same syntax as stringstream diff --git a/contrib/RemoteControl.cpp b/contrib/RemoteControl.cpp index 878af59..4adb90c 100644 --- a/contrib/RemoteControl.cpp +++ b/contrib/RemoteControl.cpp @@ -32,8 +32,6 @@ using namespace std; -RemoteControllers rcs; - RemoteControllerTelnet::~RemoteControllerTelnet() { m_active = false; diff --git a/contrib/RemoteControl.h b/contrib/RemoteControl.h index bd88f82..2358b3a 100644 --- a/contrib/RemoteControl.h +++ b/contrib/RemoteControl.h @@ -149,6 +149,8 @@ class RemoteControllers { std::list > m_controllers; }; +/* rcs is a singleton used in all parts of the program to interact with the RC. + * It is constructed in Globals.cpp */ extern RemoteControllers rcs; /* Implements a Remote controller based on a simple telnet CLI -- cgit v1.2.3