当前位置: 首页> 游戏> 网游 > 济南建设个人网站平台_免费好玩的网页游戏_灰色行业关键词推广_鄂尔多斯seo

济南建设个人网站平台_免费好玩的网页游戏_灰色行业关键词推广_鄂尔多斯seo

时间:2025/8/5 9:47:41来源:https://blog.csdn.net/lsyrhz/article/details/144377110 浏览次数:0次
济南建设个人网站平台_免费好玩的网页游戏_灰色行业关键词推广_鄂尔多斯seo

创作灵感

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

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).示例代码

#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  18struct 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);
#endifQGuiApplication app(argc, argv);
#ifdef X9setProperty(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).设置环境变量

export MY_DEBUG_CATEGORY=1

再次运行

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

关键字:济南建设个人网站平台_免费好玩的网页游戏_灰色行业关键词推广_鄂尔多斯seo

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: