log save
This commit is contained in:
@@ -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->write(QString("Viewing logs for level: %1 (Press '`' to exit)\n> ").arg(logLevel).toUtf8());
|
||||||
clientSocket->flush();
|
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) {
|
while (!stopViewLog) {
|
||||||
// 1. 监听退出标志(收到反引号 `)
|
// 1. 监听退出标志(收到反引号 `)
|
||||||
if (clientSocket->waitForReadyRead(500)) {
|
if (clientSocket->waitForReadyRead(500)) {
|
||||||
@@ -3475,9 +3498,17 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
|
|||||||
if (!logEntry.empty()) {
|
if (!logEntry.empty()) {
|
||||||
clientSocket->write("\r\x1B[K"); // 清除当前行
|
clientSocket->write("\r\x1B[K"); // 清除当前行
|
||||||
clientSocket->write((logEntry + "\n").c_str());
|
clientSocket->write((logEntry + "\n").c_str());
|
||||||
|
|
||||||
|
// --- 新增 begin ---
|
||||||
|
logStream << QString::fromStdString(logEntry) << "\n";
|
||||||
|
// --- 新增 end ---
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clientSocket->flush();
|
clientSocket->flush();
|
||||||
|
|
||||||
|
// --- 新增 begin ---
|
||||||
|
logStream.flush(); // 确保及时写入文件
|
||||||
|
// --- 新增 end ---
|
||||||
} else {
|
} else {
|
||||||
usleep(500000); // 空闲等待,降低 CPU 占用
|
usleep(500000); // 空闲等待,降低 CPU 占用
|
||||||
}
|
}
|
||||||
@@ -3487,6 +3518,10 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
|
|||||||
clientSocket->write("\r\x1B[K");
|
clientSocket->write("\r\x1B[K");
|
||||||
clientSocket->write("\nLog view stopped. Returning to shell.\n> ");
|
clientSocket->write("\nLog view stopped. Returning to shell.\n> ");
|
||||||
clientSocket->flush();
|
clientSocket->flush();
|
||||||
|
|
||||||
|
// --- 新增 begin ---
|
||||||
|
logFile.close(); // 关闭文件
|
||||||
|
// --- 新增 end ---
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -6356,7 +6391,7 @@ protected:
|
|||||||
case LOGNORMAL: shouldLog = normalOutputEnabled; break;
|
case LOGNORMAL: shouldLog = normalOutputEnabled; break;
|
||||||
case LOGDEBUG: shouldLog = debugOutputEnabled; break;
|
case LOGDEBUG: shouldLog = debugOutputEnabled; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shouldLog) return ch; // 🚫 日志开关未启用,直接跳过
|
if (!shouldLog) return ch; // 🚫 日志开关未启用,直接跳过
|
||||||
|
|
||||||
// 2) 存到我们的临时缓存,注意加锁保护
|
// 2) 存到我们的临时缓存,注意加锁保护
|
||||||
|
|||||||
Reference in New Issue
Block a user