Skip to content

Commit

Permalink
sync: from linuxdeepin/dtkcore
Browse files Browse the repository at this point in the history
Synchronize source files from linuxdeepin/dtkcore.

Source-pull-request: linuxdeepin/dtkcore#418
  • Loading branch information
deepin-ci-robot committed May 17, 2024
1 parent 831c045 commit 002e560
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 15 deletions.
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
30 changes: 26 additions & 4 deletions src/dsgapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/syscall.h>
#include <unistd.h>

#include <QFile>
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
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()) {
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()
{
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

0 comments on commit 002e560

Please sign in to comment.