summaryrefslogtreecommitdiffstats
path: root/lib/Log.cpp
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-09-02 14:23:12 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-09-02 14:23:49 +0200
commitcb8cd168a26477b5c01e73e53b727bf8f5b225db (patch)
tree73b6befe25325dc14cdd154a6751dec4a0178ddb /lib/Log.cpp
parentc0ecca4adfa5982d596adf2f8c8c01473bd28ca5 (diff)
downloaddabmod-cb8cd168a26477b5c01e73e53b727bf8f5b225db.tar.gz
dabmod-cb8cd168a26477b5c01e73e53b727bf8f5b225db.tar.bz2
dabmod-cb8cd168a26477b5c01e73e53b727bf8f5b225db.zip
Apply b1fc144 from common
Diffstat (limited to 'lib/Log.cpp')
-rw-r--r--lib/Log.cpp33
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;
+ }
}
}
}