fix(logging): add Qt logging filter rules initialization#494
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideInitializes Qt logging filter rules at application startup by reading QT_LOGGING_RULES and defaulting to disabling debug logs when the variable is unset, ensuring reduced debug output noise. Flow diagram for Qt logging rules initialization at startupflowchart TD
A[Application start] --> B[main]
B --> C[initLoggingRules]
C --> D[qgetenv QT_LOGGING_RULES]
D --> E{rules isEmpty}
E -- yes --> F[QLoggingCategory::setFilterRules DEFAULT_LOGGING_RULES]
E -- no --> G[Keep existing logging rules]
F --> H[qInfo Starting deepin-camera application]
G --> H[qInfo Starting deepin-camera application]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
f586c6a to
5c075b6
Compare
|
Warning
详情 {
"unset": {
"src/src/logconfigread.cpp": {
"b": [
" // qunsetenv \u4e4b\u524d\u4e00\u5b9a\u4e0d\u8981\u6709\u4efb\u4f55\u65e5\u5fd7\u6253\u5370\uff0c\u5426\u5219\u53d6\u6d88\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e0d\u4f1a\u751f\u6548",
" qunsetenv(\"QT_LOGGING_RULES\");"
]
}
}
} |
Add initLoggingRules() to read QT_LOGGING_RULES environment variable for log filtering. Default to disabling debug logs when unset. 添加 Qt 日志过滤规则初始化,通过 QT_LOGGING_RULES 环境变量控制日志输出, 未设置时默认关闭 debug 日志。 Log: 添加 Qt 日志过滤规则初始化功能 PMS: BUG-369935 Influence: 应用启动时可通过环境变量控制日志级别,默认关闭 debug 日志减少输出噪音。
5c075b6 to
26992ea
Compare
|
Warning
详情 {
"unset": {
"src/src/logconfigread.cpp": {
"b": [
" // qunsetenv \u4e4b\u524d\u4e00\u5b9a\u4e0d\u8981\u6709\u4efb\u4f55\u65e5\u5fd7\u6253\u5370\uff0c\u5426\u5219\u53d6\u6d88\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e0d\u4f1a\u751f\u6548",
" qunsetenv(\"QT_LOGGING_RULES\");"
]
}
}
} |
deepin pr auto review★ 总体评分:85分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 --- a/src/src/ddlog.h
+++ b/src/src/ddlog.h
@@ -6,8 +6,6 @@
#include <DLog>
#include <dtkcore_global.h>
-DCORE_USE_NAMESPACE
-
namespace DDLog {
inline Q_LOGGING_CATEGORY(appLog, "org.deepin.camera")
}
diff --git a/src/src/logconfigread.h b/src/src/logconfigread.h
index dffee452..00000000 100644
--- a/src/src/logconfigread.h
+++ b/src/src/logconfigread.h
@@ -24,6 +24,7 @@ private:
private:
QString m_rules;
+ QString m_envRules;
Dtk::Core::DConfig *m_config;
};
diff --git a/src/src/logconfigread.cpp b/src/src/logconfigread.cpp
index bdfaf334..00000000 100644
--- a/src/src/logconfigread.cpp
+++ b/src/src/logconfigread.cpp
@@ -8,7 +8,6 @@
#include "logconfigread.h"
#include "ddlog.h"
#include "dtkcore_global.h"
-#include "qglobal.h"
#include <QLoggingCategory>
#include <QObject>
#include <DConfig>
@@ -16,6 +15,7 @@ using namespace DDLog;
MLogger::MLogger(QObject *parent)
: QObject(parent), m_rules(""), m_config(nullptr) {
QByteArray logRules = qgetenv("QT_LOGGING_RULES");
+ // qunsetenv 之前一定不要有任何日志打印,否则取消环境变量设置不会生效
qunsetenv("QT_LOGGING_RULES");
// 保存环境变量中的规则
+ m_envRules = logRules;
m_rules = logRules;
// 从 DConfig 读取日志规则
m_config = DConfig::create("org.deepin.camera", "org.deepin.camera.encode");
@@ -26,7 +26,10 @@ MLogger::MLogger(QObject *parent)
setRules(m_rules);
// 监听 DConfig 变化,动态更新日志规则
connect(m_config, &DConfig::valueChanged, this, [this](const QString &key) {
- if (key == "rules") {
- setRules(m_config->value(key).toByteArray());
+ if (key == "rules") {
+ m_rules = m_envRules;
+ appendRules(m_config->value(key).toByteArray());
+ setRules(m_rules);
}
});
}
@@ -37,7 +40,7 @@ MLogger::MLogger(QObject *parent) {
MLogger::~MLogger() {
if (m_config) {
- m_config->deleteLater();
+ delete m_config;
}
}
@@ -51,12 +54,17 @@ void MLogger::setRules(const QString &rules) {
}
void MLogger::appendRules(const QString &rules) {
- QString tmpRules = rules;
+ QString tmpRules = rules;
tmpRules = tmpRules.replace(";", "\n");
auto tmplist = tmpRules.split('\n');
+ QSet<QString> existingRules;
+ for (const QString &r : m_rules.split('\n')) {
+ existingRules.insert(r.trimmed());
+ }
+ for (const QString &r : m_rules.split(';')) {
+ existingRules.insert(r.trimmed());
+ }
for (int i = 0; i < tmplist.count(); i++) {
- if (m_rules.contains(tmplist.at(i))) {
+ if (existingRules.contains(tmplist.at(i).trimmed())) {
tmplist.removeAt(i);
i--;
} |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: add-uos, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
Add initLoggingRules() to read QT_LOGGING_RULES environment variable for log filtering. Default to disabling debug logs when unset.
添加 Qt 日志过滤规则初始化,通过 QT_LOGGING_RULES 环境变量控制日志输出,
未设置时默认关闭 debug 日志。
Log: 添加 Qt 日志过滤规则初始化功能
PMS: BUG-369935
Influence: 应用启动时可通过环境变量控制日志级别,默认关闭 debug 日志减少输出噪音。
Summary by Sourcery
Enhancements: