进阶之格式化qDebug()输出

创作灵感

刚刚在看qt帮助手册时,无意间在<QtGlobal>中看见了这个函数void qSetMessagePattern(const QString &pattern),该函数的精华在于,你可以直接重定义qDebug()的输出结果格式。以往打印调试内容,调试内容所在的行,所在的文件等都采用定义宏的方式,如下所示

#include <QDebug>#define UN_MY_DEBUG
//#undef UN_MY_DEBUG
#if defined(UN_MY_DEBUG)
#define MY_DEBUG(x)
#else
#define MY_DEBUG(x) qDebug()<<"["<<__FILE__<<"}"<<__LINE__<<x;
#endif

然而现在你有另外一中全新的方式定义,Qt官方给出的原文如下图所示

void qSetMessagePattern(const QString &pattern)

Changes the output of the default message handler.

更改默认消息处理程序的输出。

Allows to tweak the output of qDebug(), qInfo(), qWarning(), qCritical(), and qFatal(). The category logging output of qCDebug(), qCInfo(), qCWarning(), and qCCritical() is formatted, too.

允许调整qDebug()、qInfo()、qWarning()、qCritical() 和 qFatal()的类别日志输出。qCDebug()、qCInfo()、qCWarning()和qCCritical()的类别日志输出也被格式化。

Following placeholders are supported:

支持以下占位符:

Placeholder

占位符

Description

描述

%{appname}

QCoreApplication::applicationName()

应用程序名称

%{category}

Logging category

日志记录类别

%{file}

Path to source file

源文件路径

%{function}

Function

函数名称

%{line}

Line in source file

源文件中的行

%{message}

The actual message

打印的日志信息

%{pid}

QCoreApplication::applicationPid()

应用程序Pid

%{threadid}

The system-wide ID of current thread (if it can be obtained)

当前线程的系统范围ID(如果可以获得)

%{qthreadptr}

A pointer to the current QThread (result of QThread::currentThread())

指向当前QThread(QThread的结果::当前线程QThread::currentThreadQThread的结果::当前线程()结果)

%{type}

"debug", "warning", "critical" or "fatal"

“调试”、“警告”、“严重”或“致命”

%{time process}

time of the message, in seconds since the process started (the token "process" is literal)

消息的时间,自进程启动以来的秒数(“process”是字面意思)

%{time boot}

the time of the message, in seconds since the system boot if that can be determined (the token "boot" is literal). If the time since boot could not be obtained, the output is indeterminate (see QElapsedTimer::msecsSinceReference()).

消息的时间,如果可以确定,自系统启动以来的秒数(“boot”是字面意思)。如果无法获得启动后的时间,则输出不确定(参见QElapsedTimer::msecsSinceReference())。

%{time [format]}

system time when the message occurred, formatted by passing the format to QDateTime::toString(). If the format is not specified, the format of Qt::ISODate is used.

消息发生时的系统时间,通过将format传递到QDateTime::toString()来格式化。如果未指定格式,则使用Qt::ISODate的格式。

%{backtrace [depth=N] [separator="..."]}

A backtrace with the number of frames specified by the optional depth parameter (defaults to 5), and separated by the optional separator parameter (defaults to "|"). This expansion is available only on some platforms (currently only platfoms using glibc). Names are only known for exported functions. If you want to see the name of every function in your application, use QMAKE_LFLAGS += -rdynamic. When reading backtraces, take into account that frames might be missing due to inlining or tail call optimization.

由可选的depth参数(默认为5)指定帧数的回溯,并由可选的separator参数(默认为 "|"). 此扩展仅在某些平台上可用(目前只有使用glibc的平台)。名称仅用于导出函数。如果您想查看应用程序中每个函数的名称,请使用QMAKE_LFLAGS += -rdynamic。读取回溯时,请考虑由于内联或尾部调用最优化而可能丢失帧。

You can also use conditionals on the type of the message using %{if-debug}%{if-info} %{if-warning}%{if-critical} or %{if-fatal} followed by an %{endif}. What is inside the %{if-*} and %{endif} will only be printed if the type matches.

您也可以对消息的类型使用条件%{if-debug}%{if-info} %{if-warning}%{if-critical}%{if-fatal}后跟%{endif}。只有当类型匹配时,才会打印%{if-*}%{endif}中的内容。

Finally, text inside %{if-category} ... %{endif} is only printed if the category is not the default one.

最后,只有当%{if-category}%{endif}不是默认类别时,才会打印文本。

Example:

示例:

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

The default pattern is "%{if-category}%{category}: %{endif}%{message}".

默认模式是"%{if-category}%{category}:%{endif}%{message}"。

The pattern can also be changed at runtime by setting the QT_MESSAGE_PATTERN environment variable; if both qSetMessagePattern() is called and QT_MESSAGE_PATTERN is set, the environment variable takes precedence.

也可以在运行时通过设置QT_MESSAGE_PATTERN环境变量来更改模式;如果同时调用了qSetMessagePattern()并设置了QT_MESSAGE_PATTERN,则环境变量优先。

Custom message handlers can use qFormatLogMessage() to take pattern into account.

自定义消息处理程序可以使用qFormatLogMessage()来考虑模式

案例1

#include "mainwindow.h"
#include <QApplication>
#include <QtGlobal>
#include <QDebug>int main(int argc, char *argv[])
{qSetMessagePattern("begin*************************************************\n""appname:%{appname}\n""category:%{category}\n""file:%{file}\n""function:%{function}\n""line:%{line}\n""pid:%{pid} ""threadid:%{threadid}\n""qthreadptr:%{qthreadptr}\n""type:%{type}\n""time process:%{time process}s\n""time boot:%{time boot}s\n""time [format]:%{time [yyyy-MM-dd hh:mm:ss.zzz]}\n""message:%{message}\n""end*************************************************\n");QApplication a(argc, argv);MainWindow w;w.show();qDebug()<<"hello world";qInfo()<<"hello world";qWarning()<<"hello world";return a.exec();
}

案例1输出

begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:26
pid:13712 threadid:20500
qthreadptr:0x8d3758
type:debug
time process:     0.118s
time boot:280024.458s
time [format]:[2024-06-06 16:32:04.481]
message:hello world
end*************************************************begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:27
pid:13712 threadid:20500
qthreadptr:0x8d3758
type:info
time process:     0.118s
time boot:280024.458s
time [format]:[2024-06-06 16:32:04.481]
message:hello world
end*************************************************begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:28
pid:13712 threadid:20500
qthreadptr:0x8d3758
type:warning
time process:     0.119s
time boot:280024.459s
time [format]:[2024-06-06 16:32:04.483]
message:hello world
end*************************************************

案例2

当然有时候我们还会去限定某些条件同某些功能,例如

#include "mainwindow.h"
#include <QApplication>
#include <QtGlobal>
#include <QDebug>int main(int argc, char *argv[])
{qSetMessagePattern("begin*************************************************\n""appname:%{appname}\n""category:%{category}\n""file:%{file}\n""function:%{function}\n""line:%{line}\n""pid:%{pid} ""threadid:%{threadid}\n""qthreadptr:%{qthreadptr}\n""type:%{type}\n""time process:%{time process}s\n""time boot:%{time boot}s\n""time [format]:%{time [yyyy-MM-dd hh:mm:ss.zzz]}\n""%{if-debug}debug:%{endif}""%{if-info}info:%{endif}""%{if-warning}warning:%{endif}""%{if-critical}critical:%{endif}""%{if-fatal}fatal:%{endif}""message:%{message}\n""end*************************************************\n");QApplication a(argc, argv);MainWindow w;w.show();qDebug()<<"hello world";qInfo()<<"hello world";qWarning()<<"hello world";return a.exec();
}

案例2输出

begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:31
pid:14164 threadid:17496
qthreadptr:0x13c36c0
type:debug
time process:     0.120s
time boot:280626.977s
time [format]:[2024-06-06 16:42:07.000]
debug:message:hello world
end*************************************************begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:32
pid:14164 threadid:17496
qthreadptr:0x13c36c0
type:info
time process:     0.120s
time boot:280626.977s
time [format]:[2024-06-06 16:42:07.000]
info:message:hello world
end*************************************************begin*************************************************
appname:untitled2
category:default
file:main.cpp
function:main
line:33
pid:14164 threadid:17496
qthreadptr:0x13c36c0
type:warning
time process:     0.122s
time boot:280626.979s
time [format]:[2024-06-06 16:42:07.003]
warning:message:hello world
end*************************************************

案例3

再如,我需要

debug模式下打印file,line

info模式下打印file,line,qthreadptr,time

waring模式下打印file,line

critical模式下打印file,line

fatal模式下打印file,line

​
#include "mainwindow.h"
#include <QApplication>
#include <QtGlobal>
#include <QDebug>int main(int argc, char *argv[])
{qSetMessagePattern("begin*************************************************\n""%{if-debug}%{file}(%{line}) %{endif}""%{if-info}%{file}(%{line}) qthreadptr:%{qthreadptr} time [format]:%{time [yyyy-MM-dd hh:mm:ss.zzz]} %{endif}""%{if-warning}%{file}(%{line}) %{endif}""%{if-critical}%{file}(%{line}) %{endif}""%{if-fatal}%{file}(%{line}) %{endif}""%{type}:%{message}\n""end*************************************************\n");QApplication a(argc, argv);MainWindow w;w.show();qDebug()<<"hello world";qInfo()<<"hello world";qWarning()<<"hello world";return a.exec();
}​

案例3输出

begin*************************************************
main.cpp(41) main debug:hello world
end*************************************************begin*************************************************
main.cpp(42) qthreadptr:0x13c3828 time [format]:[2024-06-06 16:56:18.884] info:hello world
end*************************************************begin*************************************************
main.cpp(43) warning:hello world
end*************************************************

案例4

设置环境变量

QT_MESSAGE_PATTERN=%{if-critical}%{file}(%{line}) %{endif}%{if-debug}%{file}(%{line}) %{endif}%{if-fatal}%{file}(%{line}) %{endif}%{if-info}%{file}(%{line}) qthreadptr:%{qthreadptr} time [format]:%{time [yyyy-MM-dd hh:mm:ss.zzz]} %{endif}%{if-warning}%{file}(%{line}) %{endif}%{type}:%{message}

 方法1 QtCreator系统的环境变量设置,下次新建项目时依旧可用,一劳永逸

注意:变量不需要加双引号

 弊端:没法加换行,亲测加换行就不行(我更倾向在代码里添加)

实例代码

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;qDebug()<<"hello world";qInfo()<<"hello world";qWarning()<<"hello world";w.show();return a.exec();
}

 

方法1 输出

main.cpp(8) debug:hello world
main.cpp(9) qthreadptr:0xbd1770 time [format]:[2024-06-06 17:39:29.131] info:hello world
main.cpp(10) warning:hello world

实例代码 

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;qDebug()<<"hello world";qInfo()<<"hello world";qWarning()<<"hello world";w.show();return a.exec();
}

 

  方法2 针对项目设置,下次新建项目时需重新设置

方法2输出

main.cpp(8) debug:hello world
main.cpp(9) qthreadptr:0xbd1770 time [format]:[2024-06-06 17:39:29.131] info:hello world
main.cpp(10) warning:hello world

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/23442.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大数据和数据分析来优化推荐算法

当涉及到使用大数据和数据分析来优化推荐算法时&#xff0c;通常我们会结合编程语言和特定的数据分析工具来实现。以下是一个简化的流程&#xff0c;以及在该流程中可能涉及的代码和工具内容的详细介绍。 1. 数据收集与预处理 工具&#xff1a;Python, pandas, NumPy 代码示例…

