update:保存日志到/tmp

This commit is contained in:
2025-06-18 20:47:48 +08:00
parent a9d9f035de
commit f925ac7242
2 changed files with 95 additions and 37 deletions

View File

@@ -240,11 +240,53 @@ void AppDelegate::startNextInstall() {
} }
m_installProcess = new QProcess(this); m_installProcess = new QProcess(this);
// 新增:准备安装日志文件
QString logPath = QString("/tmp/%1_install.log").arg(packageName);
QFile *logFile = new QFile(logPath, m_installProcess);
if (logFile->open(QIODevice::Append | QIODevice::Text)) {
// 设置权限为777
QFile::setPermissions(logPath, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
connect(m_installProcess, &QProcess::readyReadStandardOutput, this, [this, packageName, logFile]() {
QByteArray out = m_installProcess->readAllStandardOutput();
logFile->write(out);
logFile->flush();
QString text = QString::fromLocal8Bit(out);
qDebug().noquote() << text;
// 检查“软件包已安装”关键字
if (text.contains(QStringLiteral("软件包已安装"))) {
m_downloads[packageName].isInstalling = false;
m_downloads[packageName].isInstalled = true;
emit updateDisplay(packageName);
}
});
connect(m_installProcess, &QProcess::readyReadStandardError, this, [this, logFile]() {
QByteArray err = m_installProcess->readAllStandardError();
logFile->write(err);
logFile->flush();
qDebug().noquote() << QString::fromLocal8Bit(err);
});
connect(m_installProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, packageName, logFile](int /*exitCode*/, QProcess::ExitStatus /*status*/) {
if (logFile) logFile->close();
// 若未检测到“软件包已安装”,此处兜底
if (!m_downloads[packageName].isInstalled) {
m_downloads[packageName].isInstalling = false;
}
emit updateDisplay(packageName);
m_installProcess->deleteLater();
m_installProcess = nullptr;
m_installingPackage.clear();
startNextInstall();
});
} else {
// 日志文件无法打开时,仍然要连接原有信号
connect(m_installProcess, &QProcess::readyReadStandardOutput, this, [this, packageName]() { connect(m_installProcess, &QProcess::readyReadStandardOutput, this, [this, packageName]() {
QByteArray out = m_installProcess->readAllStandardOutput(); QByteArray out = m_installProcess->readAllStandardOutput();
QString text = QString::fromLocal8Bit(out); QString text = QString::fromLocal8Bit(out);
qDebug().noquote() << text; qDebug().noquote() << text;
// 检查“软件包已安装”关键字
if (text.contains(QStringLiteral("软件包已安装"))) { if (text.contains(QStringLiteral("软件包已安装"))) {
m_downloads[packageName].isInstalling = false; m_downloads[packageName].isInstalling = false;
m_downloads[packageName].isInstalled = true; m_downloads[packageName].isInstalled = true;
@@ -257,16 +299,13 @@ void AppDelegate::startNextInstall() {
}); });
connect(m_installProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), connect(m_installProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, packageName](int /*exitCode*/, QProcess::ExitStatus /*status*/) { this, [this, packageName](int /*exitCode*/, QProcess::ExitStatus /*status*/) {
// 若未检测到“软件包已安装”,此处兜底
if (!m_downloads[packageName].isInstalled) {
m_downloads[packageName].isInstalling = false;
}
emit updateDisplay(packageName); emit updateDisplay(packageName);
m_installProcess->deleteLater(); m_installProcess->deleteLater();
m_installProcess = nullptr; m_installProcess = nullptr;
m_installingPackage.clear(); m_installingPackage.clear();
startNextInstall(); startNextInstall();
}); });
}
// 注意参数顺序deb路径在前--no-create-desktop-entry在后 // 注意参数顺序deb路径在前--no-create-desktop-entry在后
QStringList args; QStringList args;

View File

@@ -33,9 +33,20 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
QProcess *process = new QProcess(this); QProcess *process = new QProcess(this);
m_processes.insert(packageName, process); m_processes.insert(packageName, process);
connect(process, &QProcess::readyReadStandardOutput, this, [this, packageName, process]() { // 新增:准备日志文件
QString logPath = QString("/tmp/%1_download.log").arg(packageName);
QFile *logFile = new QFile(logPath, process);
if (logFile->open(QIODevice::Append | QIODevice::Text)) {
// 设置权限为777
QFile::setPermissions(logPath, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
connect(process, &QProcess::readyReadStandardOutput, this, [this, packageName, process, logFile]() {
while (process->canReadLine()) { while (process->canReadLine()) {
QString line = QString::fromUtf8(process->readLine()).trimmed(); QString line = QString::fromUtf8(process->readLine()).trimmed();
// 写入日志
logFile->write(line.toUtf8() + '\n');
logFile->flush();
QRegularExpression regex(R"(\((\d+)%\))"); QRegularExpression regex(R"(\((\d+)%\))");
QRegularExpressionMatch match = regex.match(line); QRegularExpressionMatch match = regex.match(line);
if (match.hasMatch()) { if (match.hasMatch()) {
@@ -44,9 +55,15 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
} }
} }
}); });
connect(process, &QProcess::readyReadStandardError, this, [process, logFile]() {
QByteArray err = process->readAllStandardError();
logFile->write(err);
logFile->flush();
});
}
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, packageName, outputPath](int exitCode, QProcess::ExitStatus status) { this, [this, packageName, outputPath, logFile](int exitCode, QProcess::ExitStatus status) {
bool success = (exitCode == 0 && status == QProcess::NormalExit); bool success = (exitCode == 0 && status == QProcess::NormalExit);
if (!success) { if (!success) {
qWarning() << "Download failed for" << packageName << "exit code:" << exitCode; qWarning() << "Download failed for" << packageName << "exit code:" << exitCode;
@@ -55,6 +72,8 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
removeAria2Files(outputPath); // 清理残留 .aria2 removeAria2Files(outputPath); // 清理残留 .aria2
emit downloadFinished(packageName, success); emit downloadFinished(packageName, success);
if (logFile) logFile->close();
QProcess *proc = m_processes.take(packageName); QProcess *proc = m_processes.take(packageName);
if (proc) proc->deleteLater(); if (proc) proc->deleteLater();
}); });