From 23a923e31af4fde0bc7f2993ac8a3b08fe229f46 Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Sun, 1 Apr 2018 10:24:55 +0200 Subject: [PATCH] Logging: Make logging thread-safe Otherwise, we crash when two threads try to add entries to the log at the same time. --- pv/logging.cpp | 4 ++++ pv/logging.hpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/pv/logging.cpp b/pv/logging.cpp index 49b7d582..ab0594c3 100644 --- a/pv/logging.cpp +++ b/pv/logging.cpp @@ -28,6 +28,8 @@ #include +using std::lock_guard; + namespace pv { Logging logging; @@ -84,6 +86,8 @@ QString Logging::get_log() const void Logging::log(const QString &text, int source) { + lock_guard log_lock(log_mutex_); + if (buffer_.size() >= buffer_size_) buffer_.removeFirst(); diff --git a/pv/logging.hpp b/pv/logging.hpp index 0be270d4..fe05c543 100644 --- a/pv/logging.hpp +++ b/pv/logging.hpp @@ -22,11 +22,15 @@ #include "globalsettings.hpp" +#include + #include #include #include #include +using std::mutex; + namespace pv { class Logging : public QObject, public GlobalSettingsInterface @@ -70,6 +74,7 @@ Q_SIGNALS: private: int buffer_size_; QStringList buffer_; + mutable mutex log_mutex_; }; extern Logging logging; -- 2.30.2