Fix(logger): preserve queued logs on shutdown; record exit signal

This commit is contained in:
yuanyuanxiang
2026-05-27 08:51:49 +02:00
parent 268a427172
commit 1fd431ba76
3 changed files with 37 additions and 4 deletions

View File

@@ -228,16 +228,19 @@ private:
}
// 后台线程处理日志
// 退出语义stop() 设 running=false 后,本线程必须把队列里**已入队**的日志
// 全部刷盘再退出。否则进程死亡前最后几条 Mprintf包括退出原因会丢失。
void processLogs()
{
threadRun = true;
while (running) {
while (true) {
std::unique_lock<std::mutex> lock(queueMutex);
cv.wait(lock, [this]() {
return !running || !logQueue.empty();
});
while (running && !logQueue.empty()) {
// drain不带 running 判断,确保 stop() 时残留条目也写完
while (!logQueue.empty()) {
std::string logEntry = logQueue.front();
logQueue.pop();
lock.unlock();
@@ -247,7 +250,9 @@ private:
lock.lock();
}
lock.unlock();
// 队列已空再决定要不要退出
if (!running) break;
}
threadRun = false;
}