Skip to content

202510281922 master feat - #415

Merged
lzwind merged 6 commits into
linuxdeepin:masterfrom
GongHeng2017:202510281922-master-feat
Oct 30, 2025
Merged

lzwind merged 6 commits into
linuxdeepin:masterfrom
GongHeng2017:202510281922-master-feat

Conversation

@GongHeng2017

Copy link
Copy Markdown
Contributor

No description provided.

-- Add ops log logic code for ops log.

Log: add feature for ops logs
Task: https://pms.uniontech.com/story-view-39399.html
-- Add dbus interface for get ops logs.

Log: add feature for ops logs.
Task: https://pms.uniontech.com/story-view-39399.html
-- Add ops log directory structure.

Log: add feature for ops logs.
Task: https://pms.uniontech.com/story-view-39399.html
-- Export apps logs for ops.

Log: add feature for ops logs.
Task: https://pms.uniontech.com/story-view-39399.html
-- Add system logs and kernel logs for ops.

Log: add feature for ops logs.
Task: https://pms.uniontech.com/story-view-39399.html
-- Export DDE logs.
-- Export special platform logs.
-- Export os version logs.
-- Export deb verison logs.

Log: add feature for ops logs.
Task: https://pms.uniontech.com/story-view-39399.html
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这是一个用于添加运维日志导出功能的代码变更。让我从多个角度分析这段代码:

  1. 代码结构与组织:
  • 新增了一个独立的 opsLogViewerService 模块,结构清晰
  • 使用 D-Bus 进行进程间通信,符合 Linux 桌面应用的设计模式
  • 日志导出功能被合理地拆分到不同的方法中,如 exportAppLogs、exportSystemLogs 等
  1. 安全性:
  • 使用 system() 执行命令存在安全风险,特别是在 utils.cpp 中的 exportSomeOpsLogs() 函数
  • 没有对输入参数进行充分的验证,如 outDir 和 userHomeDir
  • 文件权限设置为 777 过于宽松,可能带来安全隐患
  1. 性能:
  • 大量使用 system() 调用,频繁创建进程会影响性能
  • 文件操作没有使用异步方式,可能导致 UI 卡顿
  • 临时目录的清理机制不够健壮
  1. 代码质量:
  • 部分硬编码的路径和命令,降低了代码的可维护性
  • 缺少错误处理机制,如 system() 调用失败的处理
  • 日志输出不够详细,不利于问题排查

改进建议:

  1. 安全性改进:
// 替换 system() 调用,使用 QProcess
void Utils::exportSomeOpsLogs(const QString &outDir, const QString &userHomeDir)
{
    // 验证输入参数
    if (!QDir(outDir).exists() || !QDir(userHomeDir).exists()) {
        qCCritical(logApp) << "Invalid directory paths";
        return;
    }

    // 使用 QProcess 替代 system()
    QProcess process;
    
    // glxinfo
    process.setWorkingDirectory(outDir + "/app/kwin");
    process.start("glxinfo", QStringList{"-B"});
    process.waitForFinished();
    QFile file(outDir + "/app/kwin/glxinfo.log");
    if (file.open(QIODevice::WriteOnly)) {
        file.write(process.readAllStandardOutput());
    }

    // 其他命令类似处理...
}
  1. 性能优化:
// 使用异步方式处理文件操作
void OpsLogExport::copy_file_or_dir(const QString &src, const QString &dst_dir)
{
    if (!QFile::exists(src)) return;
    
    QFuture<void> future = QtConcurrent::run([=]() {
        QDir().mkpath(dst_dir);
        QFile::copy(src, dst_dir);
    });
}
  1. 错误处理增强:
bool DLDBusHandler::exportOpsLog(const QString &outDir, const QString &userHomeDir)
{
    // 参数验证
    if (outDir.isEmpty() || userHomeDir.isEmpty()) {
        qCCritical(logApp) << "Empty directory paths provided";
        return false;
    }

    QDBusMessage message = QDBusMessage::createMethodCall(kLogExportService,
                                                         kLogExportPath,
                                                         kLogExportInterface,
                                                         "ExportOpsLog");
    message << outDir << userHomeDir;

    QDBusMessage reply = QDBusConnection::systemBus().call(message, QDBus::Block, 1200000);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qCCritical(logApp) << "DBus call failed:" << reply.errorMessage();
        return false;
    }

    bool result = false;
    const QDBusArgument &arg = reply.arguments().first().value<QDBusArgument>();
    arg >> result;
    
    return result;
}
  1. 配置管理改进:
// 使用配置文件管理路径和命令
class OpsLogConfig {
public:
    static QString getAppLogPath(const QString &appName) {
        static QMap<QString, QString> paths = {
            {"deepin-defender", "~/.cache/deepin/deepin-defender/"},
            // 其他应用路径...
        };
        return paths.value(appName);
    }
    
    static QStringList getSystemCommands() {
        return {
            "dmesg -T",
            "journalctl",
            // 其他系统命令...
        };
    }
};
  1. 权限管理改进:
// 使用更合理的权限设置
void OpsLogExport::setFilePermissions(const QString &path)
{
    QFile file(path);
    if (file.exists()) {
        // 设置为 644 (rw-r--r--)
        file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | 
                          QFile::ReadGroup | QFile::ReadOther);
    }
}
  1. 资源管理改进:
// 使用 RAII 管理临时目录
class TempDirectory {
public:
    TempDirectory(const QString &prefix) {
        m_path = QDir::temp().filePath(prefix + QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
        QDir().mkpath(m_path);
    }
    
    ~TempDirectory() {
        if (QDir(m_path).exists()) {
            QDir(m_path).removeRecursively();
        }
    }
    
    QString path() const { return m_path; }
    
private:
    QString m_path;
};

这些改进可以显著提高代码的安全性、性能和可维护性。建议在实施这些改进时,确保进行充分的测试,特别是涉及系统调用和文件操作的部分。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lzwind
lzwind merged commit 17d1ace into linuxdeepin:master Oct 30, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants