Feature: add client log dialog with auto-refresh, icon and menu bitmap
- CClientLogManager: push thread sends incremental logs every 3s; join() instead of detach() prevents use-after-free on shutdown - CClientLog::OnReceiveComplete: handles TOKEN_REPORT_LOG on IOCP thread via PostMessage, fixing incremental packets being silently dropped - Dialog icon (ClientLog.ico) and menu bitmap (ClientLog.bmp) added; OnInitDialog loads icon; IDR_MENU_LIST_ONLINE "运行日志" item gets the bitmap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include <cstdarg>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
|
||||
|
||||
inline bool stringToBool(const std::string& str)
|
||||
@@ -150,6 +151,16 @@ public:
|
||||
std::string logEntry = file && line ?
|
||||
id + "[" + timestamp + "] [" + file + ":" + std::to_string(line) + "] " + message:
|
||||
id + "[" + timestamp + "] " + message;
|
||||
|
||||
// Always record to in-memory ring buffer regardless of enable flag.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||||
m_memLog.push_back(logEntry);
|
||||
m_totalCount++;
|
||||
if (m_memLog.size() > kMemLogMax)
|
||||
m_memLog.pop_front();
|
||||
}
|
||||
|
||||
if (enable) {
|
||||
if (running) {
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
@@ -168,6 +179,32 @@ public:
|
||||
cv.notify_one(); // 通知写线程
|
||||
}
|
||||
|
||||
// 返回内存 ring buffer 中的全部日志(供 TCP 查询使用)
|
||||
std::string DumpMemoryLog() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||||
std::string result;
|
||||
result.reserve(m_memLog.size() * 128);
|
||||
for (const auto& entry : m_memLog)
|
||||
result += entry;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 增量版本:只返回 fromAbs 之后的新条目,并将 fromAbs 更新到当前末尾。
|
||||
// fromAbs 是调用方维护的绝对计数器(首次传 0 即得全量)。
|
||||
std::string DumpMemoryLogFrom(size_t& fromAbs) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||||
// ring buffer 内最旧条目的绝对序号
|
||||
size_t oldest = (m_totalCount >= m_memLog.size()) ? m_totalCount - m_memLog.size() : 0;
|
||||
size_t startIdx = (fromAbs <= oldest) ? 0 : (fromAbs - oldest);
|
||||
std::string result;
|
||||
for (size_t i = startIdx; i < m_memLog.size(); ++i)
|
||||
result += m_memLog[i];
|
||||
fromAbs = m_totalCount;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 停止日志系统
|
||||
void stop()
|
||||
{
|
||||
@@ -220,6 +257,11 @@ private:
|
||||
std::mutex fileMutex; // 文件写入锁
|
||||
std::string pid; // 进程ID
|
||||
|
||||
static constexpr size_t kMemLogMax = 1000;
|
||||
std::deque<std::string> m_memLog;
|
||||
size_t m_totalCount = 0;
|
||||
mutable std::mutex m_memMutex;
|
||||
|
||||
Logger() : enable(false), threadRun(false), running(true), workerThread(&Logger::processLogs, this) {}
|
||||
|
||||
~Logger()
|
||||
|
||||
Reference in New Issue
Block a user