Repository navigation
feat: categorize logs and register dynamic dconfig log watcher #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #include "log.h" | ||
|
|
||
| Q_LOGGING_CATEGORY(dmOcr, "deepin.ocr") | ||
| Q_LOGGING_CATEGORY(dmOcr, "org.deepin.ocr") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| #include "logger.h" | ||
|
|
||
| #include <QLoggingCategory> | ||
| #include <QDebug> | ||
|
|
||
| #include <DConfig> | ||
|
|
||
| #include "log.h" | ||
|
|
||
| DCORE_USE_NAMESPACE | ||
|
|
||
| MLogger::MLogger(QObject *parent) | ||
| : QObject(parent), m_rules("") | ||
| #ifdef DTKCORE_CLASS_DConfig | ||
| , m_config(nullptr) | ||
| #endif | ||
| { | ||
| QByteArray logRules = qgetenv("QT_LOGGING_RULES"); | ||
| // 在 Qt 日志系统初始化前置空 QT_LOGGING_RULES,避免环境变量规则 | ||
| // 与 DConfig 规则叠加;此时一定不能有任何日志打印,否则置空不会生效 | ||
| qputenv("QT_LOGGING_RULES", ""); | ||
|
|
||
| // set env | ||
| m_rules = logRules; | ||
|
|
||
| #ifdef DTKCORE_CLASS_DConfig | ||
| // set dconfig | ||
| m_config = DConfig::create("org.deepin.ocr", "org.deepin.ocr"); | ||
| if (!m_config) { | ||
| // meta 缺失时兑底:仅沿用环境变量规则,避免空指针崩溃 | ||
| setRules(m_rules); | ||
| return; | ||
| } | ||
| logRules = m_config->value("log_rules").toByteArray(); | ||
| appendRules(logRules); | ||
| setRules(m_rules); | ||
|
|
||
| // watch dconfig | ||
| connect(m_config, &DConfig::valueChanged, this, [this](const QString &key) { | ||
| if (key == "log_rules") { | ||
| setRules(m_config->value(key).toByteArray()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Environment filters stop applying When environment rules are set and Rebuild the active rules from both the environment rules and the updated DConfig value before calling Prompt for AI agentsIn `src/util/logger.cpp` at line 39:
**Environment filters stop applying**
When environment rules are set and `log_rules` changes at runtime, `setRules()` replaces the initially merged filter set with only the DConfig value, so environment-configured filters stop applying and logging behavior changes.
Rebuild the active rules from both the environment rules and the updated DConfig value before calling `setRules()`. |
||
| } | ||
| }); | ||
| #else | ||
| setRules(m_rules); | ||
| #endif | ||
| } | ||
|
|
||
| MLogger::~MLogger() | ||
| { | ||
| #ifdef DTKCORE_CLASS_DConfig | ||
| if (m_config) { | ||
| m_config->deleteLater(); | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| void MLogger::setRules(const QString &rules) | ||
| { | ||
| auto tmpRules = rules; | ||
| // Qt 限制:setFilterRules 的规则分隔符必须为 \n,不能是 ; | ||
| m_rules = tmpRules.replace(";", "\n"); | ||
| QLoggingCategory::setFilterRules(m_rules); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Framework debug logs remain enabled When A Qt/framework category emits debug or info messages while the app’s logging rules are active, Add explicit wildcard rules to disable debug/info for unmatched categories, then enable the requested levels for Prompt for AI agentsIn `src/util/logger.cpp` at line 61:
**Framework debug logs remain enabled**
When A Qt/framework category emits debug or info messages while the app’s logging rules are active, `setFilterRules` applies only the app-category rules from DConfig; Qt categories that do not match them retain Qt’s default filter, so their debug and info messages appear alongside app logs instead of remaining excluded.
Add explicit wildcard rules to disable debug/info for unmatched categories, then enable the requested levels for `org.deepin.ocr*`. |
||
| } | ||
|
|
||
| void MLogger::appendRules(const QString &rules) | ||
| { | ||
| QString tmpRules = rules; | ||
| tmpRules = tmpRules.replace(";", "\n"); | ||
| auto tmplist = tmpRules.split('\n'); | ||
| for (int i = 0; i < tmplist.count(); i++) | ||
| if (m_rules.contains(tmplist.at(i))) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Substring match suppresses DConfig rules When an environment rule contains a DConfig rule as a substring but is not the same rule, Compare normalized individual rules for exact equality rather than using substring containment. Prompt for AI agentsIn `src/util/logger.cpp` at line 70:
**Substring match suppresses DConfig rules**
When an environment rule contains a DConfig rule as a substring but is not the same rule, `m_rules.contains` treats any substring match as a duplicate, so an environment rule such as `xorg.deepin.ocr*.debug=false` suppresses the distinct DConfig rule `org.deepin.ocr*.debug=false`. The DConfig policy is then not applied, and the application category follows other matching rules or Qt defaults instead.
Compare normalized individual rules for exact equality rather than using substring containment. |
||
| tmplist.removeAt(i); | ||
| i--; | ||
| } | ||
| if (tmplist.isEmpty()) | ||
| return; | ||
| m_rules.isEmpty() ? m_rules = tmplist.join("\n") | ||
| : m_rules += "\n" + tmplist.join("\n"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| #ifndef DEEPIN_OCR_LOGGER_H | ||
| #define DEEPIN_OCR_LOGGER_H | ||
|
|
||
| #include <QObject> | ||
| #include <dtkcore_global.h> | ||
|
|
||
| #ifdef DTKCORE_CLASS_DConfig | ||
| DCORE_BEGIN_NAMESPACE | ||
| class DConfig; | ||
| DCORE_END_NAMESPACE | ||
| #endif | ||
|
|
||
| // 动态日志规则监听器:从 DConfig(org.deepin.ocr) 读取 log_rules 并应用, | ||
| // 监听运行中的配置变更,实现调试模式动态调整日志级别(参考 deepin-reader 的 MLogger)。 | ||
| // 必须在 Application 实例化之前创建,且实例需存活整个进程,否则 valueChanged 监听失效。 | ||
| class MLogger : public QObject | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| explicit MLogger(QObject *parent = nullptr); | ||
| ~MLogger() override; | ||
|
|
||
| inline QString rules() const { return m_rules; } | ||
| void setRules(const QString &rules); | ||
|
|
||
| private: | ||
| void appendRules(const QString &rules); | ||
|
|
||
| private: | ||
| QString m_rules; | ||
| #ifdef DTKCORE_CLASS_DConfig | ||
| Dtk::Core::DConfig *m_config; | ||
| #endif | ||
| }; | ||
|
|
||
| #endif // LOGGER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High · Application exits during startup
When DConfig::create returns null,
MLoggerimmediately dereferencesm_configto readlog_rules, so construction terminates the process before the application starts.Check
m_configbefore readinglog_rulesand continue with the captured environment rules when creation fails.Prompt for AI agents