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
6 changes: 5 additions & 1 deletion basestruct/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
# SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -12,6 +12,8 @@ find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
Widgets
DBus
REQUIRED)
# DConfig(动态日志规则)需要 DTK Core
find_package(Dtk${DTK_VERSION_MAJOR}Core REQUIRED)


set(CMAKE_CXX_STANDARD 14)
Expand All @@ -26,13 +28,15 @@ set(BASESTRUCT_SRCS ${BASESTRUCT_SOURCES} ${BASESTRUCT_HEADERS} )

add_library(${PROJECT_NAME} STATIC ${BASESTRUCT_SRCS})
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
Qt${QT_VERSION_MAJOR}::GuiPrivate
${PROJECT_BINARY_DIR}
)

target_link_libraries(${PROJECT_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::DBus
Dtk${DTK_VERSION_MAJOR}::Core
Threads::Threads
)

Expand Down
23 changes: 23 additions & 0 deletions basestruct/ddlog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

#ifndef DDLOG_H
#define DDLOG_H

#include <QLoggingCategory>

Check warning on line 8 in basestruct/ddlog.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.

// 全项目统一的日志分类声明,便于 DConfig(org.deepin.dde.diskmanager) 按应用统一过滤。
// 分类名必须以 org.deepin. 前缀、与 desktop 名(deepin-diskmanager)对应,
// 调试模式(deepin-debug-config)按 "org.deepin.dde.diskmanager*.debug" 等通配规则分级开关。

// 主程序 deepin-diskmanager(GUI 前端进程)
Q_DECLARE_LOGGING_CATEGORY(appLog)

// 后台服务 deepin-diskmanager-service(systemd/D-Bus 激活的 root 守护进程)
Q_DECLARE_LOGGING_CATEGORY(serviceLog)

// 公共基础库 basestruct(被主程序与后台服务共同链接,在两个进程中都会输出)
Q_DECLARE_LOGGING_CATEGORY(baseLog)

#endif // DDLOG_H
47 changes: 24 additions & 23 deletions basestruct/deviceinfo.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

#include "ddlog.h"
#include "deviceinfo.h"
#include <QDBusArgument>

Check warning on line 7 in basestruct/deviceinfo.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusArgument> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 8 in basestruct/deviceinfo.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

/*********************************** stCustest *********************************************/
QDBusArgument &operator<<(QDBusArgument &argument, const stCustest &stcus)
{
// qDebug() << "Starting stCustest serialization";
// qCDebug(baseLog) << "Starting stCustest serialization";
argument.beginStructure();
argument << stcus.m_length
<< stcus.m_heads
<< stcus.m_path;
argument.endStructure();
// qDebug() << "Completed stCustest serialization";
// qCDebug(baseLog) << "Completed stCustest serialization";
return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, stCustest &stcus)
{
// qDebug() << "Starting stCustest deserialization";
// qCDebug(baseLog) << "Starting stCustest deserialization";
argument.beginStructure();
argument >> stcus.m_length
>> stcus.m_heads
>> stcus.m_path;

argument.endStructure();
// qDebug() << "Completed stCustest deserialization";
// qCDebug(baseLog) << "Completed stCustest deserialization";
return argument;
}
/*********************************** HardDiskInfo *********************************************/
QDBusArgument &operator<<(QDBusArgument &argument, const HardDiskInfo &inhdinfo)
{
// qDebug() << "Starting HardDiskInfo serialization";
// qCDebug(baseLog) << "Starting HardDiskInfo serialization";
argument.beginStructure();
argument << inhdinfo.m_model
<< inhdinfo.m_vendor
Expand All @@ -51,13 +52,13 @@
<< inhdinfo.m_firmwareVersion
<< inhdinfo.m_speed;
argument.endStructure();
// qDebug() << "Completed HardDiskInfo serialization";
// qCDebug(baseLog) << "Completed HardDiskInfo serialization";
return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, HardDiskInfo &inhdinfo)
{
// qDebug() << "Starting HardDiskInfo deserialization";
// qCDebug(baseLog) << "Starting HardDiskInfo deserialization";
argument.beginStructure();
argument >> inhdinfo.m_model
>> inhdinfo.m_vendor
Expand All @@ -74,13 +75,13 @@
>> inhdinfo.m_firmwareVersion
>> inhdinfo.m_speed;
argument.endStructure();
// qDebug() << "Completed HardDiskInfo deserialization";
// qCDebug(baseLog) << "Completed HardDiskInfo deserialization";
return argument;
}
/*********************************** HardDiskStatusInfo *********************************************/
QDBusArgument &operator<<(QDBusArgument &argument, const HardDiskStatusInfo &inhdinfo)
{
// qDebug() << "Starting HardDiskStatusInfo serialization";
// qCDebug(baseLog) << "Starting HardDiskStatusInfo serialization";
argument.beginStructure();
argument << inhdinfo.m_id
<< inhdinfo.m_attributeName
Expand All @@ -93,12 +94,12 @@
<< inhdinfo.m_whenFailed
<< inhdinfo.m_rawValue;
argument.endStructure();
// qDebug() << "Completed HardDiskStatusInfo serialization";
// qCDebug(baseLog) << "Completed HardDiskStatusInfo serialization";
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &argument, HardDiskStatusInfo &inhdinfo)
{
// qDebug() << "Starting HardDiskStatusInfo deserialization";
// qCDebug(baseLog) << "Starting HardDiskStatusInfo deserialization";
argument.beginStructure();
argument >> inhdinfo.m_id
>> inhdinfo.m_attributeName
Expand All @@ -112,13 +113,13 @@
>> inhdinfo.m_rawValue;
argument.endStructure();

// qDebug() << "Completed HardDiskStatusInfo deserialization";
// qCDebug(baseLog) << "Completed HardDiskStatusInfo deserialization";
return argument;
}
/*********************************** DeviceInfo *********************************************/
QDBusArgument &operator<<(QDBusArgument &argument, const DeviceInfo &info)
{
// qDebug() << "Starting DeviceInfo serialization";
// qCDebug(baseLog) << "Starting DeviceInfo serialization";
argument.beginStructure();
argument << info.m_length
<< info.m_heads
Expand All @@ -142,13 +143,13 @@
<< static_cast<int>(info.m_luksFlag)
<< info.m_crySupport;
argument.endStructure();
// qDebug() << "Completed DeviceInfo serialization";
// qCDebug(baseLog) << "Completed DeviceInfo serialization";
return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, DeviceInfo &info)
{
// qDebug() << "Starting DeviceInfo deserialization";
// qCDebug(baseLog) << "Starting DeviceInfo deserialization";
argument.beginStructure();
int flag = 0;
int flag2 = 0;
Expand Down Expand Up @@ -176,26 +177,26 @@
info.m_vgFlag = static_cast<LVMFlag>(flag);
info.m_luksFlag = static_cast<LUKSFlag>(flag2);
argument.endStructure();
// qDebug() << "Completed DeviceInfo deserialization";
// qCDebug(baseLog) << "Completed DeviceInfo deserialization";
return argument;
}

DeviceInfo::DeviceInfo()
: m_readonly(false)
{
qDebug() << "Initializing DeviceInfo with default values";
qCDebug(baseLog) << "Initializing DeviceInfo with default values";
m_length = m_heads = m_sectors = m_cylinders = m_cylsize = m_sectorSize = m_maxPrims = m_highestBusy = m_maxPartitionNameLength = 0;
m_path = m_model = m_serialNumber = m_disktype = m_mediaType = m_interface = QString("");
m_partition.clear();
m_vgFlag = LVM_FLAG_NOT_PV;
m_vglist.clear();
m_luksFlag = LUKSFlag::NOT_CRYPT_LUKS;
qDebug() << "DeviceInfo initialization completed";
qCDebug(baseLog) << "DeviceInfo initialization completed";
}
/*********************************** WipeAction *********************************************/
QDBusArgument &operator<<(QDBusArgument &argument, const WipeAction &data)
{
// qDebug() << "Starting WipeAction serialization";
// qCDebug(baseLog) << "Starting WipeAction serialization";
argument.beginStructure();
argument << data.m_fstype
<< data.m_path
Expand All @@ -209,13 +210,13 @@
<< data.m_decryptStr
<< data.m_dmName;
argument.endStructure();
// qDebug() << "Completed WipeAction serialization";
// qCDebug(baseLog) << "Completed WipeAction serialization";
return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, WipeAction &data)
{
// qDebug() << "Starting WipeAction deserialization";
// qCDebug(baseLog) << "Starting WipeAction deserialization";
argument.beginStructure();
int flag1, flag2;
argument >> data.m_fstype
Expand All @@ -232,6 +233,6 @@
data.m_luksFlag = static_cast<LUKSFlag>(flag1);
data.m_crypt = static_cast<CRYPT_CIPHER>(flag2);
argument.endStructure();
// qDebug() << "Completed WipeAction deserialization";
// qCDebug(baseLog) << "Completed WipeAction deserialization";
return argument;
}
88 changes: 88 additions & 0 deletions basestruct/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

#include "logger.h"
#include "ddlog.h"
#include <QLoggingCategory>

Check warning on line 7 in basestruct/logger.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QObject>

Check warning on line 8 in basestruct/logger.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#ifdef DTKCORE_CLASS_DConfigFile
#include <DConfig>

Check warning on line 11 in basestruct/logger.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DConfig> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#endif
DCORE_USE_NAMESPACE

// 分类定义:主程序 / 后台服务 / 公共库 basestruct 各自独立分类,
// 规则 "org.deepin.dde.diskmanager*.debug" 通配三者(* 可跨 '.' 匹配)
Q_LOGGING_CATEGORY(appLog, "org.deepin.dde.diskmanager")
Q_LOGGING_CATEGORY(serviceLog, "org.deepin.dde.diskmanager.service")
Q_LOGGING_CATEGORY(baseLog, "org.deepin.dde.diskmanager.basestruct")

MLogger::MLogger(QObject *parent)
: QObject(parent), m_rules("")
#ifdef DTKCORE_CLASS_DConfigFile
, m_config(nullptr)
#endif
{
QByteArray logRules = qgetenv("QT_LOGGING_RULES");
// qunsetenv 之前一定不要有任何日志打印,否则取消环境变量设置不会生效(大坑)
qunsetenv("QT_LOGGING_RULES");

// set env
m_rules = logRules;

#ifdef DTKCORE_CLASS_DConfigFile
// set dconfig
m_config = DConfig::create("org.deepin.dde.diskmanager", "org.deepin.dde.diskmanager");
if (m_config) {
logRules = m_config->value("log_rules").toByteArray();
appendRules(logRules);
setRules(m_rules);

// 监听 dconfig 变更,实现调试模式运行中动态调整
connect(m_config, &DConfig::valueChanged, this, [this](const QString &key) {
if (key == "log_rules") {
setRules(m_config->value(key).toByteArray());
}
});
} else {
// dconfig meta 未安装时退回环境变量规则,保证基础日志能力可用
setRules(m_rules);
}
#else
setRules(m_rules);
#endif
}

MLogger::~MLogger()
{
#ifdef DTKCORE_CLASS_DConfigFile
if (m_config) {
m_config->deleteLater();
}
#endif
}

void MLogger::setRules(const QString &rules)
{
auto tmpRules = rules;
// Qt 限制:setFilterRules 的分隔符必须是 '\n',不能是 ';'
m_rules = tmpRules.replace(";", "\n");
QLoggingCategory::setFilterRules(m_rules);
}

void MLogger::appendRules(const QString &rules)
{
QString tmpRules = rules;
tmpRules = tmpRules.replace(";", "\n");
auto tmplist = tmpRules.split('\n');
for (int i = 0; i < tmplist.count(); i++)
if (m_rules.contains(tmplist.at(i))) {
tmplist.removeAt(i);
i--;
}
if (tmplist.isEmpty())
return;
m_rules.isEmpty() ? m_rules = tmplist.join("\n")
: m_rules += "\n" + tmplist.join("\n");
}
49 changes: 49 additions & 0 deletions basestruct/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

#ifndef BASESTRUCT_LOGGER_H
#define BASESTRUCT_LOGGER_H

#include <QObject>

Check warning on line 8 in basestruct/logger.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dtkcore_global.h>

Check warning on line 9 in basestruct/logger.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dtkcore_global.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#ifdef DTKCORE_CLASS_DConfigFile
DCORE_BEGIN_NAMESPACE
class DConfig;
DCORE_END_NAMESPACE
#endif

/**
* @brief 动态日志规则监听器
*
* 参考 deepin-reader 的 MLogger 实现:
* 1. 读取环境变量 QT_LOGGING_RULES 后立即 qunsetenv(必须在任何日志打印之前,
* 否则取消环境变量设置不生效),保证外部注入的规则仍然有效;
* 2. 读取 DConfig(org.deepin.dde.diskmanager) 的 log_rules 默认规则;
* 3. 监听 DConfig valueChanged,实现调试模式运行中动态调整日志级别。
*
* 使用方式:在 main() 中、Application 实例化之前创建一次(先于任何日志打印):
* MLogger logger;
*/
class MLogger : public QObject
{
Q_OBJECT
public:
explicit MLogger(QObject *parent = nullptr);
~MLogger();

inline QString rules() const { return m_rules; }

Check warning on line 36 in basestruct/logger.h

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'rules' is never used.
void setRules(const QString &rules);

private:
void appendRules(const QString &rules);

private:
QString m_rules;
#ifdef DTKCORE_CLASS_DConfigFile
Dtk::Core::DConfig *m_config;
#endif
};

#endif // BASESTRUCT_LOGGER_H
Loading
Loading