]> Some of my projects - localmylist.git/commitdiff
Add custom message handler installed by MyList.
authorAPTX <marek321@gmail.com>
Thu, 30 May 2013 21:00:46 +0000 (23:00 +0200)
committerAPTX <marek321@gmail.com>
Thu, 30 May 2013 21:00:46 +0000 (23:00 +0200)
To disable set MyList::INSTALL_CUSTOM_ERROR_HANDLER to false.

localmylist/localmylist.pro
localmylist/messagehandler.cpp [new file with mode: 0644]
localmylist/messagehandler.h [new file with mode: 0644]
localmylist/mylist.cpp
localmylist/mylist.h

index 513e918fca3562389684602e6bf15d556bb8b7bf..030b99a55bdb2443bc436a738adf1aa7d5e87183 100644 (file)
@@ -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 (file)
index 0000000..6fda72a
--- /dev/null
@@ -0,0 +1,96 @@
+#include "messagehandler.h"
+
+#include <QtGlobal>
+#include <QString>
+#include <QByteArray>
+#include <QDateTime>
+
+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 (file)
index 0000000..f2404a4
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef MESSAGEHANDLER_H
+#define MESSAGEHANDLER_H
+
+namespace LocalMyList {
+       extern bool DETAILED_DEBUG_CONTEXT;
+
+       void installMessageHandler();
+} // namespace LocalMyList
+
+#endif // MESSAGEHANDLER_H
index d6aa173ebc0267dff92c0608eed50e83532e7f52..eaba3c6188ff4cc59dfd7ef00c4efe65c6803545 100644 (file)
@@ -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>("AbstractTaskPtr");
        qRegisterMetaType<QFileInfo>("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()
index 3489ebdd60ab289e0bb2719504e9cf6ebeda7653..31107d45b5d8b3d9b406ba739e6355622943b2b1 100644 (file)
@@ -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;