This commit is contained in:
lnk
2025-07-29 20:58:36 +08:00
parent c22bc96e60
commit 7ee93deefa

View File

@@ -3449,6 +3449,29 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
clientSocket->write(QString("Viewing logs for level: %1 (Press '`' to exit)\n> ").arg(logLevel).toUtf8());
clientSocket->flush();
// --- 新增 begin ---
// 创建 /FeProject/dat/log 目录(若不存在)
QString logDir = "/FeProject/dat/log";
QDir().mkpath(logDir);
// 生成唯一的日志文件名,如 temp.log、temp_1.log、temp_2.log 等
QString baseName = "temp.log";
QString filePath = logDir + "/" + baseName;
int index = 1;
while (QFile::exists(filePath)) {
filePath = QString("%1/temp_%2.log").arg(logDir).arg(index++);
}
QFile logFile(filePath);
QTextStream logStream(&logFile);
if (!logFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
clientSocket->write("\r\x1B[K");
clientSocket->write("Failed to open log file for writing.\n> ");
clientSocket->flush();
return;
}
// --- 新增 end ---
while (!stopViewLog) {
// 1. 监听退出标志(收到反引号 `
if (clientSocket->waitForReadyRead(500)) {
@@ -3475,9 +3498,17 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
if (!logEntry.empty()) {
clientSocket->write("\r\x1B[K"); // 清除当前行
clientSocket->write((logEntry + "\n").c_str());
// --- 新增 begin ---
logStream << QString::fromStdString(logEntry) << "\n";
// --- 新增 end ---
}
}
clientSocket->flush();
// --- 新增 begin ---
logStream.flush(); // 确保及时写入文件
// --- 新增 end ---
} else {
usleep(500000); // 空闲等待,降低 CPU 占用
}
@@ -3487,6 +3518,10 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
clientSocket->write("\r\x1B[K");
clientSocket->write("\nLog view stopped. Returning to shell.\n> ");
clientSocket->flush();
// --- 新增 begin ---
logFile.close(); // 关闭文件
// --- 新增 end ---
}
//////////////////////////////////////////////////////////////////////////////////////////////////
@@ -6356,7 +6391,7 @@ protected:
case LOGNORMAL: shouldLog = normalOutputEnabled; break;
case LOGDEBUG: shouldLog = debugOutputEnabled; break;
}
if (!shouldLog) return ch; // 🚫 日志开关未启用,直接跳过
// 2) 存到我们的临时缓存,注意加锁保护