diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-09-02 14:28:29 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-09-02 14:28:29 +0200 |
commit | 3e5b7d0544e0a595a375644ae8eaef1b3ffb4b75 (patch) | |
tree | d018f6df28e53646e36ff1b4f3f1b628dfa50909 /lib/Log.cpp | |
parent | 3af9de93da4344089783142503d9f6e9c308c6b6 (diff) | |
download | dabmux-3e5b7d0544e0a595a375644ae8eaef1b3ffb4b75.tar.gz dabmux-3e5b7d0544e0a595a375644ae8eaef1b3ffb4b75.tar.bz2 dabmux-3e5b7d0544e0a595a375644ae8eaef1b3ffb4b75.zip |
Apply b1fc144 from common
Diffstat (limited to 'lib/Log.cpp')
-rw-r--r-- | lib/Log.cpp | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/lib/Log.cpp b/lib/Log.cpp index 2417f3a..abbd69a 100644 --- a/lib/Log.cpp +++ b/lib/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<std::mutex> guard(m_backend_mutex); + backends.clear(); +} void Logger::register_backend(std::shared_ptr<LogBackend> backend) { + std::lock_guard<std::mutex> 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<std::mutex> guard(m_backend_mutex); + for (auto &backend : backends) { + backend->log(m.level, message); + } - if (m.level != log_level_t::trace) { - std::lock_guard<std::mutex> 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; + } } } } |