feat: 增加registerLoggingRulesWatcher接口

功能:监听 dconfig 的值然后设置应用的日志级别
目的:
1.取消QT_LOGGING_RULES需要在创建 application之前处理
因为QLoggingRegistry在构造函数中获取环境变量的值后不会监听环境变量的变化.
目前直接使用applicationName当做appId 的做法不可行.
2.不和应用的 applicationName耦合目前大部分应用的applicationName和dconfig
的appId是不一样的,需要修改应用的名字
可能存在多个 application 使用通过一个 dconfig
的情况,修改applicantionName的方法不可行.
PS:从 DConfig 获取默认 appId 的逻辑上看, DSGApplication应用会有问题
3.开发者可以选择是否需要设置以及监听日志级别变动

Log:
Task: https://pms.uniontech.com/task-view-303379.html
This commit is contained in:
YinJie 2023-12-20 15:17:10 +08:00 committed by mike
parent 63a62b14f4
commit 8ba05a2f80
2 changed files with 65 additions and 25 deletions

View File

@ -31,10 +31,18 @@ public:
static void setLogFormat(const QString &format); 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: private:
void initConsoleAppender(); void initConsoleAppender();
void initRollingFileAppender(); void initRollingFileAppender();
void initJournalAppender(); void initJournalAppender();
void initLoggingRules(const QString &appId);
QString joinPath(const QString &path, const QString &fileName); QString joinPath(const QString &path, const QString &fileName);
inline static DLogManager* instance(){ inline static DLogManager* instance(){

View File

@ -17,7 +17,6 @@
DCORE_BEGIN_NAMESPACE DCORE_BEGIN_NAMESPACE
#define RULES_KEY ("rules") #define RULES_KEY ("rules")
Q_GLOBAL_STATIC_WITH_ARGS(DConfig, _d_dconfig, ("org.deepin.dtk.loggingrules"));
// Courtesy qstandardpaths_unix.cpp // Courtesy qstandardpaths_unix.cpp
static void appendOrganizationAndApp(QString &path) static void appendOrganizationAndApp(QString &path)
@ -43,12 +42,19 @@ public:
, q_ptr(q) , q_ptr(q)
{ {
} }
~DLogManagerPrivate() {
if (m_loggingRulesConfig) {
delete m_loggingRulesConfig;
m_loggingRulesConfig = nullptr;
}
}
QString m_format; QString m_format;
QString m_logPath; QString m_logPath;
ConsoleAppender* m_consoleAppender = nullptr; ConsoleAppender* m_consoleAppender = nullptr;
RollingFileAppender* m_rollingFileAppender = nullptr; RollingFileAppender* m_rollingFileAppender = nullptr;
JournalAppender* m_journalAppender = nullptr; JournalAppender* m_journalAppender = nullptr;
DConfig* m_loggingRulesConfig = nullptr;
DLogManager *q_ptr = nullptr; DLogManager *q_ptr = nullptr;
Q_DECLARE_PUBLIC(DLogManager) Q_DECLARE_PUBLIC(DLogManager)
@ -67,30 +73,6 @@ DLogManager::DLogManager()
{ {
spdlog::set_automatic_registration(true); spdlog::set_automatic_registration(true);
spdlog::set_pattern("%v"); spdlog::set_pattern("%v");
/* QT_LOGGING_RULES环境变量设置日志的优先级最高会与dconfig的设置冲突
* unset掉使dconfig进行设置则使用该环境变量的值*/
QByteArray logRules = qgetenv("QT_LOGGING_RULES");
qunsetenv("QT_LOGGING_RULES");
if (!logRules.isEmpty()) {
QLoggingCategory::setFilterRules(logRules.replace(";", "\n"));
}
if (_d_dconfig->isValid()) {
auto updateLoggingRules = [](const QString & key) {
if (key != RULES_KEY)
return;
const QVariant &var = _d_dconfig->value(RULES_KEY);
if (var.isValid() && !var.toString().isEmpty()) {
QLoggingCategory::setFilterRules(var.toString().replace(";", "\n"));
}
};
updateLoggingRules(RULES_KEY);
QObject::connect(_d_dconfig, &DConfig::valueChanged, _d_dconfig, updateLoggingRules);
}
} }
void DLogManager::initConsoleAppender(){ void DLogManager::initConsoleAppender(){
@ -144,6 +126,56 @@ void DLogManager::registerJournalAppender()
DLogManager::instance()->initJournalAppender(); DLogManager::instance()->initJournalAppender();
} }
void DLogManager::initLoggingRules(const QString &appId)
{
if (appId.isEmpty()) {
qWarning() << "App id is empty, logging rules won't take effect";
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"));
}
Q_D(DLogManager);
d->m_loggingRulesConfig = DConfig::create(appId, "org.deepin.dtk.loggingrules");
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;
}
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 @~english
\brief Return the path file log storage. \brief Return the path file log storage.