Browse Source

threadify file logger

pull/664/head
dubslow 11 years ago
parent
commit
922bcb2384
  1. 5
      src/main.cpp

5
src/main.cpp

@ -24,10 +24,12 @@ @@ -24,10 +24,12 @@
#include <QFile>
#include <QDir>
#include <QDateTime>
#include <QMutexLocker>
#ifdef LOG_TO_FILE
static QtMessageHandler dflt;
static QTextStream* logFile {nullptr};
static QMutex mutex;
void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QString& msg)
{
@ -39,7 +41,8 @@ void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QStr @@ -39,7 +41,8 @@ void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QStr
&& msg == QString("QFSFileEngine::open: No file name specified"))
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->flush();
}

Loading…
Cancel
Save