前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt重定向调试信息输出到(stdout, stderr, 文件等)

Qt重定向调试信息输出到(stdout, stderr, 文件等)

作者头像
Qt君
发布2019-07-15 15:19:09
6.2K0
发布2019-07-15 15:19:09
举报
文章被收录于专栏:跟Qt君学编程

先上代码:

代码语言:javascript
复制
#include <QCoreApplication>
#include <QDebug>
#include <stdio.h>
#include <stdlib.h>

FILE *output = NULL;

#if (QT_VERSION <= QT_VERSION_CHECK(5, 0, 0))
/* Qt4版本写法 */
void outputRedirection(QtMsgType type, const char *msg)
{
    switch (type) {
    case QtDebugMsg:
        fprintf(output, "Debug: %s\n", msg);
        break;
    case QtWarningMsg:
        fprintf(output, "Warning: %s\n", msg);
        break;
    case QtCriticalMsg:
        fprintf(output, "Critical: %s\n", msg);
        break;
    case QtFatalMsg:
        fprintf(output, "Fatal: %s\n", msg);
        abort();
    }
}
#else
/* Qt5版本写法 */
void outputRedirection(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        fprintf(output, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtInfoMsg:
        fprintf(output, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(output, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(output, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(output, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        abort();
    }
}
#endif

int main(int argc, char **argv)
{
#if (QT_VERSION <= QT_VERSION_CHECK(5, 0, 0))
    output = fopen("output.txt", "a"); //重定向于文件
    qInstallMsgHandler(outputRedirection);
#else
//    output = stdout; // 重定向于打印输出
    output = stderr; // 重定向与错误输出
    qInstallMessageHandler(outputRedirection);
#endif

    QCoreApplication app(argc, argv);
    qDebug()<<"Test Test Test";

    if (output)
        fclose(output);
    return app.exec();
}

这里的调试信息为qDebug(), qWarning(), qCritical(), qFatal()的输出.通过注册回调函数:

代码语言:javascript
复制
qInstallMsgHandler(Qt4版本使用Api);
qInstallMessageHandler(Qt5版本使用Api);

即可重定向(拦截)调试信息.

Qt4版本回调函数可实现输出类型与信息,而通过改变output变量的值可重定向输出到文件,stdout(屏幕)或stderr等.

代码语言:javascript
复制
/* Qt4版本写法 */
void outputRedirection(QtMsgType type, const char *msg)
{
    switch (type) {
    case QtDebugMsg:
        fprintf(output, "Debug: %s\n", msg);
        break;
    case QtWarningMsg:
        fprintf(output, "Warning: %s\n", msg);
        break;
    case QtCriticalMsg:
        fprintf(output, "Critical: %s\n", msg);
        break;
    case QtFatalMsg:
        fprintf(output, "Fatal: %s\n", msg);
        abort();
    }
}

同理Qt5版本回调函数也一样用法,只是功能更加扩展,可以获取到调试信息的文件名字,打印行,所在执行的函数等信息.

代码语言:javascript
复制
/* Qt5版本写法 */
void outputRedirection(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        fprintf(output, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtInfoMsg:
        fprintf(output, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(output, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(output, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(output, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        abort();
    }
}

一般应用在发布release版本后使用该功能,还实现日志等功能.

代码地址:

代码语言:javascript
复制
https://github.com/aeagean/QtOutputRedirection
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档