Skip to content

fix: expand event log to all non-community editions / 修复事件日志启用条件为非社区版#1114

Merged
Ivy233 merged 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/eventlog-non-community
May 20, 2026
Merged

fix: expand event log to all non-community editions / 修复事件日志启用条件为非社区版#1114
Ivy233 merged 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/eventlog-non-community

Conversation

@Ivy233
Copy link
Copy Markdown
Contributor

@Ivy233 Ivy233 commented May 20, 2026

PMS: BUG-362029

Summary by Sourcery

Bug Fixes:

  • Correct event log enablement logic to no longer require the Professional edition and instead disable only on Community edition systems.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 20, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts the event logging enablement condition so that it is enabled for all editions except UosCommunity instead of only UosProfessional, by changing the edition comparison logic when parsing /etc/os-version and updating the associated comments.

Flow diagram for event log enablement condition

flowchart TD
    A[Read /etc/os-version] --> B{EditionName found?}
    B -- No --> C[Return false]
    B -- Yes --> D{EditionName == Community?}
    D -- Yes --> E[Return false]
    D -- No --> F[Return true]
Loading

File-Level Changes

Change Details Files
Broaden event log enablement from only Professional edition to all non-Community editions based on /etc/os-version EditionName.
  • Update comments to describe that event logging is enabled for all non-Community editions rather than only Professional
  • Change the condition that evaluates EditionName from equality with "Professional" to inequality with "Community" when deciding to enable event logging
session/eventlog/event_sdk.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Since the check now hinges on edition != "Community", consider normalizing the EditionName value (e.g., trimming whitespace and handling case) to avoid unexpected behavior from minor formatting differences in /etc/os-version.
  • The logic currently returns true when EditionName is missing or malformed; if the intent is to restrict logging to non-community editions only, you may want to explicitly handle the case where no EditionName is found instead of implicitly enabling.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since the check now hinges on `edition != "Community"`, consider normalizing the `EditionName` value (e.g., trimming whitespace and handling case) to avoid unexpected behavior from minor formatting differences in `/etc/os-version`.
- The logic currently returns `true` when `EditionName` is missing or malformed; if the intent is to restrict logging to non-community editions only, you may want to explicitly handle the case where no `EditionName` is found instead of implicitly enabling.

## Individual Comments

### Comment 1
<location path="session/eventlog/event_sdk.cpp" line_range="32-35" />
<code_context>
     return true;
 #else
-    // Production mode: only enable for UosProfessional edition
+    // Production mode: disable for UosCommunity edition
     // Read /etc/os-version and check EditionName
     ifstream osVersion("/etc/os-version");
</code_context>
<issue_to_address>
**issue:** The implementation does not fully match the "all non-Community editions enabled" intent when /etc/os-version is missing or lacks EditionName.

Given the intent to "disable only for Community", this logic still returns false when `/etc/os-version` is missing or lacks an `EditionName=` line, effectively disabling event logging in those cases. To align with the intent, consider returning `true` when the file or `EditionName` is absent, and only returning `false` when `EditionName=Community` is explicitly found.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread session/eventlog/event_sdk.cpp
@Ivy233 Ivy233 force-pushed the fix/eventlog-non-community branch from d7fc262 to d743e5d Compare May 20, 2026 03:48
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的 git diff。这次代码变更的核心逻辑是将事件日志的启用策略从**“仅限专业版启用”改为了“除社区版外均启用”**。

下面我将从语法逻辑、代码质量、代码性能和代码安全四个维度为你进行详细的代码审查,并提出改进意见:

1. 语法与逻辑

  • 逻辑反转的一致性:代码逻辑已经成功从 == "Professional" 反转为 != "Community",且在未找到匹配项和文件无法打开时的默认返回值也从 false 改为了 true,逻辑反转是完整的。
  • 硬编码的魔法数字line.substr(12) 中的 12 是一个魔法数字。如果前缀字符串 "EditionName=" 发生变化,开发者很容易忘记修改这个数字,从而导致截取错误。

