From 821cceef1d16e88f5e3e38a6a14ad52efdda866a Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Sat, 13 May 2023 16:02:31 +0800 Subject: [PATCH] fix: log-to-file does not work in multithread --- src/main.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.cc b/src/main.cc index 6260c13d..83178663 100644 --- a/src/main.cc +++ b/src/main.cc @@ -29,6 +29,8 @@ #include #include "gddebug.hh" +#include +#include #if defined(USE_BREAKPAD) #include "client/windows/handler/exception_handler.h" @@ -48,6 +50,8 @@ bool callback(const wchar_t* dump_path, const wchar_t* id, } #endif +QMutex logMutex; + void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const QString &mess ) { QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" ); @@ -59,6 +63,8 @@ void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const .arg( mess ); if ( ( logFilePtr != nullptr ) && logFilePtr->isOpen() ) { + //without the lock ,on multithread,there would be assert error. + QMutexLocker _( &logMutex ); switch ( type ) { case QtDebugMsg: message.insert( 0, "Debug: " ); @@ -84,6 +90,8 @@ void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const return; } + + //the following code lines actually will have no chance to run, schedule to remove in the future. QByteArray msg = mess.toUtf8().constData(); switch ( type ) { case QtDebugMsg: