2

在 linux 上运行我的 Qt5 应用程序时,我看不到来自 qDebug、qWarning、qCritical 或 qFatal 的任何输出。我知道我可以qInstallMsgHandler用来安装消息处理程序并查看它们,但这是相当重量级的。

我只想检查 qWarning 日志,看看是否有任何信号连接错误。有没有办法查看这个日志?一个特殊的命令行选项,一个环境变量?

我想我记得在过去,一切都打印到 stderr,也许这是 Qt5 的变化?

4

4 回答 4

15

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

  • If the process' stderr has a console attached, that's where the debug log will go.
  • If you want to always log on stderr, set QT_FORCE_STDERR_LOGGING to 1.
  • In alternative, set 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

  • If you want to always log on stderr, set the QT_LOGGING_TO_CONSOLE environment variable to 1.
  • If you do not want to log on stderr, the QT_LOGGING_TO_CONSOLE environment variable to 0 (this will force logging through the native system logger).
  • If the 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.

  • If Qt has been built with support for a specific logging framework (e.g. SLOG2, journald, Android log, etc.) then logging always goes to that framework
  • Otherwise on UNIX it goes to stderr
  • Otherwise on Windows OutputDebugString or stderr is used depending whether the app is a console app.

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.

于 2014-10-12T13:40:54.573 回答
4

如果您碰巧运行 Arch Linux,它使用该-journald选项编译 Qt,所有调试输出默认定向到 systemd 日志(显示为journalctl)。

QT_LOGGING_TO_CONSOLE=1您可以通过定义为环境变量来覆盖此行为。

于 2014-10-10T11:28:26.093 回答
1

它们仍然打印为标准错误。

如果从命令行启动应用程序,它通常会打印在那里,或者如果使用 Qt Creator,它会显示在“应用程序输出”窗口中。

于 2014-10-10T08:50:54.463 回答
0

如果您使用的是 Visual Studio,则会将 qDebug 等打印到 IDE 的输出窗口中。

于 2014-10-10T12:07:38.773 回答