Qt之自定义动态调控是否显示日志

创作灵感

最近在芯驰x9hp上开发仪表应用。由于需要仪表警告音,所以在该平台上折腾并且调试仪表声音的时候,无意间发现使用:

bash 复制代码
export QT_DEBUG_PLUGINS=1

可以打印更详细的调试信息。于是想着自己开发的应用也可以这样搞,这样更方便自己去动态的开关调试信息。

一、使用QLoggingCategory

1.1)、使用QLoggingCategory

QLoggingCategory是 Qt 中用于日志管理的强大工具,可以通过环境变量控制日志打印。要实现通过自定义环境变量控制日志开关,可以按照以下步骤操作:

  1. 创建自定义日志分类 : 使用 QLoggingCategory 创建一个新的日志分类。

  2. 定义日志输出规则 : 配置环境变量(如 MY_DEBUG_CATEGORY=true)来控制特定日志分类的打印。

  3. 使用日志分类 : 用 qCDebug()qCWarning() 等宏来打印日志。

  4. 动态读取环境变量 : 在程序启动时通过 QLoggingCategory::setFilterRules() 或直接设置 QT_LOGGING_RULES 环境变量,启用或禁用特定日志分类。

二、实现步骤

2.1).示例代码

cpp 复制代码
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QLoggingCategory>
#include <QDebug>
#ifdef X9
#include <sys/ioctl.h>
#include <linux/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define DWCTRL_VERSION 0x010003
#define DMP_ID_CLUSTER_STATUS  12
#define DMP_ID_CVBS_STATUS  18

struct dcf_ioc_setproperty {
    __u32 property_id;
    __u32 property_value;
    __u32 reserved1;
    __u32 reserved2;
};

#define DCF_IOC_MAGIC    'D'
#define DCF_IOC_SET_PROPERTY  _IOWR(DCF_IOC_MAGIC, 2,\
          struct dcf_ioc_setproperty)

#define DCF_IOC_GET_PROPERTY  _IOWR(DCF_IOC_MAGIC, 3,\
          struct dcf_ioc_setproperty)

void setProperty(int id,int value) {
    int fd = open("/dev/property", O_RDWR);
    if (fd < 0) {
        qFatal("Failed to open property device.");
        return;
    }

    while(1) {
        struct dcf_ioc_setproperty prop;
        prop.property_id = id;
        int ret = ioctl(fd, DCF_IOC_GET_PROPERTY, &prop);
        if(ret < 0) {
            qFatal("Failed to read property.");
            break;
        }

        prop.property_value = ((prop.property_value & 0xFFFF)|(value << 16));
        ret = ioctl(fd, DCF_IOC_SET_PROPERTY, &prop);
        if(ret < 0) {
            qFatal("Failed to read property.");
        }
        break;
    }

    close(fd);
}

#endif


// 声明和定义自定义日志分类
Q_DECLARE_LOGGING_CATEGORY(CustomCategory)
Q_LOGGING_CATEGORY(CustomCategory, "com.custom.category")

#define DEBUG      qCDebug(CustomCategory)
#define WARNING    qCWarning(CustomCategory)
#define CCRITICAL  qCCritical(CustomCategory)
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);
#ifdef X9
    setProperty(DMP_ID_CLUSTER_STATUS,1);
#endif


    // 读取自定义环境变量
    QByteArray envValue = qgetenv("MY_DEBUG_CATEGORY");
    if (!envValue.isEmpty() && envValue == "1") {
        // 设置过滤规则(可从环境变量或硬编码中设置)
        QLoggingCategory::setFilterRules("com.custom.category=true");
    } else {
        // 设置过滤规则(可从环境变量或硬编码中设置)
        QLoggingCategory::setFilterRules("com.custom.category=false");
    }

    // 使用自定义日志分类
    DEBUG << "This is a debug message.";
    WARNING << "This is a warning message.";
    CCRITICAL << "This is a critical message.";


    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreated,
        &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

三、使用方式

3.1).直接运行例子

3.2).设置环境变量

bash 复制代码
export MY_DEBUG_CATEGORY=1

再次运行

可以输出自己打印的内容了。

相关推荐
TAN-90°-2 分钟前
Java 6——成员变量初始值 object equals和== toString instanceof 参数传递问题
java·开发语言
故事和你9111 分钟前
洛谷-【图论2-1】树6
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
被AI抢饭碗的人12 分钟前
C++过渡Python
开发语言·python
不知名的老吴13 分钟前
在C++中不用宏怎么打日志的使用建议
开发语言·c++·算法
jieyucx20 分钟前
Go 语言进阶:结构体指针、new 关键字与匿名结构体/成员详解
开发语言·后端·golang·结构体
wjs202430 分钟前
jEasyUI 添加复选框指南
开发语言
迪霸LZTXDY31 分钟前
U-NET模型训练--图像标注脚本工具
开发语言·python
码界筑梦坊32 分钟前
119-基于Python的各类企业排行数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts·fastapi
习明然33 分钟前
记录下解决Python在windows 2008 Server 无法启动
开发语言·windows·python
凯瑟琳.奥古斯特35 分钟前
IP组播跨子网传输核心技术解析
java·开发语言·网络·网络协议·职场和发展