00-macOS和Linux安装和管理多个Python版本

在 Mac 上安装多个 Python 版本可通过几种不同方法实现。 1 Homebrew 1.1 安装 Homebrew 若安装过&#xff0c;跳过该步。 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 1.2 安装 Python 如安装 Python …

统计每天某个时间范围内得 数据状态

来统计在特定条件下的记录数。具体来说&#xff0c;并且这些记录必须满足以下时间条件和存在条件&#xff1a; 时间条件&#xff1a;当前时间的小时和分钟部分在0600&#xff08;早上6点&#xff09;和0800&#xff08;早上8点&#xff09;之间。这是通过SUBSTRB和TO_CHAR函数实…

计算机毕业设计 | SSM 校园线上订餐系统 外卖购物网站(附源码)

1&#xff0c; 概述 1.1 项目背景 传统的外卖方式就是打电话预定&#xff0c;然而&#xff0c;在这种方式中&#xff0c;顾客往往通过餐厅散发的传单来获取餐厅的相关信息&#xff0c;通过电话来传达自己的订单信息&#xff0c;餐厅方面通过电话接受订单后&#xff0c;一般通…

Time-LLM :超越了现有时间序列预测模型的学习器

AI预测相关目录 AI预测流程&#xff0c;包括ETL、算法策略、算法模型、模型评估、可视化等相关内容 最好有基础的python算法预测经验 EEMD策略及踩坑VMD-CNN-LSTM时序预测对双向LSTM等模型添加自注意力机制K折叠交叉验证optuna超参数优化框架多任务学习-模型融合策略Transform…

纷享销客集成平台(IPaaS)解决方案

针对传统对接方式中的痛点&#xff0c;集成平台提炼了一套成熟的解决方案和配套工具。 痛点&#xff11;&#xff1a;编码工作量大。 每个功能点&#xff0c;和众多的容错分支&#xff0c;都需要逐行编码实现。日志打少了影响问题排查&#xff0c;打多了浪费大量存储。 集成…

Django里的ModelForm组件

ModelForm组件 自动生成HTML标签 自动读取关联数据表单验证 错误提示数据库进行&#xff1a;新建&#xff0c;修改 步骤如下&#xff1a; 创建类 # 在 views.py 文件里# 创建一个类 class AssetModelForm(forms.ModelForm):class Meta:model models.AssetSet #fields [n…

k8s 对外服务之 Ingress(HTTPS/HTTP 代理访问 以及Nginx 进行 BasicAuth )

目录 一 Ingress HTTP 代理访问虚拟主机 &#xff08;一&#xff09;原理 &#xff08;二&#xff09;实验 1&#xff0c;准备 2&#xff0c;创建虚拟主机1资源 3&#xff0c;创建虚拟主机2资源 4&#xff0c;创建ingress资源 5&#xff0c;查看相关参数 6&#xff0…

Python变量符号:深入探索与实用指南

Python变量符号&#xff1a;深入探索与实用指南 在Python编程的世界中&#xff0c;变量符号扮演着至关重要的角色。它们不仅是存储数据的容器&#xff0c;更是构建复杂逻辑和算法的基础。然而&#xff0c;对于初学者来说&#xff0c;Python的变量符号可能会带来一些困惑和挑战…

【文末附gpt升级秘笈】埃隆·马斯克芯片调配策略对特斯拉股价的影响分析

埃隆马斯克芯片调配策略对特斯拉股价的影响分析 一、引言 在现代商业环境中&#xff0c;企业间的资源调配与策略布局往往对其股价产生深远影响。据外媒CNBC报道&#xff0c;埃隆马斯克在芯片资源分配上的决策引起了业界的广泛关注。他秘密要求英伟达将原本预留给特斯拉的高端…

如何为律师制作专业的商务名片?含电子名片二维码

