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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值