Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qDebug() , qWarning(), qFatal(), qCritical



  • hi guys

    I want to do some study on qDebug(), qWarning(), qFatal() and qCritical.
    I went through this document https://doc.qt.io/qt-5/qtglobal.html#qCritical
    What is exact difference in all 4 and how to effectively use them in program?
    I wrote a small code

    code :

    if(c == 100)
       {
           qDebug()<<"Value is "<<c;
           qInfo()<<"INFO VALUE is";
           qCritical()<<"qWarning";
           qFatal("fatal error");
           qDebug()<<"Value is "<<c;
       }
    

    **output:

    Value is 100
    INFO VALUE is
    qwarning
    qCritical
    fatal error**

    so I got to know that qFatal will terminate the program but how I can use qWarning, qCritical or qInfo effectively as i cannot see any difference in logs of qWarning, qCritical , qInfo and qDebug.

    Thanks and Regards
    Ashish



  • @ashajg
    See also https://doc.qt.io/qt-5/debug.html, section Warning and Debugging Messages, which summarises the purpose of each one. If you read, each one has its own behaviour in terms of testing compile-time #defines or runtime environment variables.

    They all do a similar job (by default): output some text, which you can see in e.g. the Qt Creator Console window. They do so via an inbuilt Qt "message handler". You can put your own message handler in via https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler, and have it execute your own logic. In my own code, I test the https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum of a message against a "debug level" I read in from an external configuration file and only issue the message for those above a certain level. So I can change the configuration file value to control whether, say, QtInfoMsg messages are or are not output.

    So... pepper your code with the ones appropriate to the situation: a nullptr value which is not acceptable might be subjected to a qWarning, while just outputting a value so that you know what it is might be a qDebug.


  • Lifetime Qt Champion

    @ashajg see also the "new" feature of categorized logging: https://blog.qt.io/blog/2014/03/11/qt-weekly-1-categorized-logging/



  • @aha_1980 @JonB Thank you guys!!!



  • @ashajg
    @aha_1980 's logging by categories (I didn't know Qt had that) is exactly the kind of thing I do in my own code (in qtMessageHandler) to achieve logging-by-individual-categories. It helps a lot if you put something like this in as you develop: it's much better to be able to switch on info message for, say, just your database accesses rather than having to switch all info message for whole program on/off.


Log in to reply