如何重定向 qDebug、qWarning、qCritical 等输出?
我使用大量 qDebug() <<
语句进行调试输出。是否有任何跨平台方法可以将调试输出重定向到文件,而无需求助于 shell 脚本?我猜 open() 和 dup2() 可以在 Linux 中完成这项工作,但是会吗在 Windows 中使用 MinGW 编译可以工作吗?
也许有一种 Qt 方法可以做到这一点?
I'm using a lot of qDebug() <<
statements for debug output. Is there any cross-platform way I can redirect that debug output to a file, without resorting to shell scripts? I'm guessing that open() and dup2() will do the job in Linux, but will it work compiled with MinGW in Windows?
And maybe there is a Qt way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您必须使用
qInstallMessageHandler
函数安装消息处理程序,然后,您可以使用QTextStream
将调试消息写入文件。这是一个示例:取自
qInstallMessageHandler
的文档(我只添加了注释):在上面的示例中,函数
myMessageOutput
使用stderr
,您可能需要将其替换为一些其他文件流,或者完全重写该函数!编写并安装此函数后,所有
qDebug
(以及qWarning
、qCritical
等)消息将被重定向到您的文件重新写入处理程序。You've to install a message handler using
qInstallMessageHandler
function, and then, you can useQTextStream
to write the debug message to a file. Here is a sample example:Taken from the doc of
qInstallMessageHandler
(I only added the comments):In the above example, the function
myMessageOutput
usesstderr
which you might want to replace with some other file stream, or completely re-write the function!Once you write and install this function, all your
qDebug
(as well asqWarning
,qCritical
etc) messages would be redirected to the file you're writing to in the handler.从这里所有功劳都归于精神。
From here all credit goes to spirit.
这是挂钩默认消息处理程序的工作示例。
谢谢@罗斯·罗杰斯!
Here is a working example of hooking the default message handler.
Thank you @Ross Rogers!
这是一个跨平台解决方案,如果应用程序是从 Qt Creator 运行的,则可以记录到控制台;如果应用程序被编译并作为独立应用程序运行,则可以记录到
debug.log
文件。main.cpp:
日志格式由
QString("%1 %2: %3 (%4)").arg...
(对于文件)处理,fprintf(stderr, "%s %s: %s (%s:%u, %s)\n"...
(用于控制台)。灵感:https://gist.github.com/polovik/10714049。
Here is a cross-platform solution to log to the console, if app was ran from Qt Creator, and to the
debug.log
file, when it is compiled and being ran as a standalone app.main.cpp:
Log formatting is handled by
QString("%1 %2: %3 (%4)").arg...
(for the file) andfprintf(stderr, "%s %s: %s (%s:%u, %s)\n"...
(for console).Inspiration: https://gist.github.com/polovik/10714049.
好吧,我想说,当您需要将调试输出重定向到与 stderr 不同的任何内容时,您可以考虑使用某种日志记录工具。如果您觉得需要一个,我建议使用
QxtLogger
( “QxtLogger 类是一个易于使用、易于扩展的日志记录工具。”)来自Qxt
库。Well, I would say that the moment when you need to redirect your debug output to anything different than stderr is when you could think about some logging tool. If you feel you need one I would recommend using
QxtLogger
("The QxtLogger class is an easy to use, easy to extend logging tool.") fromQxt
library.这是一个简单的、线程安全的惯用 Qt 示例,用于将其记录到 stderr 和文件:
按照其他答案中所述,使用
qInstallMessageHandler(messageHandler)
安装它。Here's a simple, thread safe idiomatic Qt example to log both to
stderr
and file:Install it with
qInstallMessageHandler(messageHandler)
as described in other answers.基于 Autodidact 的答案,进行了一些更改:
case
只设置txt
的值,仅在switch
后打印,QtFatalMsg
在打印前会abort()
任何事物。这意味着致命错误不会被记录!我的版本修复了它。Based on Autodidact's answer, with a few changes:
case
only sets the value oftxt
, which is printed only after theswitch
,QtFatalMsg
willabort()
before printing anything. This means that fatal errors won't be logged! My version fixes it.QTextStream
every time a message is logged, it sets everything up only once, when the program starts. This reduces the overhead of logging a message, as suggested by Paul Masri-Stone in a comment .