QCAD 自定义消息处理

QCAD 自定义消息处理

使用qInstallMessageHandler自定义输出调试信息。

消息处理句柄

\\src\\run\\main.cpp

 RMainWindow::installMessageHandler();

\\src\\core\\RMainWindow.cpp

static void installMessageHandler();

void RMainWindow::installMessageHandler() {

    qInstallMessageHandler(RMainWindow::messageHandler);

}

QtCreator应用程序窗口输出

输出调试信息

Warning:  RPropertyTypeId::generateId: property already initialized:  class RObject : "" : "Invisible"
09:33:50: Debug:    Plugins count: 3....
Warning:  Not a toolbar:  QMenu
09:33:55: Debug:    "Export 0 elapsed 0ms."
09:36:51: Debug:    "Export 10 elapsed 0ms."
09:36:51: Debug:    init dbfilename:D:/code/qcad_win64_release/qcad-3.24.3.0\\????.db
09:38:47: Debug:    done
09:38:48: Debug:    counter:  "RAction" :  133
09:38:48: Debug:    counter:  "RDocument" :  0
09:38:48: Debug:    counter:  "RDocumentInterface" :  0
09:38:48: Debug:    counter:  "REntity" :  0
09:38:48: Debug:    counter:  "RGraphicsView" :  1
09:38:48: Debug:    counter:  "RLayer" :  0
09:38:48: Debug:    counter:  "RLineEntity" :  0
09:38:48: Debug:    counter:  "RPolylineEntity" :  0
09:38:48: Debug:    counter:  "RSpatialIndexNavel" :  0
Warning:  QWaitCondition: Destroyed while threads are still waiting
Warning:  QWaitCondition: Destroyed while threads are still waiting
Warning:  QWaitCondition: Destroyed while threads are still waiting
Warning:  QWaitCondition: Destroyed while threads are still waiting

自定义调试信息输出


static void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message);

void RMainWindow::messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
    QByteArray localMsg = message.toLocal8Bit();
    QByteArray p = RDebug::getPrefix().toLocal8Bit();

    // intercept OpenGL messages for display in about dialog:
    if (localMsg.startsWith("Qt: ") ||
        localMsg.contains("QWindowsEGLStaticContext")) {
        RSettings::appendOpenGLMessage(localMsg);
    }

    switch (type) {
    case QtDebugMsg:

        if (context.file!=NULL && context.function!=NULL) {
            fprintf(stderr, "\\033[36m%s%s:%u, %s:\\033[0m\\n",
                    p.constData(), context.file, context.line, context.function);
        }
        fprintf(stderr, "%s%s: Debug:    %s\\n",
                p.constData(), (const char*)QTime::currentTime().toString().toLocal8Bit(), localMsg.constData());
        fflush(stderr);
        break;
#if QT_VERSION >= 0x050500
    case QtInfoMsg:
        if (context.file!=NULL && context.function!=NULL) {
            fprintf(stderr, "\\033[36m%s%s:%u, %s:\\033[0m\\n",
                    p.constData(), context.file, context.line, context.function);
        }
        fprintf(stderr, "%s%s: Info:    %s\\n",
                p.constData(), (const char*)QTime::currentTime().toString().toLocal8Bit(), localMsg.constData());
        fflush(stderr);
        break;
#endif
    case QtWarningMsg:
        if (localMsg.contains("changing class of non-QScriptObject not supported")) {
            break;
        }
        if (localMsg.startsWith("QPainter::")) {
            break;
        }
        if (context.file!=NULL && context.function!=NULL) {
            fprintf(stderr, "\\033[31m%s%s:%u, %s:\\033[0m\\n",
                    p.constData(), context.file, context.line, context.function);
        }
        fprintf(stderr, "%sWarning:  %s\\n", p.constData(), localMsg.constData());
        fflush(stderr);
        break;
    case QtCriticalMsg:
        if (context.file!=NULL && context.function!=NULL) {
            fprintf(stderr, "\\033[31m%s%s:%u, %s\\033[0m\\n",
                    p.constData(), context.file, context.line, context.function);
        }
        fprintf(stderr, "%sCritical: %s\\n", p.constData(), localMsg.constData());
        fflush(stderr);
        break;
    case QtFatalMsg:
        if (context.file!=NULL && context.function!=NULL) {
            fprintf(stderr, "\\033[31m%s%s:%u, %s\\033[0m\\n",
                    p.constData(), context.file, context.line, context.function);
        }
        fprintf(stderr, "%sFatal:    %s\\n", p.constData(), localMsg.constData());
        fflush(stderr);
        abort();
    }
}

原文链接:https://blog.csdn.net/mrbaolong/article/details/111885398

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片