Where is located the qDebug qWarning qCritical and qFatal log by default on Qt?

末鹿安然 提交于 2019-11-28 10:38:02

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 introduced some behavioural changes. See here and here for discussions.

TL;DR:

On Qt >= 5.4:

  • 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.

If you happen to run Arch Linux, which compiles Qt with the -journald option, all debug output is per default directed to the systemd journal (display with journalctl).

You can override this behaviour by defining QT_LOGGING_TO_CONSOLE=1 as an environment variable.

They are still printed to standard error.

If you launch the application from the command line, it is usually printed there, or if using Qt Creator, it's displayed in the Application Output window.

If you are using visual studio, qDebug etc are printed to the output window in the IDE.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!