开发者

Logging facilities and Qt

开发者 https://www.devze.com 2022-12-08 06:45 出处:网络
What logging facilities do you use whit Qt ? Do you choose qDebug(), qWarning(), qCritical(), qFatal() methods, or maybe something like Log4cpp (Log4cplus etc.), or ma开发者_运维问答ybe some custom-

What logging facilities do you use whit Qt ?

Do you choose qDebug(), qWarning(), qCritical(), qFatal() methods, or maybe something like Log4cpp (Log4cplus etc.), or ma开发者_运维问答ybe some custom-maked code ?


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.

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.


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.


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 :)

An interesting alternative for Qt is QxtLogger.


Log4Qt is a port of famous log4j to the Qt world.


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.


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.

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


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

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


Depends on how you want to use that log data.

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

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.


You can have a look at: https://github.com/netresultsit/uniqlogger

  • LGPL
  • thread-safe
  • multiple backends: file, colored console, network, rsyslog
  • file rotation by size and number of files
  • support compression of previous files etc.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号