]> sigrok.org Git - pulseview.git/commitdiff
Logging: Make logging thread-safe
authorSoeren Apel <redacted>
Sun, 1 Apr 2018 08:24:55 +0000 (10:24 +0200)
committerSoeren Apel <redacted>
Tue, 3 Apr 2018 06:38:11 +0000 (08:38 +0200)
Otherwise, we crash when two threads try to add entries to the log
at the same time.

pv/logging.cpp
pv/logging.hpp

index 49b7d5828359962dcc0f4fbdc274865c783c826d..ab0594c342e5b58a0d7bf63ec91693640adbba50 100644 (file)
@@ -28,6 +28,8 @@
 
 #include <QApplication>
 
+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<mutex> log_lock(log_mutex_);
+
        if (buffer_.size() >= buffer_size_)
                buffer_.removeFirst();
 
index 0be270d488d4506cbc82db3fabbc9cfb81458585..fe05c5439ecf52d30c663614c08948d1c2b9ef53 100644 (file)
 
 #include "globalsettings.hpp"
 
+#include <mutex>
+
 #include <QtGlobal>
 #include <QObject>
 #include <QString>
 #include <QStringList>
 
+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;