Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loggingrules dconfig support (cherry-pick from gerrit) #413

Merged
merged 9 commits into from
May 17, 2024
10 changes: 8 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

VERSION = $(DEB_VERSION_UPSTREAM)
PACK_VER = $(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$1}')
# Fix: invalid digit "8" in octal constant. e.g. u008 ==> 008 ==> 8
BUILD_VER = $(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$2}' | sed 's/[^0-9]//g' | awk '{print int($$1)}')

# Calculate build version:
# 5.6.8 -> 0; 5.6.8.7 -> 7; 5.6.8+u001 -> 1; 5.6.8.7+u001 -> 7; 5.6.8.0+u001 -> 0
BUILD_VER = $(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$1}' | awk -F'.' '{print $$4}' | sed 's/[^0-9]//g' | awk '{print int($$1)}')
ifeq ($(BUILD_VER), 0)
ifeq ($(shell expr $(shell echo "$(VERSION)" | awk -F. '{print NF-1}') '<' 3), 1)
BUILD_VER=$(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$2}' | sed 's/[^0-9]//g' | awk '{print int($$1)}')
endif
endif

%:
dh $@
Expand Down
3 changes: 2 additions & 1 deletion include/global/dsysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class LIBDTKCORESHARED_EXPORT DSysInfo
DeepinDesktop,
DeepinProfessional,
DeepinServer,
DeepinPersonal
DeepinPersonal,
DeepinMilitary
};

enum LogoType {
Expand Down
44 changes: 35 additions & 9 deletions src/dsysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QStandardPaths>
#include <QDateTime>
#include <QRegularExpression>
#include <QLoggingCategory>
#include <qmath.h>

#ifdef Q_OS_LINUX
Expand All @@ -37,6 +38,12 @@ static inline bool inTest()

DCORE_BEGIN_NAMESPACE

#ifdef QT_DEBUG
Q_LOGGING_CATEGORY(logSysInfo, "dtk.dsysinfo")
#else
Q_LOGGING_CATEGORY(logSysInfo, "dtk.dsysinfo", QtInfoMsg)
#endif

class Q_DECL_HIDDEN DSysInfoPrivate
{
public:
Expand Down Expand Up @@ -150,7 +157,7 @@ bool DSysInfoPrivate::splitA_BC_DMode()

void DSysInfoPrivate::ensureDeepinInfo()
{
if (static_cast<int>(deepinType) >= 0 && !inTest())
if (static_cast<int>(deepinType) > 0 && !inTest())
return;

if (inTest())
Expand Down Expand Up @@ -220,6 +227,8 @@ void DSysInfoPrivate::ensureDeepinInfo()
deepinType = DSysInfo::DeepinServer;
} else if (deepin_type == "Personal") {
deepinType = DSysInfo::DeepinPersonal;
} else if (deepin_type == "Military") {
deepinType = DSysInfo::DeepinMilitary;
} else {
deepinType = DSysInfo::UnknownDeepin;
}
Expand Down Expand Up @@ -423,7 +432,7 @@ static bool readLsbRelease(DSysInfoPrivate *info)

void DSysInfoPrivate::ensureReleaseInfo()
{
if (productType >= 0 && !inTest()) {
kegechen marked this conversation as resolved.
Show resolved Hide resolved
if (productType > 0 && !inTest()) {
return;
}

Expand Down Expand Up @@ -1122,18 +1131,35 @@ qint64 DSysInfo::memoryInstalledSize()
}

const QByteArray &lshwInfoJson = lshw.readAllStandardOutput();
QJsonArray lshwResultArray = QJsonDocument::fromJson(lshwInfoJson).array();
if (!lshwResultArray.isEmpty()) {
QJsonValue memoryHwInfo = lshwResultArray.first();
QString id = memoryHwInfo.toObject().value("id").toString();
Q_ASSERT(id == "memory");
siGlobal->memoryInstalledSize = memoryHwInfo.toObject().value("size").toDouble(); // TODO: check "units" is "bytes" ?

QJsonParseError error;
auto doc = QJsonDocument::fromJson(lshwInfoJson, &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(logSysInfo(), "parse failed, expect json doc from lshw command");
return -1;
}

if (!doc.isArray()) {
qCWarning(logSysInfo(), "parse failed, expect array");
return -1;
}

QJsonArray lshwResultArray = doc.array();
for (const QJsonValue value : lshwResultArray) {
QJsonObject obj = value.toObject();
if (obj.contains("id") && obj.value("id").toString() == "memory") {
siGlobal->memoryInstalledSize = obj.value("size").toDouble(); // TODO: check "units" is "bytes" ?
break;
}
}
}

Q_ASSERT(siGlobal->memoryInstalledSize > 0);

return siGlobal->memoryInstalledSize;
#endif
#else
return -1;
#endif
}

/*!
Expand Down
5 changes: 5 additions & 0 deletions src/log/AbstractStringAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ QString AbstractStringAppender::formattedString(const QDateTime &time, Logger::L
bool withcolor) const
{
QString f = format();

// dtkcore无法正确解析Qt的日志格式,dtk默认的日志格式并未和Qt统一,解析方式需要兼容两种不同的格式。
if (f.contains(QLatin1String("time ")))
f.replace(f.indexOf(' ', f.indexOf(QLatin1String("time")) + QLatin1String("time").size()), 1, QLatin1String("}{"));

const int size = f.size();

QString result;
Expand Down
2 changes: 1 addition & 1 deletion src/log/ConsoleAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ DCORE_BEGIN_NAMESPACE

ConsoleAppender::ConsoleAppender()
: AbstractStringAppender()
,m_ignoreEnvPattern(false)
, m_ignoreEnvPattern(false)
{
if (!spdlog::get("console")) {
auto clogger = spdlog::stdout_color_mt("console");
Expand Down
68 changes: 68 additions & 0 deletions src/log/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <QtCore>
#include "LogManager.h"
#include "dconfig.h"
#include <DSGApplication>
#include <Logger.h>
#include <ConsoleAppender.h>
#include <RollingFileAppender.h>
Expand All @@ -14,6 +16,7 @@

DCORE_BEGIN_NAMESPACE

#define RULES_KEY ("rules")
// Courtesy qstandardpaths_unix.cpp
static void appendOrganizationAndApp(QString &path)
{
Expand All @@ -39,16 +42,79 @@ class DLogManagerPrivate {
{
}

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;
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;

// 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()
Copy link
Contributor

@18202781743 18202781743 May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在应用不重启的情况下,不能重置之前设置的值了,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

似的,reset 之后,isDefaultValue 是 true,也就是说不能用reset的方式来清空rules,只能删除的方式清空

{
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 @@ -62,6 +128,8 @@ DLogManager::DLogManager()
{
spdlog::set_automatic_registration(true);
spdlog::set_pattern("%v");

d_ptr->initLoggingRules();
}

void DLogManager::initConsoleAppender(){
Expand Down
Loading