使用 QTestLib 时禁止 qDebug [英] Suppress qDebug when using QTestLib

查看:29
本文介绍了使用 QTestLib 时禁止 qDebug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向 Qt 中的项目添加单元测试,并希望使用 QTestLib.我已经设置了测试并且它们运行良好.

I am adding unit tests to project in Qt and am looking to use QTestLib. I have set up the tests and they are running fine.

问题是在项目中我们重写了 qDebug() 以输出到我们自己的日志文件.这在运行应用程序时效果很好,问题是当我测试类时,它有时会开始记录,然后将其发送到输出窗口.结果是一场彻底的灾难,几乎无法读取,因为我们的日志与 QTest 输出混合在一起.

The issue is that in the project we have overridden qDebug() to output to our own log file. This works great when running the app, the problem is that when I am testing the classes, it will sometimes start logging, which is then sent to the output window. The result is a complete disaster that is next to impossible to read as our logs get mixed in with the QTest output.

我想知道是否有办法抑制 qDebug() 输出,或者至少将它移到其他地方.我尝试添加 #define QT_NO_DEBUG_OUTPUT 并使用 qInstallMsgHandler(messageOutput); 重定向或阻止输出,但都没有任何效果.

I am wondering if there is a way to suppress the qDebug() output, or at least move it somewhere else. I have tried adding #define QT_NO_DEBUG_OUTPUT and also using qInstallMsgHandler(messageOutput); to redirect or prevent the output, but neither had any effect.

推荐答案

  1. QT_NO_DEBUG_OUTPUT 定义必须进入您的项目文件或生成文件,并且必须存在于您编译的每个文件中.然后您必须重新编译您的应用程序(当然不是 Qt 本身).这个宏出现在编译器的命令行上保证了第一次 QDebug 头被任何代码包含,qDebug 将被重新定义为无操作.这就是这个宏的作用:当 <QtCore/qdebug.h> 标头被包含时,它会禁用 qDebug 如果它存在 - 是否您直接或间接通过其他标题.

  1. The QT_NO_DEBUG_OUTPUT define must go into your project files or makefiles and must be present for every file you compile. You must then recompile your application (not Qt itself of course). This macro's presence on compiler's command line guarantees that the first time QDebug header is included by any code, the qDebug will be redefined to a no-op. That's what this macro does: it disables qDebug if it is present when the <QtCore/qdebug.h> header gets included -- whether directly by you or indirectly by other headers.

使用 qInstallMsgHandler 当然可以抑制调试输出.

Using qInstallMsgHandler certainly works at suppressing debug output.

下面是一个独立的例子.

Below is a self-contained example.

#if 0
// Enabling this section disables all debug output from non-Qt code.
#define QT_NO_DEBUG_OUTPUT
#endif
#include <QtCore/QDebug>

void noMessageOutput(QtMsgType, const char *)
{}

int main(int argc, char *argv[])
{
    qDebug() << "I'm shown";
    qInstallMsgHandler(noMessageOutput);
    qDebug() << "I'm hidden";
}

这篇关于使用 QTestLib 时禁止 qDebug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