Qt项目通用类----------调试类

之前:qDebug()等调试信息。 缺点:没有行号和函数名等调试当项目调试输出特别多时,容易混乱 不知道是那里输出的调试信息。
现在:将行号,文件名,函数名,组合一体之后再输出信息。一目了然就能知道此信息在哪里输出。

#define qdebug qDebug()<<"["<<__FILE__<<":"<<__LINE__<<Q_FUNC_INFO<<"]"<<":"

debughelper.h

#ifndef COMMONHELPER_H
#define COMMONHELPER_H
#include <QFile>
#include <QDebug>
//调试所用宏
#define xdebug   (QDebug(QtDebugMsg).nospace() << \
        "Debug: [" << __LINE__ << " @"  << Q_FUNC_INFO  /*<< " @" << __FILE__*/ << "] MSG:").space()
#define zdebug(...)   (QDebug(QtDebugMsg) \
        << "Debug: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xinfo   (QDebug(QtInfoMsg).nospace() << \
        "Info: [" << __LINE__ << " @"  << Q_FUNC_INFO  << " @" << __FILE__ << "] MSG:").space()
#define zinfo(...)   (QDebug(QtInfoMsg) \
        << "Info: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xwarning   (QDebug(QtWarningMsg).nospace() << \
        "Warning: [" << __LINE__ << " @"  << Q_FUNC_INFO  << " @" << __FILE__ << "] MSG:").space()
#define zwarning(...)   (QDebug(QtWarningMsg) \
        << "Warning: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xcritical   (QDebug(QtCriticalMsg).nospace() << \
        "Critical: [" << __LINE__ << " @"  << Q_FUNC_INFO  << " @" << __FILE__ << "] MSG:").space()
#define zcritical(...)   (QDebug(QtCriticalMsg) \
        << "Critical: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";
//注意使用fatal会使程序终止
#define xfatal   (QDebug(QtFatalMsg).nospace() << \
        "Fatal: [" << __LINE__ << " @"  << Q_FUNC_INFO  << " @" << __FILE__ << "] MSG:").space()
#define zfatal(...)   (QDebug(QtFatalMsg) \
        << "Fatal: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";
#endif // COMMONHELPER_H
#

另一种方法(不改变qDebug() 等原来的语法)

新建一个名字为TestApp的应用。代码如下:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qSetMessagePattern("%{appname} %{type} %{time [yyyy-MM-dd hh:mm:ss]} %{file} %{line} %{function} %{message}");
    qInfo()    << "Hello world";
    qDebug()   << "Hello world";
    qWarning() << "Hello world";

    return a.exec();
}

输出:

TestApp info [2020-04-13 23:01:11] …\TestApp\main.cpp 8 main Hello world
TestApp debug [2020-04-13 23:01:11] …\TestApp\main.cpp 9 main Hello world
TestApp warning [2020-04-13 23:01:11] …\TestApp\main.cpp 10 main Hello world

通过设置qSetMessagePattern函数去更改默认的消息。一般常用的格式字符串有:

在这里插入图片描述

注意:

qSetMessagePattern从Qt 5.0引入。
只能打印Qt的调试输出如:qDebug,qInfo,qWarning,qCritical,qFatal。而printf,cout将会原样输出。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YanWenCheng_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值