From 1e5e1b9b1c0addf88f86d0b32b2a8a2e0e6f5cd1 Mon Sep 17 00:00:00 2001 From: ck Date: Fri, 10 May 2024 15:36:23 +0800 Subject: [PATCH] refactor: remove LoggingRules interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除相关接口, 默认启用(可以用 DTK_DISABLED_LOGGING_RULES 禁用) - 优先使用 dsgAppId , 用户可以通过 DTK_LOGGING_FALLBACK_APPID 指定 fallbackAppId - 有配置过 rules 时, 优先使用(即非默认值优先) - fallbackConfig 发生变化时会检查 dsgConfig 是否修改过,是则忽略 --- include/log/LogManager.h | 8 --- src/log/LogManager.cpp | 146 ++++++++++++++++++++++++--------------- 2 files changed, 91 insertions(+), 63 deletions(-) diff --git a/include/log/LogManager.h b/include/log/LogManager.h index deeccf75..ab07805c 100644 --- a/include/log/LogManager.h +++ b/include/log/LogManager.h @@ -31,18 +31,10 @@ class LIBDTKCORESHARED_EXPORT DLogManager static void setLogFormat(const QString &format); - /*! - * \brief 监听 org.deepin.dtk.log 的变化动态调整应用的日志输出规则 - * 此方法应该在创建 QApplication 之前调用,否则 QT_LOGGING_RULES 环境变量会覆盖 dconfig 的的值 - * \a logFilePath 指定 dconfig 的 appId - */ - static void registerLoggingRulesWatcher(const QString &appId); - private: void initConsoleAppender(); void initRollingFileAppender(); void initJournalAppender(); - void initLoggingRules(const QString &appId); QString joinPath(const QString &path, const QString &fileName); inline static DLogManager* instance(){ diff --git a/src/log/LogManager.cpp b/src/log/LogManager.cpp index 561e19c7..17906721 100644 --- a/src/log/LogManager.cpp +++ b/src/log/LogManager.cpp @@ -5,7 +5,7 @@ #include #include "LogManager.h" #include "dconfig.h" - +#include #include #include #include @@ -33,6 +33,32 @@ static void appendOrganizationAndApp(QString &path) #endif } +static void updateLoggingRules(const DConfig *config) +{ + if (!config) + return; + + const QVariant &var = config->value(RULES_KEY); + if (var.isValid()) + QLoggingCategory::setFilterRules(var.toString().replace(";", "\n")); +} + +static DConfig *createDConfig(const QString &appId) +{ + if (appId.isEmpty()) + return nullptr; + + DConfig *config = DConfig::create(appId, "org.deepin.dtk.preference"); + if (!config->isValid()) { + qWarning() << "Logging rules config is invalid, please check `appId` [" << appId << "]arg is correct"; + delete config; + config = nullptr; + return nullptr; + } + + return config; +} + #define DEFAULT_FMT "%{time}{yyyy-MM-dd, HH:mm:ss.zzz} [%{type:-7}] [%{file:-20} %{function:-35} %{line}] %{message}" class DLogManagerPrivate { @@ -43,23 +69,81 @@ class DLogManagerPrivate { { } ~DLogManagerPrivate() { - if (m_loggingRulesConfig) { - delete m_loggingRulesConfig; - m_loggingRulesConfig = nullptr; + if (m_dsgConfig) { + delete m_dsgConfig; + m_dsgConfig = nullptr; + } + if (m_fallbackConfig) { + delete m_fallbackConfig; + m_fallbackConfig = nullptr; } } + void initLoggingRules(); + QString m_format; QString m_logPath; ConsoleAppender* m_consoleAppender = nullptr; RollingFileAppender* m_rollingFileAppender = nullptr; JournalAppender* m_journalAppender = nullptr; - DConfig* m_loggingRulesConfig = nullptr; + DConfig * m_dsgConfig = nullptr; + DConfig * m_fallbackConfig = nullptr; DLogManager *q_ptr = nullptr; Q_DECLARE_PUBLIC(DLogManager) }; + +void DLogManagerPrivate::initLoggingRules() +{ + if (qEnvironmentVariableIsSet("DTK_DISABLED_LOGGING_RULES")) + return; + + DConfig *dsgConfig = nullptr, *fallbackConfig = nullptr; + // 1. 未指定 fallbackId 时,以 dsgAppId 为准 + QString dsgAppId = DSGApplication::id(); + dsgConfig = createDConfig(dsgAppId); + QObject::connect(fallbackConfig, &DConfig::valueChanged, fallbackConfig, [=](const QString &key){ + if (key != RULES_KEY) + return; + + updateLoggingRules(dsgConfig); + }); + + bool isDsgCached = dsgConfig && !dsgConfig->isDefaultValue(RULES_KEY); + bool fbCached = false; + + m_dsgConfig = dsgConfig; + + QString fallbackId = qgetenv("DTK_LOGGING_FALLBACK_APPID"); + // 2. fallbackId 和 dsgAppId 非空且不等时,都创建和监听变化 + if (!fallbackId.isEmpty() && fallbackId != dsgAppId) { + fallbackConfig = createDConfig(dsgAppId); + QObject::connect(fallbackConfig, &DConfig::valueChanged, fallbackConfig, [=](const QString &key){ + if (key != RULES_KEY) + return; + // 3. valuechanged 时,如果 dsgAppId 对应的配置非默认值则忽略。。。 + if (dsgConfig && !dsgConfig->isDefaultValue(RULES_KEY)) { + qWarning() << "Logging rules will be ignored from fallback logging appid"; + return; + } + + updateLoggingRules(fallbackConfig); + }); + + fbCached = fallbackConfig && !fallbackConfig->isDefaultValue(RULES_KEY); + m_fallbackConfig = fallbackConfig; + } + + // 4. 默认值和非默认值时,非默认值优先 + if (isDsgCached) { + updateLoggingRules(dsgConfig); + } else if (fbCached) { + updateLoggingRules(fallbackConfig); + } else{ + // both default value, do nonthing... + } +} /*! @~english \class Dtk::Core::DLogManager @@ -73,6 +157,8 @@ DLogManager::DLogManager() { spdlog::set_automatic_registration(true); spdlog::set_pattern("%v"); + + d_ptr->initLoggingRules(); } void DLogManager::initConsoleAppender(){ @@ -126,56 +212,6 @@ void DLogManager::registerJournalAppender() DLogManager::instance()->initJournalAppender(); } -void DLogManager::initLoggingRules(const QString &appId) -{ - if (appId.isEmpty()) { - qWarning() << "App id is empty, logging rules won't take effect"; - return; - } - - Q_D(DLogManager); - d->m_loggingRulesConfig = DConfig::create(appId, "org.deepin.dtk.preference"); - if (!d->m_loggingRulesConfig) { - qWarning() << "Create logging rules dconfig object failed, logging rules won't take effect"; - return; - } - - if (!d->m_loggingRulesConfig->isValid()) { - qWarning() << "Logging rules config is invalid, please check `appId` arg is correct"; - delete d->m_loggingRulesConfig; - d->m_loggingRulesConfig = nullptr; - return; - } - - // QT_LOGGING_RULES环境变量设置日志的优先级最高 - // QLoggingRegistry 初始化时会获取 QT_LOGGING_RULES 的值并保存,后续重置了环境变量 QLoggingRegistry 不会进行同步 - // 需要在 QLoggingRegistry 初始化之前重置 QT_LOGGING_RULES 的值 - QByteArray logRules = qgetenv("QT_LOGGING_RULES"); - qunsetenv("QT_LOGGING_RULES"); - - if (!logRules.isEmpty()) { - QLoggingCategory::setFilterRules(logRules.replace(";", "\n")); - } - - auto updateLoggingRules = [d](const QString & key) { - if (key != RULES_KEY) - return; - - const QVariant &var = d->m_loggingRulesConfig->value(RULES_KEY); - if (var.isValid() && !var.toString().isEmpty()) { - QLoggingCategory::setFilterRules(var.toString().replace(";", "\n")); - } - }; - - updateLoggingRules(RULES_KEY); - QObject::connect(d->m_loggingRulesConfig, &DConfig::valueChanged, d->m_loggingRulesConfig, updateLoggingRules); -} - -void DLogManager::registerLoggingRulesWatcher(const QString &appId) -{ - DLogManager::instance()->initLoggingRules(appId); -} - /*! @~english \brief Return the path file log storage.