From be5fd40abf586044d764f4692bb8d17e7f3422c9 Mon Sep 17 00:00:00 2001 From: APTX Date: Thu, 30 May 2013 23:00:46 +0200 Subject: [PATCH] Add custom message handler installed by MyList. To disable set MyList::INSTALL_CUSTOM_ERROR_HANDLER to false. --- localmylist/localmylist.pro | 6 ++- localmylist/messagehandler.cpp | 96 ++++++++++++++++++++++++++++++++++ localmylist/messagehandler.h | 10 ++++ localmylist/mylist.cpp | 6 +++ localmylist/mylist.h | 1 + 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 localmylist/messagehandler.cpp create mode 100644 localmylist/messagehandler.h diff --git a/localmylist/localmylist.pro b/localmylist/localmylist.pro index 513e918..030b99a 100644 --- a/localmylist/localmylist.pro +++ b/localmylist/localmylist.pro @@ -32,7 +32,8 @@ SOURCES += \ sqlasyncquery.cpp \ sqlasyncqueryinternal.cpp \ asyncquerytask.cpp \ - filelocationchecktask.cpp + filelocationchecktask.cpp \ + messagehandler.cpp HEADERS += \ localmylist_global.h \ @@ -59,7 +60,8 @@ HEADERS += \ sqlasyncqueryinternal.h \ asyncquerytask.h \ sqlresultiteratorinterface.h \ - filelocationchecktask.h + filelocationchecktask.h \ + messagehandler.h CONV_HEADERS += \ include/LocalMyList/AbstractTask \ diff --git a/localmylist/messagehandler.cpp b/localmylist/messagehandler.cpp new file mode 100644 index 0000000..6fda72a --- /dev/null +++ b/localmylist/messagehandler.cpp @@ -0,0 +1,96 @@ +#include "messagehandler.h" + +#include +#include +#include +#include + +namespace LocalMyList { + +/* + * When set to true, messageHandler will print context information + * if available. + */ +static bool DETAILED_DEBUG_CONTEXT = true; + +const char *messageType2Str(QtMsgType type) +{ + switch (type) + { + case QtWarningMsg: + return " Warning:"; + case QtCriticalMsg: + return " Critical:"; + case QtFatalMsg: + return " FATAL:"; + case QtDebugMsg: + default: + return ""; + } +} + +/* + * messageHandler will try to format the debug message + * and use Qt's default handlerto print it. + * It's not easy to reproduce how Qt prints debug messages on various platforms. + * Qt5 has a way to change how debug is printed via a format string, + * but for some reason it can't print a timestamp so a custom handler + * is still required. + */ +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) +static const QtMessageHandler qtMessageHandler = + []() -> QtMessageHandler + { + // Clear any set handler + qInstallMessageHandler(0); + // Get the default handler + QtMessageHandler h = qInstallMessageHandler(0); + // Reset default + qInstallMessageHandler(h); + return h; + }(); + +void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + const QString ctx = DETAILED_DEBUG_CONTEXT + ? QString(" [%2:%1]").arg(context.line).arg(context.file) + : QString(); + + const char *typeStr = messageType2Str(type); + + QString message = QString("[%3]%4%2 %1").arg( + msg, typeStr, + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"), + ctx); + + qtMessageHandler(type, context, message); +} +#else +void messageHandler(QtMsgType type, const char *msg) +{ + const char *typeStr = messageType2Str(type); + const QString message = QString("[%3]%2 %1").arg( + msg, typeStr, + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz")); + + QByteArray buf = message.toLocal8Bit(); + + qInstallMsgHandler(0); + // qt_message_output is the default message handler in Qt4. + // If you set your own handler it will just call that instead of + // actually printing the message. + qt_message_output(type, buf.constData()); + qInstallMsgHandler(messageHandler); +} +#endif + +void installMessageHandler() +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + qInstallMessageHandler(messageHandler); +#else + qInstallMsgHandler(messageHandler); +#endif +} + +} // namespace LocalMyList diff --git a/localmylist/messagehandler.h b/localmylist/messagehandler.h new file mode 100644 index 0000000..f2404a4 --- /dev/null +++ b/localmylist/messagehandler.h @@ -0,0 +1,10 @@ +#ifndef MESSAGEHANDLER_H +#define MESSAGEHANDLER_H + +namespace LocalMyList { + extern bool DETAILED_DEBUG_CONTEXT; + + void installMessageHandler(); +} // namespace LocalMyList + +#endif // MESSAGEHANDLER_H diff --git a/localmylist/mylist.cpp b/localmylist/mylist.cpp index d6aa173..eaba3c6 100644 --- a/localmylist/mylist.cpp +++ b/localmylist/mylist.cpp @@ -12,6 +12,7 @@ #include "mylistexportparsetask.h" #include "asyncquerytask.h" #include "workthread.h" +#include "messagehandler.h" #ifndef LOCALMYLIST_NO_ANIDBUDPCLIENT # include "requesthandler.h" # include "renamehandler.h" @@ -464,6 +465,9 @@ void MyList::init() if (!MANUAL_CLEANUP) qAddPostRoutine(MyList::destroy); + if (INSTALL_CUSTOM_ERROR_HANDLER) + installMessageHandler(); + if (!REGISTER_QT_TYPES) return; // qRegisterMetaType("AbstractTaskPtr"); qRegisterMetaType("QFileInfo"); @@ -471,6 +475,8 @@ void MyList::init() bool MyList::REGISTER_QT_TYPES = true; bool MyList::MANUAL_CLEANUP = false; +bool MyList::INSTALL_CUSTOM_ERROR_HANDLER = true; + MyList *MyList::m_instance = 0; MyList *instance() diff --git a/localmylist/mylist.h b/localmylist/mylist.h index 3489ebd..31107d4 100644 --- a/localmylist/mylist.h +++ b/localmylist/mylist.h @@ -115,6 +115,7 @@ public: static bool REGISTER_QT_TYPES; static bool MANUAL_CLEANUP; + static bool INSTALL_CUSTOM_ERROR_HANDLER; static const char *organizationName; static const char *libraryName; -- 2.52.0