如何让 qDebug 编写方法和 class 名称?

How make qDebug to write method and class name?

我想用 qDebugqWarning 方法来编写调用 them.Now 的 class 和方法名称,我的写法如下所示,但这很辛苦我的 application.Does 中的所有 class 和方法 任何人都知道我该怎么做?

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

我想要 android 之类的东西,因为我在输出中看到它用不同的颜色编写方法和 class 名称只需调用 qDebug()

利用Q_FUNC_INFO宏。

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

颜色使用此线程answer

看看https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern

因此您可以在 main.cpp 中使用例如:

int main(int argc, char *argv[])
{
    qSetMessagePattern("%{function} %{message}");
    ...
}

Q_FUNC_INFO 是 std 漂亮函数的 Qt 版本。

来自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.

如果您想避免客户端代码冗长,您可以使用 Q_FUNC_INFO 定义自己的宏,例如:

#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";
}