Kaynağa Gözat

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.
Richard Stanway 2 yıl önce
ebeveyn
işleme
bb7ca3c2c3
1 değiştirilmiş dosya ile 5 ekleme ve 2 silme
  1. 5 2
      UI/obs-app.cpp

+ 5 - 2
UI/obs-app.cpp

@@ -277,14 +277,17 @@ string CurrentDateTimeString()
 	return buf;
 	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;
 	string msg;
 	msg += timeString;
 	msg += timeString;
 	msg += str;
 	msg += str;
 
 
+	logfile_mutex.lock();
 	logFile << msg << endl;
 	logFile << msg << endl;
+	logfile_mutex.unlock();
 
 
 	if (!!obsLogViewer)
 	if (!!obsLogViewer)
 		QMetaObject::invokeMethod(obsLogViewer.data(), "AddLine",
 		QMetaObject::invokeMethod(obsLogViewer.data(), "AddLine",