c++qtqdebug

How make qDebug to write method and class name?


I want to make the qDebug or qWarning method to write the class and method name that calling them.Now I writing like below but It is hard work for all class and method in my application.Does Any one know How can I do this?

void File::downloadingFinishd()
{
    qWarning()<<"File::downloadingFinishd()";//How write this ?without manually doing this?
    qDebug()<<"Download was succceed";
}

I want something Like android because I see in output that it writing method and class name with diffrent color Just with calling qDebug()


Solution

  • Q_FUNC_INFO is the Qt version of std pretty function.

    From Qt Doc:

    const char* Q_FUNC_INFO

    Expands to a string that describe the function the macro resides in. How this string looks more specifically is compiler dependent. With GNU GCC it is typically the function signature, while with other compilers it might be the line and column number.

    If you want to avoid verbosity in client code, you can define your own macro with a Q_FUNC_INFO, for instance:

    #define qPrettyDebug() qDebug() << Q_FUNC_INFO
    #define qPrettyWarning() qWarning() << Q_FUNC_INFO
    
    void File::downloadingFinishd()
    {
        qPrettyWarning()<< "Download was succceed";
        qDebug() << "here I don't want to print again the function context";
    }