Skip to content

Commit

Permalink
refactor: remove LoggingRules interface
Browse files Browse the repository at this point in the history
- 移除相关接口, 默认启用(可以用 DTK_DISABLED_LOGGING_RULES 禁用)
- 如果不好获取 APPID 可以通过 DTK_LOGGING_APP_ID 类配置,没有的话 fallback
  到  DSGApplication::id() 获取 APPID
  • Loading branch information
kegechen committed May 11, 2024
1 parent d5f7366 commit a260433
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 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
104 changes: 53 additions & 51 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 @@ -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 {
Expand All @@ -49,6 +65,8 @@ class DLogManagerPrivate {
}
}

void initLoggingRules();

QString m_format;
QString m_logPath;
ConsoleAppender* m_consoleAppender = nullptr;
Expand All @@ -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
Expand All @@ -73,6 +123,8 @@ DLogManager::DLogManager()
{
spdlog::set_automatic_registration(true);
spdlog::set_pattern("%v");

d_ptr->initLoggingRules();
}

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

0 comments on commit a260433

Please sign in to comment.