fix: log-to-file does not work in multithread

This commit is contained in:
YiFang Xiao 2023-05-13 16:02:31 +08:00 committed by xiaoyifang
parent 6e52ae601f
commit 821cceef1d

View file

@ -29,6 +29,8 @@
#include <QtWebEngineCore/QWebEngineUrlScheme> #include <QtWebEngineCore/QWebEngineUrlScheme>
#include "gddebug.hh" #include "gddebug.hh"
#include <QMutexLocker>
#include <QMutex>
#if defined(USE_BREAKPAD) #if defined(USE_BREAKPAD)
#include "client/windows/handler/exception_handler.h" #include "client/windows/handler/exception_handler.h"
@ -48,6 +50,8 @@ bool callback(const wchar_t* dump_path, const wchar_t* id,
} }
#endif #endif
QMutex logMutex;
void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const QString &mess ) void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const QString &mess )
{ {
QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" ); QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" );
@ -59,6 +63,8 @@ void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const
.arg( mess ); .arg( mess );
if ( ( logFilePtr != nullptr ) && logFilePtr->isOpen() ) { if ( ( logFilePtr != nullptr ) && logFilePtr->isOpen() ) {
//without the lock ,on multithread,there would be assert error.
QMutexLocker _( &logMutex );
switch ( type ) { switch ( type ) {
case QtDebugMsg: case QtDebugMsg:
message.insert( 0, "Debug: " ); message.insert( 0, "Debug: " );
@ -84,6 +90,8 @@ void gdMessageHandler( QtMsgType type, const QMessageLogContext &context, const
return; return;
} }
//the following code lines actually will have no chance to run, schedule to remove in the future.
QByteArray msg = mess.toUtf8().constData(); QByteArray msg = mess.toUtf8().constData();
switch ( type ) { switch ( type ) {
case QtDebugMsg: case QtDebugMsg: