在 linux 上运行我的 Qt5 应用程序时,我看不到来自 qDebug、qWarning、qCritical 或 qFatal 的任何输出。我知道我可以qInstallMsgHandler
用来安装消息处理程序并查看它们,但这是相当重量级的。
我只想检查 qWarning 日志,看看是否有任何信号连接错误。有没有办法查看这个日志?一个特殊的命令行选项,一个环境变量?
我想我记得在过去,一切都打印到 stderr,也许这是 Qt5 的变化?
Please do not make the mistake of assuming that qDebug, qWarning, qCritical and qFatal always log on standard error. That's absolutely not the case.
The actual destination varies depending on the Qt configuration and the targeting OS. Plus, 5.4 and then 5.11 introduced some behavioural changes. See here and here for discussions.
TL;DR:
On Qt >= 5.11
QT_FORCE_STDERR_LOGGING
to 1
.QT_ASSUME_STDERR_HAS_CONSOLE
to 1
. I suspect this one is meant to be used by a parent process that reads a child's stderr and shows it to the user somehow.QT_LOGGING_TO_CONSOLE
to 1
still works, but Qt will complain.On Qt >= 5.4 and < 5.11
QT_LOGGING_TO_CONSOLE
environment variable to 1
.QT_LOGGING_TO_CONSOLE
environment variable to 0
(this will force logging through the native system logger).QT_LOGGING_TO_CONSOLE
environment variable is not set, then whether logging to the console or not depends on whether the application is running in a TTY (on UNIX) or whether there's a console window (on Windows).On Qt < 5.4, the situation is more confusing.
The problem with the pre-5.4 approach was that, f.i., under Unix IDEs would not capture an application's debug output if Qt had been built with journald support. That's because the output went to journald, not to the IDE. In 5.4 the approach has been made more flexible and uniform across OSes.
如果您碰巧运行 Arch Linux,它使用该-journald
选项编译 Qt,所有调试输出默认定向到 systemd 日志(显示为journalctl
)。
QT_LOGGING_TO_CONSOLE=1
您可以通过定义为环境变量来覆盖此行为。
它们仍然打印为标准错误。
如果从命令行启动应用程序,它通常会打印在那里,或者如果使用 Qt Creator,它会显示在“应用程序输出”窗口中。
如果您使用的是 Visual Studio,则会将 qDebug 等打印到 IDE 的输出窗口中。