1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

threadify file logger

This commit is contained in:
dubslow 2014-11-06 22:53:05 -06:00
parent f29305bb01
commit 922bcb2384

View File

@ -24,10 +24,12 @@
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QDateTime> #include <QDateTime>
#include <QMutexLocker>
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
static QtMessageHandler dflt; static QtMessageHandler dflt;
static QTextStream* logFile {nullptr}; static QTextStream* logFile {nullptr};
static QMutex mutex;
void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QString& msg) void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QString& msg)
{ {
@ -39,7 +41,8 @@ void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QStr
&& msg == QString("QFSFileEngine::open: No file name specified")) && msg == QString("QFSFileEngine::open: No file name specified"))
return; return;
dflt(type, ctxt, msg); dflt(type, ctxt, msg); // this must be thread safe, otherwise qDebug() would never ever work
QMutexLocker locker(&mutex);
*logFile << QTime::currentTime().toString("HH:mm:ss' '") << msg << '\n'; *logFile << QTime::currentTime().toString("HH:mm:ss' '") << msg << '\n';
logFile->flush(); logFile->flush();
} }