From ec5bb944b24413c1dee30a2a8429f484231c60c1 Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 1 Feb 2023 14:59:11 +0100 Subject: [PATCH 1/2] thread-safe logger Signed-off-by: KosmX --- launcher/Application.cpp | 2 ++ launcher/Application.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 387f735ce..ae7a69c66 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -150,6 +150,8 @@ namespace { /** This is used so that we can output to the log file in addition to the CLI. */ void appDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + const std::lock_guard lock(APPLICATION->loggerMutex); // synchronized, QFile logFile is not thread-safe + QString out = qFormatLogMessage(type, context, msg); out += QChar::LineFeed; diff --git a/launcher/Application.h b/launcher/Application.h index 1b3dc4990..caee074d1 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -45,6 +45,7 @@ #include #include +#include #include "minecraft/launch/MinecraftServerTarget.h" @@ -310,4 +311,5 @@ public: QList m_zipsToImport; QString m_instanceIdToShowWindowOf; std::unique_ptr logFile; + std::mutex loggerMutex; }; From 35a62d97875360132d8d67c0e6e6d69dd48481f5 Mon Sep 17 00:00:00 2001 From: KosmX Date: Wed, 1 Feb 2023 23:31:12 +0100 Subject: [PATCH 2/2] commit requested change, make the lock static Signed-off-by: KosmX --- launcher/Application.cpp | 4 +++- launcher/Application.h | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index ae7a69c66..0d3b086f6 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -77,6 +77,7 @@ #include "ApplicationMessage.h" #include +#include #include #include @@ -150,7 +151,8 @@ namespace { /** This is used so that we can output to the log file in addition to the CLI. */ void appDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { - const std::lock_guard lock(APPLICATION->loggerMutex); // synchronized, QFile logFile is not thread-safe + static std::mutex loggerMutex; + const std::lock_guard lock(loggerMutex); // synchronized, QFile logFile is not thread-safe QString out = qFormatLogMessage(type, context, msg); out += QChar::LineFeed; diff --git a/launcher/Application.h b/launcher/Application.h index caee074d1..1b3dc4990 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -45,7 +45,6 @@ #include #include -#include #include "minecraft/launch/MinecraftServerTarget.h" @@ -311,5 +310,4 @@ public: QList m_zipsToImport; QString m_instanceIdToShowWindowOf; std::unique_ptr logFile; - std::mutex loggerMutex; };