From 002e560e934d13e3aa2d21a0f79976b6f2b125b7 Mon Sep 17 00:00:00 2001 From: deepin-ci-robot Date: Fri, 17 May 2024 09:45:53 +0000 Subject: [PATCH] sync: from linuxdeepin/dtkcore Synchronize source files from linuxdeepin/dtkcore. Source-pull-request: https://github.com/linuxdeepin/dtkcore/pull/418 --- include/global/dsysinfo.h | 3 +- src/dsgapplication.cpp | 30 +++++++++++-- src/dsysinfo.cpp | 44 +++++++++++++++---- src/log/AbstractStringAppender.cpp | 5 +++ src/log/ConsoleAppender.cpp | 2 +- src/log/LogManager.cpp | 68 ++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 15 deletions(-) diff --git a/include/global/dsysinfo.h b/include/global/dsysinfo.h index c159461..24293b8 100644 --- a/include/global/dsysinfo.h +++ b/include/global/dsysinfo.h @@ -38,7 +38,8 @@ class LIBDTKCORESHARED_EXPORT DSysInfo DeepinDesktop, DeepinProfessional, DeepinServer, - DeepinPersonal + DeepinPersonal, + DeepinMilitary }; enum LogoType { diff --git a/src/dsgapplication.cpp b/src/dsgapplication.cpp index cab17fb..f1e73a2 100644 --- a/src/dsgapplication.cpp +++ b/src/dsgapplication.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,15 @@ static bool isServiceActivatable(const QString &service) return activatableNames.value().contains(service); } +// Format appId to valid. +static QByteArray formatAppId(const QByteArray &appId) +{ + static const QRegularExpression regex("[^\\w\\-\\.]"); + QString format(QString::fromStdString(appId.toStdString())); + format = format.replace(regex, QStringLiteral(".")); + return format.toLocal8Bit(); +} + QByteArray DSGApplication::id() { static QByteArray selfId = getSelfAppId(); @@ -55,11 +65,23 @@ QByteArray DSGApplication::id() QByteArray result = selfId; if (!qEnvironmentVariableIsSet("DTK_DISABLED_FALLBACK_APPID")) { result = QCoreApplication::applicationName().toLocal8Bit(); + if (result.isEmpty()) { + QFile file("/proc/self/cmdline"); + if (file.open(QIODevice::ReadOnly)) + result = file.readLine(); + } + if (result.isEmpty()) { + const QFile file(QFile::symLinkTarget("/proc/self/exe")); + if (file.exists()) + result = file.fileName().toLocal8Bit(); + } + if (!result.isEmpty()) { + result = formatAppId(result); + qCDebug(dsgApp) << "The applicatiion ID is fallback to " << result; + } } - Q_ASSERT(!result.isEmpty()); - if (result.isEmpty()) { - qt_assert("The application ID is empty", __FILE__, __LINE__); - } + if (result.isEmpty()) + qCWarning(dsgApp) << "The application ID is empty."; return result; } diff --git a/src/dsysinfo.cpp b/src/dsysinfo.cpp index b964f59..052ffa5 100644 --- a/src/dsysinfo.cpp +++ b/src/dsysinfo.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef Q_OS_LINUX @@ -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: @@ -150,7 +157,7 @@ bool DSysInfoPrivate::splitA_BC_DMode() void DSysInfoPrivate::ensureDeepinInfo() { - if (static_cast(deepinType) >= 0 && !inTest()) + if (static_cast(deepinType) > 0 && !inTest()) return; if (inTest()) @@ -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; } @@ -423,7 +432,7 @@ static bool readLsbRelease(DSysInfoPrivate *info) void DSysInfoPrivate::ensureReleaseInfo() { - if (productType >= 0 && !inTest()) { + if (productType > 0 && !inTest()) { return; } @@ -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 } /*! diff --git a/src/log/AbstractStringAppender.cpp b/src/log/AbstractStringAppender.cpp index 78ae219..8cf3518 100644 --- a/src/log/AbstractStringAppender.cpp +++ b/src/log/AbstractStringAppender.cpp @@ -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; diff --git a/src/log/ConsoleAppender.cpp b/src/log/ConsoleAppender.cpp index c89a9c4..6969b5b 100644 --- a/src/log/ConsoleAppender.cpp +++ b/src/log/ConsoleAppender.cpp @@ -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"); diff --git a/src/log/LogManager.cpp b/src/log/LogManager.cpp index 6d0573a..3f99ee8 100644 --- a/src/log/LogManager.cpp +++ b/src/log/LogManager.cpp @@ -4,6 +4,8 @@ #include #include "LogManager.h" +#include "dconfig.h" +#include #include #include #include @@ -14,6 +16,7 @@ DCORE_BEGIN_NAMESPACE +#define RULES_KEY ("rules") // Courtesy qstandardpaths_unix.cpp static void appendOrganizationAndApp(QString &path) { @@ -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 m_dsgConfig; + QScopedPointer 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() +{ + 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 @@ -62,6 +128,8 @@ DLogManager::DLogManager() { spdlog::set_automatic_registration(true); spdlog::set_pattern("%v"); + + d_ptr->initLoggingRules(); } void DLogManager::initConsoleAppender(){