Fix(battery): get battery info error.#661
Conversation
-- In Qt 6, when using QProcess::start(), you must separate the program name from the argument list for the command to execute correctly. Log: fix issue Bug: https://pms.uniontech.com/bug-view-361071.html
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates battery information retrieval to use Qt 6–compliant QProcess invocation for the Sequence diagram for updated battery info retrieval with QProcess in Qt6sequenceDiagram
participant CmdTool_getCurPowerInfo as CmdTool::getCurPowerInfo
participant QProcess_process as QProcess
participant upower as upower
CmdTool_getCurPowerInfo->>QProcess_process: start("upower", QStringList(--dump))
activate QProcess_process
QProcess_process->>upower: execute upower --dump
upower-->>QProcess_process: stdout (battery info)
QProcess_process-->>CmdTool_getCurPowerInfo: waitForFinished(5000)
deactivate QProcess_process
CmdTool_getCurPowerInfo->>QProcess_process: readAllStandardOutput()
QProcess_process-->>CmdTool_getCurPowerInfo: powerInfo
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff内容。这次修改主要针对外部命令的调用方式和超时处理进行了优化,整体改进方向是非常好的,但也存在一些需要关注的隐患。以下是详细的审查意见: 一、 语法与逻辑
二、 代码质量
三、 代码性能
四、 代码安全
🚀 综合改进后的代码建议结合以上分析,我为你重构了这部分代码,增强了健壮性、错误处理和资源管理: QMap<QString, QMap<QString, QString>> CmdTool::getCurPowerInfo()
{
qCDebug(appLog) << "Getting current power info from upower.";
QString powerInfo;
QMap<QString, QMap<QString, QString>> map;
// 执行"upower --dump"命令获取电池相关信息
QProcess process;
// 推荐使用现代C++初始化列表风格
process.start("upower", QStringList{"--dump"});
// 获取命令执行结果,设置5秒超时
if (!process.waitForFinished(5000)) {
// 区分是启动失败还是执行超时
if (process.error() == QProcess::Timedout) {
qCWarning(appLog) << "upower --dump timed out after 5000ms.";
process.kill(); // 终止进程
process.waitForFinished(); // 等待进程资源回收
} else {
qCWarning(appLog) << "Failed to start upower:" << process.errorString();
}
return map; // 返回空map,避免后续解析错误数据
}
// 检查进程退出码,确保命令正常执行完毕
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
qCWarning(appLog) << "upower --dump exited with error. Exit code:" << process.exitCode()
<< "Error output:" << process.readAllStandardError();
return map;
}
powerInfo = process.readAllStandardOutput();
qCDebug(appLog) << "upower --dump output:" << powerInfo;
QStringList items = powerInfo.split("\n\n");
// ... 后续解析逻辑 ...
return map;
}主要改进点说明:
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider checking the return value of
process.waitForFinished(5000)and handling the timeout or failure case explicitly instead of assuming the process completed successfully. - If a fixed 5-second timeout is required, it might be clearer to define it as a named constant (or configuration) so its rationale and usage are easier to adjust in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider checking the return value of `process.waitForFinished(5000)` and handling the timeout or failure case explicitly instead of assuming the process completed successfully.
- If a fixed 5-second timeout is required, it might be clearer to define it as a named constant (or configuration) so its rationale and usage are easier to adjust in the future.
## Individual Comments
### Comment 1
<location path="deepin-devicemanager/src/GenerateDevice/CmdTool.cpp" line_range="1324" />
<code_context>
+ process.start("upower", QStringList() << "--dump");
// 获取命令执行结果
- process.waitForFinished(-1);
+ process.waitForFinished(5000);
powerInfo = process.readAllStandardOutput();
qCDebug(appLog) << "upower --dump output:" << powerInfo;
</code_context>
<issue_to_address>
**issue (bug_risk):** A fixed 5s timeout may cause incomplete results or subtle bugs on slower systems.
With this change, `waitForFinished(5000)` may return while `upower --dump` is still running, so you could parse partial output as if it were complete. If you need a timeout, check the boolean return value and handle timeouts explicitly (e.g., log and skip parsing), or make the timeout configurable so it can be tuned for slower systems.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
-- In Qt 6, when using QProcess::start(), you must separate the program name from the argument list for the command to execute correctly.
Log: fix issue
Bug: https://pms.uniontech.com/bug-view-361071.html
Summary by Sourcery
Fix battery information retrieval by correctly invoking the upower command with separated program and arguments and adding a timeout for the process execution.
Bug Fixes:
Enhancements: