Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
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