From 50ce0f9681638176aa6a7cf0edd9c482a7b07e2f Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 26 May 2014 13:01:29 +0200 Subject: Fix crash at exit when there is a log after the Logger has been destroyed Use a proper static Logger instead of allocating one, and cleanup the QTMessageLogger when it is destroyed --- src/mirall/logger.cpp | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to 'src/mirall/logger.cpp') diff --git a/src/mirall/logger.cpp b/src/mirall/logger.cpp index 6c8561908..0bc8d1ccb 100644 --- a/src/mirall/logger.cpp +++ b/src/mirall/logger.cpp @@ -27,6 +27,9 @@ static void mirallLogCatcher(QtMsgType type, const char *msg) // qDebug() exports to local8Bit, which is not always UTF-8 Logger::instance()->mirallLog( QString::fromLocal8Bit(msg) ); } +static void qInstallMessageHandler(QtMsgHandler h) { + qInstallMsgHandler(h); +} #else static void mirallLogCatcher(QtMsgType, const QMessageLogContext &ctx, const QString &message) { Q_UNUSED(ctx); @@ -38,35 +41,23 @@ static void mirallLogCatcher(QtMsgType, const QMessageLogContext &ctx, const QSt } #endif -Logger* Logger::_instance=0; - -Logger::Logger( QObject* parent) -: QObject(parent), - _showTime(true), _doLogging(false), _doFileFlush(false), _logExpire(0) +Logger *Logger::instance() { + static Logger log; + return &log; } -Logger *Logger::instance() +Logger::Logger( QObject* parent) : QObject(parent), + _showTime(true), _doLogging(false), _doFileFlush(false), _logExpire(0) { - if( !Logger::_instance ) { - Logger::_instance = new Logger; -#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) - qInstallMsgHandler( mirallLogCatcher ); -#else - qInstallMessageHandler(mirallLogCatcher); -#endif - } - return Logger::_instance; + qInstallMessageHandler(mirallLogCatcher); } -void Logger::destroy() -{ - if( Logger::_instance ) { - delete Logger::_instance; - Logger::_instance = 0; - } +Logger::~Logger() { + qInstallMessageHandler(0); } + void Logger::postGuiLog(const QString &title, const QString &message) { emit guiLog(title, message); -- cgit v1.2.3