律师关注细节&#xff0c;律师名片也不例外。它们不仅仅是身份的象征&#xff0c;更是律师专业形象的代表&#xff0c;传递专业知识和信任。今天就来和我们一起来看看制作律师商务名片的注意事项&#xff0c;以及如何制作商务名片上的电子名片二维码&#xff1f; 一、名片的主…

【Text2SQL】评估 LLM 的 Text2SQL 能力

论文&#xff1a;Evaluating the Text-to-SQL Capabilities of Large Language Models ⭐⭐⭐⭐ arXiv:2204.00498 一、论文速读 本论文尝试了多种 prompt 结构&#xff0c;并且评估了他们在 Codex 和 GPT-3 上的表现。下面介绍这些 prompt 结构&#xff1a; 二、不同的 prom…

Hive 面试题(二)

1. 简述Hive如何实现分区 &#xff1f; Hive中的分区是一种数据组织方式&#xff0c;它允许用户将表中的数据分割成不同的部分&#xff0c;每个部分称为一个分区。分区的主要目的是提高查询性能和数据管理的效率。以下是Hive实现分区的步骤和概念&#xff1a; 1. 创建分区表 …

优思学院|谈汽车零部件企业生产精益及现场管理

精益生产&#xff08;Lean Production&#xff09;和现场管理作为现代制造企业的核心管理理念&#xff0c;正在越来越多的企业中得到应用。尤其是在中国&#xff0c;许多汽车零部件企业通过精益管理和六西格玛方法&#xff0c;显著提高了生产效率&#xff0c;降低了生产成本&am…

过渡属性 height 设置 auto 不起作用

transition 在过渡时 无法将 auto 转换为 px&#xff0c;所以设置必须是具体数值&#xff0c;否则没有过渡效果 解决办法 操作 dom 对具体的数值进行转换 <div class"card-detail" v-for"(item, index) in 2" :key"index"><div class…

15.FreeRTOS 消息缓存 Message Buffer

FreeRTOS 消息缓存&#xff08;Message Buffer&#xff09;的使用 介绍 在实时操作系统&#xff08;RTOS&#xff09;中&#xff0c;任务之间的通信是一个非常重要的方面。FreeRTOS 提供了多种机制来实现任务间通信&#xff0c;其中之一就是消息缓存&#xff08;Message Buffe…

大数据之CDH对Hdfs做Balance数据均衡/数据平衡/数据倾斜

问题的来源: 由于在hive工具运行sql,出现sql卡顿的情况,去cdh上查看yarn资源的分布情况,发现了整个cdh平台中hdfs和yarn资源分布不均匀,大量的爆红显示: 以下 DataNode 数据目录 位于小于其可用空间 10.0 吉字节 的文件系统中。 /data1/dfs/dn&#xff08;可用&#xff1a;7.2 …

C++的类和new和delete和菱形继承机制

文章目录 参考虚函数使用虚函数的class结构相关实现源码IDA反编译子类虚表和父类虚表调用函数菱形继承 参考 https://showlinkroom.me/2017/08/21/C-%E9%80%86%E5%90%91%E5%88%86%E6%9E%90/ https://www.cnblogs.com/bonelee/p/17299985.html https://xz.aliyun.com/t/5242?t…

hadoop疑难问题解决_NoClassDefFoundError: org/apache/hadoop/fs/adl/AdlFileSystem

1、问题描述 impala执行查询&#xff1a;select * from stmta_raw limit 10; 报错信息如下&#xff1a; Query: select * from sfmta_raw limit 10 Query submitted at: 2018-04-11 14:46:29 (Coordinator: http://mrj001:25000) ERROR: AnalysisException: Failed to load …

Go GORM中的迁移系统,实现自动迁移与手动迁移

在Golang生态系统中&#xff0c;GORM作为一个广泛使用的ORM框架&#xff0c;不仅在数据库操作方面提供了友好的API支持&#xff0c;其迁移系统&#xff08;Migration System&#xff09;同样功能强大且易于使用。在本文中&#xff0c;我们将详细解析GORM中的迁移机制&#xff0…