fix test shell print log
This commit is contained in:
@@ -3450,34 +3450,38 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
|
||||
clientSocket->flush();
|
||||
|
||||
while (!stopViewLog) {
|
||||
// **1. 监听输入,用户输入 ``` 退出**
|
||||
if (clientSocket->waitForReadyRead(500)) { // ? 监听输入
|
||||
QByteArray input = clientSocket->readAll().trimmed();
|
||||
if (input == "`") { // ? 用户输入 ```,退出日志模式
|
||||
std::cout << "Received '`' from shell socket! Exiting viewlog...\n";
|
||||
stopViewLog = true;
|
||||
showinshellflag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 1. 监听退出标志(收到反引号 `)
|
||||
if (clientSocket->waitForReadyRead(500)) {
|
||||
QByteArray input = clientSocket->readAll().trimmed();
|
||||
if (input == "`") {
|
||||
std::cout << "Received '`' from shell socket! Exiting viewlog...\n";
|
||||
stopViewLog = true;
|
||||
showinshellflag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// **2. 获取日志内容并发送**
|
||||
pthread_mutex_lock(logMutex);
|
||||
if (!logList->empty()) {
|
||||
std::string logEntry = logList->front();
|
||||
logList->pop_front();
|
||||
pthread_mutex_unlock(logMutex);
|
||||
// 2. 批量获取日志并发送
|
||||
std::list<std::string> tempLogs;
|
||||
|
||||
if (!logEntry.empty()) {
|
||||
clientSocket->write("\r\x1B[K");
|
||||
clientSocket->write((logEntry + "\n").c_str());
|
||||
clientSocket->flush();
|
||||
}
|
||||
} else {
|
||||
pthread_mutex_unlock(logMutex);
|
||||
usleep(500000); // ? 防止 CPU 100% 占用
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(logMutex);
|
||||
if (!logList->empty()) {
|
||||
tempLogs.swap(*logList); // 把 logList 中的内容全取出
|
||||
}
|
||||
pthread_mutex_unlock(logMutex);
|
||||
|
||||
if (!tempLogs.empty()) {
|
||||
for (const auto& logEntry : tempLogs) {
|
||||
if (!logEntry.empty()) {
|
||||
clientSocket->write("\r\x1B[K"); // 清除当前行
|
||||
clientSocket->write((logEntry + "\n").c_str());
|
||||
}
|
||||
}
|
||||
clientSocket->flush();
|
||||
} else {
|
||||
usleep(500000); // 空闲等待,降低 CPU 占用
|
||||
}
|
||||
}
|
||||
|
||||
// **3. 退出 `viewlog`,返回 Shell**
|
||||
clientSocket->write("\r\x1B[K");
|
||||
@@ -6343,6 +6347,18 @@ protected:
|
||||
return traits_type::eof();
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ 如果当前级别未启用,不记录
|
||||
bool shouldLog = false;
|
||||
switch (m_level) {
|
||||
case LOGERROR: shouldLog = errorOutputEnabled; break;
|
||||
case LOGWARN: shouldLog = warnOutputEnabled; break;
|
||||
case LOGNORMAL: shouldLog = normalOutputEnabled; break;
|
||||
case LOGDEBUG: shouldLog = debugOutputEnabled; break;
|
||||
}
|
||||
|
||||
if (!shouldLog) return ch; // 🚫 日志开关未启用,直接跳过
|
||||
|
||||
// 2) 存到我们的临时缓存,注意加锁保护
|
||||
pthread_mutex_lock(&m_mutex); //防止多线程推入崩溃lnk20250305
|
||||
m_buffer.push_back(static_cast<char>(ch));
|
||||
|
||||
Reference in New Issue
Block a user