From 7ee93deefa98db690e2fa34d235c0052436322fd Mon Sep 17 00:00:00 2001 From: lnk Date: Tue, 29 Jul 2025 20:58:36 +0800 Subject: [PATCH] log save --- cfg_parse/cfg_parser.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cfg_parse/cfg_parser.cpp b/cfg_parse/cfg_parser.cpp index cdb29b6..486e775 100644 --- a/cfg_parse/cfg_parser.cpp +++ b/cfg_parse/cfg_parser.cpp @@ -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) 存到我们的临时缓存,注意加锁保护