2. 代码质量

  • 冗余的 close() 调用:在 while 循环内部发现匹配项后,代码手动调用了 osVersion.close() 然后返回。在函数末尾也手动调用了 osVersion.close()。这是不必要的,因为 C++ 的 RAII(资源获取即初始化)机制保证在 ifstream 对象超出作用域时(即函数返回时)会自动关闭文件。手动关闭不仅增加了代码冗余,还增加了出错风险(如在已关闭的对象上调用方法)。
  • 注释与代码的精确性:注释从 Only UosProfessional is enabled 改为 Not UosCommunity is enabled。后者在英语表达上略显生硬,建议优化为 Enabled for all editions except UosCommunity,这样更符合英语习惯且表意更清晰。
  • 字符串查找方式:使用 find("EditionName=") == 0 来判断行首匹配是可以的,但 C++ 提供了更现代、更直观的 starts_with 方法(C++20起支持)。如果项目支持 C++20,建议替换。

3. 代码性能

  • 不必要的字符串拷贝line.substr(12) 会产生一个临时的字符串对象。对于仅仅是判断是否等于 "Community" 的场景,这带来了一次不必要的内存分配和拷贝。可以直接使用 std::string_view 或者利用 compare 方法在原字符串上直接进行比较。
  • 文件 I/O 效率:读取系统配置文件通常数据量很小,当前的逐行读取性能已经足够。但如果没有特殊需求,直接读取整个文件或者利用更底层的系统 API(如 Linux 下的 fopen/fgets)有时能减少 C++ iostream 的额外开销(但会牺牲跨平台性,当前保持 iostream 是合理的折中)。

4. 代码安全

  • 符号链接攻击:读取 /etc/os-version 时,如果该文件被恶意替换为指向超大文件的符号链接,可能会导致内存耗尽(DoS攻击)。但在实际场景中,/etc/ 目录通常只有 root 用户有写权限,风险相对较低。
  • 空格或大小写处理:如果 /etc/os-version 文件中的值带有不可见的空格(如 Community ),或者大小写不一致(如 community),当前的严格等于判断 != "Community" 会失效,导致本该禁用的社区版也启用了日志。建议进行大小写无关比较或去除首尾空格。

改进后的代码建议

综合以上分析,我为你提供一版优化后的代码(假设项目支持 C++17,如果仅支持 C++11,可将 std::string_view 替换为 const std::string& 并保留 substr):

// Check if event logging should be enabled based on system edition
// Enabled for all editions except UosCommunity
// Reads /etc/os-version to determine edition
static bool shouldEnableEventLog()
{
#ifdef DEBUG
    // Debug mode: enable for current system version
    return true;
#else
    // Production mode: disable for UosCommunity edition
    // Read /etc/os-version and check EditionName
    std::ifstream osVersion("/etc/os-version");
    if (!osVersion.is_open()) {
        return true; // 默认开启
    }

    std::string line;
    const std::string prefix = "EditionName=";
    while (std::getline(osVersion, line)) {
        // 使用 starts_with (C++20) 或 find (C++11) 检查前缀
        if (line.find(prefix) == 0) {
            // 避免 substr 产生的字符串拷贝,直接在原字符串上比较
            // 或者使用 C++17 的 std::string_view
            std::string edition = line.substr(prefix.length());
            
            // 安全性增强:可以在此处添加 trim 空格和转换为小写的逻辑
            // trim(edition); 
            // to_lower(edition);
            
            return edition != "Community"; 
        }
    }
    
    return true; // 未找到 EditionName 字段,默认开启
#endif
}

主要改进点说明:

  1. 移除了手动的 osVersion.close():依赖 RAII 自动管理资源,代码更简洁安全。
  2. 消除了魔法数字:引入 prefix 变量,使用 prefix.length() 作为 substr 的参数,提高可维护性。
  3. 优化了注释:使注释表达更加地道和清晰。
  4. 安全性提示:在注释中预留了处理空格和大小写的位置,如果你的系统配置文件格式不够严格,强烈建议实现 trimto_lower 函数。

@Ivy233 Ivy233 requested review from 18202781743 and mhduiy May 20, 2026 05:19
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ivy233, mhduiy

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

@Ivy233 Ivy233 merged commit 29746bc into linuxdeepin:master May 20, 2026
18 checks passed
@Ivy233 Ivy233 deleted the fix/eventlog-non-community branch May 20, 2026 05:50
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.

4 participants