25

What logging facilities do you use whit Qt ?

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

cybevnm
  • 2,586
  • 4
  • 30
  • 33

9 Answers9

12

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.

DarrylC
  • 129
  • 1
  • 4
  • -1 `qDebug()` is thread-safe. The thread-safety of `QTextStream` is irrelevant because there is no shared `QTextStream`. Each invokation of `qDebug()` gets its own `QTextStream`. – Oktalist Aug 17 '14 at 21:50
  • @Oktalist: I don't agree with you: http://stackoverflow.com/a/23517726/1202500 - and the answer(s) there is / are exactly my experience... – mozzbozz Feb 17 '15 at 17:32
  • 2
    @mozzbozz Thanks. If `qDebug` is unsafe then it is not for the reason given by DarrylC. If you install your own thread-safe `QtMessageHandler` then `qDebug` will be thread-safe. The default handler may interleave output but I don't believe there is a data race/UB, although I can't prove it. – Oktalist Feb 19 '15 at 21:54
  • 1
    @Oktalist Surprised you know that so certainly, since not many people have access to the Meego devices and source that showed it back in the late 2010/early 2011. That was the failure back then (and also on Symbian, if I remember correctly). What happens now I don't know, but you still shouldn't rely upon something being thread-safe that is not documented as being thread-safe, if you want the software used for more than a throw-away prototype. That is how you get code-rot: relying upon accidental behavior of today's implementation, rather than upon the specification. – DarrylC May 11 '15 at 18:51
  • I start supporting the claim that "qDebug() is not thread-safe". My experience has shown that in a multi-threaded application, the use of qDebug() may sometimes lead to crash or freeze. The problem is that this is hard to reproduce. A simple code example that can consistently reproduce the crash/freeze would help to confirm this claim. – jonathanzh Oct 28 '15 at 17:42
  • @jonathanzh Compiling Qt with [`-fsanitize=thread`](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual) would help to confirm. – Oktalist Jan 21 '17 at 17:22
  • 1
    qDebug and friends are now documented to be thread-safe (and they've always been thread-safe at least in Qt 4, 5 times): https://doc.qt.io/qt-5/qtglobal.html#qDebug – kkoehne Jan 18 '18 at 14:01
11

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.

kkoehne
  • 1,176
  • 9
  • 17
10

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.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
rpg
  • 7,746
  • 3
  • 38
  • 43
  • Maybe QxtLogger is best solution for Qt based projects. Only drawback (at my opinion), that this logger is part of pretty big library. Anyway, thanks for your advise. – cybevnm Oct 09 '09 at 15:49
  • 2
    @vnm: I have published my logging library at http://bitbucket.org/razvanpetru/qt-components/downloads/ – rpg Mar 25 '10 at 22:10
  • 2
    Just to update - there is now an official http://www.developer.nokia.com/Community/Wiki/Advanced,_cross-platform_logging_for_Qt – Martin Beckett Feb 17 '13 at 03:35
  • 1
    Pity all the links are now broken, I was hoping to take a look. – Thalia Jun 08 '17 at 16:29
  • 5
    How does this text qualify as an answer ***"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 :)"***.. What *"existing C++ logging libraries"* are you referring to? And why are they "too heavy"? Also, how does your custom library solve the problem? Any benchmark? And most importantly, how does it solve readers' problem if it is not available for use? – Nawaz Jul 31 '17 at 10:32
7

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

jirkamat
  • 1,776
  • 2
  • 15
  • 17
4

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.

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
  • 2
    But it does not log to files or the system event log. – philk Jul 31 '13 at 13:10
  • 1
    It doesn't do that by default, but that's not a good default as for me. If you want to reroute qDebug, refer to this post http://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output – Max Desiatov Aug 01 '13 at 18:08
  • 2
    I know that you can re-route qDebug. Still its not flexible enough for a mature application. – philk Aug 01 '13 at 21:53
3

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.

Cristian Adam
  • 4,749
  • 22
  • 19
  • 1
    In Qt several classes have a quite comfortable human readable serialization facilities, which should ihmo be called by a good Qt-logging library implicitely. A general C++ logging framework is here not good enough. – Valentin H May 03 '11 at 05:57
3

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.

  • The source seems to be http://www.qtcentre.org/threads/28879-redirecting-qDebug-to-file-threading-question – Finn Årup Nielsen Apr 09 '14 at 09:46
  • 1
    Doubt it or not - it doesn't matter. In 2011, on at least 1 platform, using it from multiple threads did crash. If it isn't documented as thread-safe, then it should be assumed to not be thread-safe, because the implementation is subject to change - which means that it isn't a bug if it doesn't work. If you want to ensure that the implementation doesn't change, then you need to copy the functionality, which means you are effectively writing your own version anyway (just with a head start). If the doc's say it is thread-safe as of a particular version, then you can use it. – DarrylC Feb 18 '15 at 18:38
1

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.

Pavels
  • 1,256
  • 1
  • 12
  • 19
0

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.
Alienpenguin
  • 967
  • 1
  • 9
  • 28