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..33df0356 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,22 @@ static void appendOrganizationAndApp(QString &path) #endif } +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 { @@ -49,6 +65,8 @@ class DLogManagerPrivate { } } + void initLoggingRules(); + QString m_format; QString m_logPath; ConsoleAppender* m_consoleAppender = nullptr; @@ -60,6 +78,38 @@ class DLogManagerPrivate { Q_DECLARE_PUBLIC(DLogManager) }; + +void DLogManagerPrivate::initLoggingRules() +{ + if (qEnvironmentVariableIsSet("DTK_DISABLED_LOGGING_RULES")) + return; + + QString appId = qgetenv("DTK_LOGGING_APP_ID"); + if (!appId.isEmpty()) + m_loggingRulesConfig = createDConfig(appId); + + QString dsgAppId = DSGApplication::id(); + if (!m_loggingRulesConfig && !appId.isEmpty() && appId != dsgAppId) + m_loggingRulesConfig = createDConfig(dsgAppId); + + if (!m_loggingRulesConfig) { + qWarning() << "Logging rules config create failed"; + return; + } + + auto updateLoggingRules = [this](const QString & key) { + if (key != RULES_KEY) + return; + + const QVariant &var = m_loggingRulesConfig->value(RULES_KEY); + if (var.isValid() && !var.toString().isEmpty()) { + QLoggingCategory::setFilterRules(var.toString().replace(";", "\n")); + } + }; + + updateLoggingRules(RULES_KEY); + QObject::connect(m_loggingRulesConfig, &DConfig::valueChanged, m_loggingRulesConfig, updateLoggingRules); +} /*! @~english \class Dtk::Core::DLogManager @@ -73,6 +123,8 @@ DLogManager::DLogManager() { spdlog::set_automatic_registration(true); spdlog::set_pattern("%v"); + + d_ptr->initLoggingRules(); } void DLogManager::initConsoleAppender(){ @@ -126,56 +178,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.