C++ 日志记录设施和 Qt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1542799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:21:36  来源:igfitidea点击:

Logging facilities and Qt

c++qtlogging

提问by cybevnm

What logging facilities do you use whit Qt ?

您使用 Qt 的哪些日志记录工具?

Do you choose qDebug(), qWarning(), qCritical(), qFatal() methods, or maybe something like Log4cpp (Log4cplus etc.), or maybe some custom-maked code ?

您是选择 qDebug()、qWarning()、qCritical()、qFatal() 方法,还是 Log4cpp(Log4cplus 等)之类的方法,或者一些自定义代码?

采纳答案by rpg

Existing C++ logging libraries are too heavy for my tastes, so I have created a custom front-end based on ideas from Logging in C++for the Qt qInstallMsgHandlerq back-end. It's cross-platform and thread-safe. Someday I'll clean up the code and release it to the world :)

现有的 C++ 日志库对我的口味来说太重了,所以我根据来自Logging in C++ 的想法为 Qt qInstallMsgHandlerq 后端创建了一个自定义前端。它是跨平台和线程安全的。总有一天我会清理代码并将其发布给全世界:)

An interesting alternative for Qt is QxtLogger.

Qt 的一个有趣的替代品是QxtLogger

回答by DarrylC

If you are just working in a single thread, qDebug and such work pretty well, or you can modify them somewhat by installing your own handler with qInstallMessageHandler in QT 5.0+, or qInstallMsgHandler in old versions.

如果您只是在单线程中工作,qDebug 等工作得很好,或者您可以通过在 QT 5.0+ 中使用 qInstallMessageHandler 或在旧版本中使用 qInstallMsgHandler 安装自己的处理程序来稍微修改它们。

Note: Older versions of qDebug() etc., where you used qInstallMsgHandler (now deprecated, e.g. http://doc.qt.io/archives/4.6/qtglobal.html#qDebug) were not thread-safe. If you use threads, they would crash/break badly. Internally it was using QTextStream, which was reentrant, but not thread-safe.

注意:较旧版本的 qDebug() 等,您在其中使用 qInstallMsgHandler (现已弃用,例如http://doc.qt.io/archives/4.6/qtglobal.html#qDebug)不是线程安全的。如果您使用线程,它们会严重崩溃/中断。它在内部使用 QTextStream,它是可重入的,但不是线程安全的。

回答by kkoehne

Since Qt 5.2 supports categorized logging: http://qt-project.org/doc/qt-5/qloggingcategory.html. This allows you to split up your logging messages into a (hierarchy of) categories, and fine tune which is logged, and what not.

由于 Qt 5.2 支持分类日志记录:http: //qt-project.org/doc/qt-5/qloggingcategory.html。这允许您将日志消息分成(层次结构)类别,并微调哪些已记录,哪些未记录。

回答by jirkamat

Log4Qtis a port of famous log4j to the Qt world.

Log4Qt是著名的 log4j 到 Qt 世界的端口。

回答by Max Desiatov

QDebug is the best way to go, as it presents out-of-the-box integration with the rest of the framework, doesn't make you dependent on 3rd party code and covers pretty all of the logging needs.

QDebug 是最好的方法,因为它提供了与框架其余部分的开箱即用集成,不会使您依赖于 3rd 方代码并且几乎涵盖了所有日志记录需求。

回答by user2749832

Regarding the answer saying "Unfortunately qDebug() etc. are not thread-safe. If you use threads, they will crash/break badly. Internally it uses QTextStream, which is reentrant, but not thread-safe."

关于回答说“不幸的是,qDebug() 等不是线程安全的。如果你使用线程,它们会严重崩溃/中断。在内部它使用 QTextStream,它是可重入的,但不是线程安全的。”

I seriously doubt that, qDebug is designed to be used concurrently. File a bug if that isn't true.

我严重怀疑,qDebug 被设计为同时使用。如果这不是真的,请提交错误。

回答by Cristian Adam

I'm not using Qt, but for logging I'm using a modified version of Dr'Dobb's Logging in C++. The original code can be found here.

我没有使用 Qt,但对于日志记录,我使用的是 Dr'Dobb's Logging in C++的修改版本。原始代码可以在这里找到。

My modifications are specific to the Microsoft Windows platform (fopen doesn't allow file read sharing) and can be found here.

我的修改特定于 Microsoft Windows 平台(fopen 不允许文件读取共享),可以在此处找到。

回答by Pavels

Depends on how you want to use that log data.

取决于您想如何使用该日志数据。

If it is used for debugging at runtime, qWarning() would do just fine.

如果它用于运行时调试,qWarning() 就可以了。

If you need to debug retrospective (usually server side code), plain old text files are the best. It is best to organize these log files by day log is written.

如果您需要调试回溯(通常是服务器端代码),纯旧文本文件是最好的。这些日志文件最好按天日志被写入来组织。