--- /dev/null
+#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
#include "mylistexportparsetask.h"
#include "asyncquerytask.h"
#include "workthread.h"
+#include "messagehandler.h"
#ifndef LOCALMYLIST_NO_ANIDBUDPCLIENT
# include "requesthandler.h"
# include "renamehandler.h"
if (!MANUAL_CLEANUP)
qAddPostRoutine(MyList::destroy);
+ if (INSTALL_CUSTOM_ERROR_HANDLER)
+ installMessageHandler();
+
if (!REGISTER_QT_TYPES) return;
// qRegisterMetaType<AbstractTaskPtr>("AbstractTaskPtr");
qRegisterMetaType<QFileInfo>("QFileInfo");
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()