0

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

Moia
  • 2,216
  • 1
  • 12
  • 34
mohsen
  • 1,763
  • 3
  • 17
  • 55
  • 1
    I still use these: [https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c](https://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c) – drescherjm Jun 05 '20 at 15:23

3 Answers3

2

make use of Q_FUNC_INFO macro.

qDebug() << Q_FUNC_INFO << "debug stmt";         ==> Qt way
qDebug() << __PRETTY_FUNCTION__ << "debug stmt"; ==> c++ way

To color use this thread answer

Ramkumar R
  • 497
  • 5
  • 11
2

take a look at https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern

So you can use for example in your main.cpp:

int main(int argc, char *argv[])
{
    qSetMessagePattern("%{function} %{message}");
    ...
}
SME
  • 96
  • 5
  • 1
    qSetMessagePattern is nice, but prevent you to choose when doing a pretty printing and when not. – Moia Jun 05 '20 at 15:45
1

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";
}
Moia
  • 2,216
  • 1
  • 12
  • 34