diff --git a/src/blacksim/xplane/xbus/main.cpp b/src/blacksim/xplane/xbus/main.cpp index eb1b56b53..257936862 100644 --- a/src/blacksim/xplane/xbus/main.cpp +++ b/src/blacksim/xplane/xbus/main.cpp @@ -6,7 +6,6 @@ #define _CRT_SECURE_NO_WARNINGS #include "stub.h" #include "utils.h" -#include QSharedPointer g_qApp; XBus::CStub *g_stub; @@ -25,6 +24,7 @@ PLUGIN_API void XPluginStop() PLUGIN_API int XPluginEnable() { + QXPlaneMessageHandler::install(); g_qApp = QSharedApplication::sharedInstance(); QXPlaneEventLoop::exec(); g_stub = new XBus::CStub; diff --git a/src/blacksim/xplane/xbus/utils.h b/src/blacksim/xplane/xbus/utils.h index c9580ba12..afbc317c2 100644 --- a/src/blacksim/xplane/xbus/utils.h +++ b/src/blacksim/xplane/xbus/utils.h @@ -6,17 +6,62 @@ #ifndef BLACKSIM_XBUS_UTILS_H #define BLACKSIM_XBUS_UTILS_H +#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include +#include +#include +#include /*! * \file */ +/*! + * Install a Qt message handler which outputs to the X-Plane debug log. + */ +class QXPlaneMessageHandler +{ + QXPlaneMessageHandler(); + QXPlaneMessageHandler(const QXPlaneMessageHandler &); + + static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg) + { + QByteArray localMsg = msg.toLocal8Bit(); + char *buffer = new char[64 + localMsg.size() + std::strlen(context.file) + std::strlen(context.function)]; + switch (type) { + case QtDebugMsg: + std::sprintf(buffer, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + XPLMDebugString(buffer); + break; + case QtWarningMsg: + std::sprintf(buffer, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + XPLMDebugString(buffer); + break; + default: + case QtCriticalMsg: + std::sprintf(buffer, "Error: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + XPLMDebugString(buffer); + break; + case QtFatalMsg: + std::sprintf(buffer, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + XPLMDebugString(buffer); + std::abort(); + } + delete[] buffer; + } + +public: + static void install() + { + qInstallMessageHandler(handler); + } +}; + /*! * QApplication subclass used by XBus. * @@ -49,7 +94,7 @@ public: } if (! instance()->inherits("QSharedApplication")) { - XPLMDebugString("Error: there is an unshared QApplication in another plugin\n"); + qFatal("There is an unshared QApplication in another plugin"); } return static_cast(instance())->m_weakptr; }