Skip to content

Commit

Permalink
refactor: remove LoggingRules interface
Browse files Browse the repository at this point in the history
- 移除相关接口, 默认启用(可以用 DTK_DISABLED_LOGGING_RULES 禁用)
- 优先使用 dsgAppId , 用户可以通过 DTK_LOGGING_FALLBACK_APPID 指定
  fallbackAppId
- 有配置过 rules 时, 优先使用(即非默认值优先)
- fallbackConfig 发生变化时会检查 dsgConfig 是否修改过,是则忽略
  • Loading branch information
kegechen committed May 14, 2024
1 parent 06c34a9 commit e825ed1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 64 deletions.
8 changes: 0 additions & 8 deletions include/log/LogManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
124 changes: 68 additions & 56 deletions src/log/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QtCore>
#include "LogManager.h"
#include "dconfig.h"

#include <DSGApplication>
#include <Logger.h>
#include <ConsoleAppender.h>
#include <RollingFileAppender.h>
Expand Down Expand Up @@ -43,23 +43,83 @@ class DLogManagerPrivate {
{
}
~DLogManagerPrivate() {
if (m_loggingRulesConfig) {
delete m_loggingRulesConfig;
m_loggingRulesConfig = nullptr;
}
}

DConfig *createDConfig(const QString &appId);
void initLoggingRules();
void updateLoggingRules();

QString m_format;
QString m_logPath;
ConsoleAppender* m_consoleAppender = nullptr;
RollingFileAppender* m_rollingFileAppender = nullptr;
JournalAppender* m_journalAppender = nullptr;
DConfig* m_loggingRulesConfig = nullptr;
QScopedPointer<DConfig> m_dsgConfig;
QScopedPointer<DConfig> m_fallbackConfig;

DLogManager *q_ptr = nullptr;
Q_DECLARE_PUBLIC(DLogManager)

};

DConfig *DLogManagerPrivate::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;
}

QObject::connect(config, &DConfig::valueChanged, config, [this](const QString &key) {
if (key != RULES_KEY)
return;

updateLoggingRules();
});

return config;
}

void DLogManagerPrivate::initLoggingRules()
{
if (qEnvironmentVariableIsSet("DTK_DISABLED_LOGGING_RULES"))
return;

DConfig *dsgConfig = nullptr, *fallbackConfig = nullptr;
// 1. 未指定 fallbackId 时,以 dsgAppId 为准
QString dsgAppId = DSGApplication::id();
m_dsgConfig.reset(createDConfig(dsgAppId));

QString fallbackId = qgetenv("DTK_LOGGING_FALLBACK_APPID");
// 2. fallbackId 和 dsgAppId 非空且不等时,都创建和监听变化
if (!fallbackId.isEmpty() && fallbackId != dsgAppId)
m_fallbackConfig.reset(createDConfig(fallbackId));

// 3. 默认值和非默认值时,非默认值优先
updateLoggingRules();

}

void DLogManagerPrivate::updateLoggingRules()
{
QVariant var;
// 4. 优先看 dsgConfig 是否默认值,其次 fallback 是否默认值
if (m_dsgConfig && !m_dsgConfig->isDefaultValue(RULES_KEY)) {
var = m_dsgConfig->value(RULES_KEY);
} else if (m_fallbackConfig && !m_fallbackConfig->isDefaultValue(RULES_KEY)) {
var = m_fallbackConfig->value(RULES_KEY);
} else {
// do nothing..
}

if (var.isValid())
QLoggingCategory::setFilterRules(var.toString().replace(";", "\n"));
}
/*!
@~english
\class Dtk::Core::DLogManager
Expand All @@ -73,6 +133,8 @@ DLogManager::DLogManager()
{
spdlog::set_automatic_registration(true);
spdlog::set_pattern("%v");

d_ptr->initLoggingRules();
}

void DLogManager::initConsoleAppender(){
Expand Down Expand Up @@ -126,56 +188,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.
Expand Down

0 comments on commit e825ed1

Please sign in to comment.