Skip to content

fix(logging): add Qt logging filter rules initialization#494

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369935-fix-hi-dpi-display-scaling
Jul 14, 2026
Merged

fix(logging): add Qt logging filter rules initialization#494
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369935-fix-hi-dpi-display-scaling

Conversation

@add-uos

@add-uos add-uos commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

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:

  • Add centralized initialization of Qt logging filter rules using QT_LOGGING_RULES, with a default rule that disables debug-level logging.

@sourcery-ai

sourcery-ai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Initializes 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 startup

flowchart 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]
Loading

File-Level Changes

Change Details Files
Initialize Qt logging filter rules at startup with a default that disables debug logs when QT_LOGGING_RULES is not set.
  • Include QLoggingCategory to support runtime logging filter configuration
  • Define a default logging rules string that disables debug messages for all categories
  • Add initLoggingRules() helper that reads QT_LOGGING_RULES and, when empty, applies the default filter rules via QLoggingCategory::setFilterRules
  • Invoke initLoggingRules() at the beginning of main() so logging configuration is applied before any log output
src/main.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@add-uos add-uos force-pushed the fix-369935-fix-hi-dpi-display-scaling branch 2 times, most recently from f586c6a to 5c075b6 Compare July 14, 2026 08:53
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "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 日志减少输出噪音。
@add-uos add-uos force-pushed the fix-369935-fix-hi-dpi-display-scaling branch from 5c075b6 to 26992ea Compare July 14, 2026 08:55
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词unset变动
详情
    {
    "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-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:85分

■ 【总体评价】

代码实现了通过环境变量和DConfig动态配置日志规则的功能,整体逻辑清晰。
因头文件存在命名空间污染及去重逻辑不严谨等代码质量问题扣15分。

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

MLogger 类的构造函数和析构函数逻辑基本通顺,能够正确读取环境变量和 DConfig 配置并设置日志规则。但在 logconfigread.cppMLogger 构造函数中,当 DConfig 的 valueChanged 信号触发时,直接调用 setRules(m_config->value(key).toByteArray()),这会用 DConfig 的新规则完全覆盖 m_rules,导致环境变量中配置的规则丢失。此外,MLoggermain 函数中作为局部变量创建,程序退出时析构函数中调用 m_config->deleteLater(),但此时事件循环已停止,deleteLater 可能无法执行,造成轻微内存泄漏。
潜在问题:DConfig 规则更新覆盖环境变量规则;析构时 deleteLater 可能无效。
建议:在 valueChanged 槽函数中合并环境变量规则与新规则;析构函数中直接使用 delete m_config

  • 2.代码质量(一般)✕

代码整体可读性尚可,但存在一些规范问题。在 ddlog.h 中使用了 DCORE_USE_NAMESPACE 宏,该宏展开为 using namespace Dtk::Core;,在头文件中使用 using namespace 会污染包含该头文件的所有文件的命名空间,属于不良实践。此外,logconfigread.cppappendRules 方法的去重逻辑基于 QString::contains 的子串匹配,不够严谨,可能导致误删规则。#include "qglobal.h" 似乎是多余的。
潜在问题:头文件命名空间污染;去重逻辑不严谨;多余包含。
建议:在 ddlog.h 中移除 DCORE_USE_NAMESPACE,改用完整命名空间限定;优化去重逻辑,使用精确匹配;清理无用头文件。

  • 3.代码性能(无性能问题)✓

日志配置的读取和规则设置仅在程序启动和配置变更时执行,不涉及高频调用或复杂计算,对程序运行性能无影响。
建议:无需优化。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码主要处理环境变量和本地 DConfig 配置,输入源可信,不存在命令注入、缓冲区溢出等安全风险。

  • 建议:保持现有安全实现。

■ 【改进建议代码示例】

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

@deepin-ci-robot

Copy link
Copy Markdown

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 2553e4d into linuxdeepin:master Jul 14, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants