Qt 之 qSetMessagePattern

作者: 一去、二三里
个人微信号: iwaleon
微信公众号: 高效程序员

改变默认的消息处理输出。

允许改变qDebug()、qWarning()、qCritical()、qFatal()的输出。

占位符

支持以下占位符:

占位符描述
%{appname}QCoreApplication::applicationName()
%{category}日志类别
%{file}原文件路径
%{function}函数
%{line}源文件所在行
%{message}实际的消息
%{pid}QCoreApplication::applicationPid()
%{threadid}当前线程的系统范围ID(如果它可以获得)
%{type}“debug”、“warning”、“critical"或"fatal”
%{time process}“debug”、“warning”、“critical"或"fatal”
%{time boot}消息的时间,启动进程的秒数
%{time [format]}消息产生时,系统时间被格式化通过把格式传递至QDateTime::toString()。如果没有指定的格式,使用Qt::ISODate。
%{backtrace [depth=N] [separator=“…”]}很多平台不支持,暂略…

还可以使用条件类型,%{if-debug}, %{if-info} %{if-warning}, %{if-critical}%{if-fatal}后面跟着一个%{endif}。如果类型匹配,%{if-*}%{endif}之间的内容会被打印。

最后,如果类别不是默认的一个,%{if-category} ... %{endif}之间的内容将被打印。

例如:

QT_MESSAGE_PATTERN="[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}"

默认的模式是:“%{if-category}%{category}: %{endif}%{message}”。

也可以在运行时改变模式,通过设置QT_MESSAGE_PATTERN环境变量。如果既调用了 qSetMessagePattern()又设置了环境变量QT_MESSAGE_PATTERN,那么,环境变量优先。

示例

qSetMessagePattern

下面,我们使用上面讲解的占位符,来改变缺省消息处理程序的输出。

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    // 改变缺省消息处理程序的输出
    qSetMessagePattern("Message:%{message} File:%{file} Line:%{line} Function:%{function} DateTime:%{time [yyyy-MM-dd hh:mm:ss ddd]}");

    // 打印信息
    qDebug("This is a debug message.");
    qInfo("This is a info message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    qFatal("This is a fatal message.");

    ...
    return app.exec();
}

输出如下:

Message:This is a debug message. File:..\MessagePattern\main.cpp Line:138 Function:main DateTime:[2016-07-06 15:21:40 周三] Message:This is a info message. File:..\MessagePattern\main.cpp Line:139 Function:main DateTime:[2016-07-06 15:21:40 周三] Message:This is a warning message. File:..\MessagePattern\main.cpp Line:140 Function:main DateTime:[2016-07-06 15:21:40 周三] Message:This is a critical message. File:..\MessagePattern\main.cpp Line:141 Function:main DateTime:[2016-07-06 15:21:40 周三] Message:This is a fatal message. File:..\MessagePattern\main.cpp Line:142 Function:main DateTime:[2016-07-06 15:21:40 周三]

QT_MESSAGE_PATTERN环境变量

选择:项目 -> 构建环境,添加环境变量:

QT_MESSAGE_PATTERN = [%{type}] %{appname} (%{file}:%{line}) - %{message}
int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    // 改变缺省消息处理程序的输出
    qSetMessagePattern("Message:%{message} File:%{file} Line:%{line} Function:%{function} DateTime:%{time [yyyy-MM-dd hh:mm:ss ddd]}");

    // 打印信息
    qDebug("This is a debug message.");
    qInfo("This is a info message.");
    qWarning("This is a warning message.");
    qCritical("This is a critical message.");
    qFatal("This is a fatal message.");

    ...
    return app.exec();
}

输出如下:

[debug] MessagePattern (..\MessagePattern\main.cpp:138) - This is a debug message. [info] MessagePattern (..\MessagePattern\main.cpp:139) - This is a info message. [warning] MessagePattern (..\MessagePattern\main.cpp:140) - This is a warning message. [critical] MessagePattern (..\MessagePattern\main.cpp:141) - This is a critical message. [fatal] MessagePattern (..\MessagePattern\main.cpp:142) - This is a fatal message.

如上所述,这时即使我们使用了qSetMessagePattern也无济于事,因为,环境变量优先。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一去丶二三里

有收获,再打赏!

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

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

打赏作者

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

抵扣说明:

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

余额充值