From bb7ca3c2c31dd797b90e58c02ee80d58cd4e4636 Mon Sep 17 00:00:00 2001 From: Richard Stanway Date: Mon, 27 Feb 2023 23:37:22 +0100 Subject: [PATCH] UI: Add mutex for writing to the log file fstream is not thread-safe and the act of writing the string and the newline are two separate operations which could execute concurrently in multiple threads, resulting in lines joining together followed by two newlines. Due to the presence of a static mutex, this also removes inline on the function. --- UI/obs-app.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 9a21ef90a..666fc71a9 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -277,14 +277,17 @@ string CurrentDateTimeString() return buf; } -static inline void LogString(fstream &logFile, const char *timeString, - char *str, int log_level) +static void LogString(fstream &logFile, const char *timeString, char *str, + int log_level) { + static mutex logfile_mutex; string msg; msg += timeString; msg += str; + logfile_mutex.lock(); logFile << msg << endl; + logfile_mutex.unlock(); if (!!obsLogViewer) QMetaObject::invokeMethod(obsLogViewer.data(), "AddLine",