From: Soeren Apel Date: Sun, 1 Apr 2018 08:24:55 +0000 (+0200) Subject: Logging: Make logging thread-safe X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=23a923e31af4fde0bc7f2993ac8a3b08fe229f46;p=pulseview.git Logging: Make logging thread-safe Otherwise, we crash when two threads try to add entries to the log at the same time. --- 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;