fix log error, add color log

This commit is contained in:
UnknownObject
2026-07-24 17:51:38 +08:00
parent 292c09619e
commit 45af94e535
42 changed files with 1727 additions and 87 deletions
+67 -8
View File
@@ -171,11 +171,33 @@ std::string uns::toBinary(unsigned long number, int bits)
std::string ServerLogger::GenerateLogHeader(uns::ServerLogLevel LogLevel)
{
std::string hstr;
// 在日志行最开头添加对应日志级别的颜色控制码
switch (LogLevel)
{
case uns::llDebug:
hstr += "\033[36m"; // 青色
break;
case uns::llInfo:
hstr += "\033[32m"; // 绿色
break;
case uns::llWarning:
hstr += "\033[33m"; // 黄色
break;
case uns::llError:
hstr += "\033[31m"; // 红色
break;
case uns::llFatal:
hstr += "\033[1;37;41m"; // 亮白字 + 红底 (极度醒目)
break;
case uns::llTrace:
default:
break; // TRACE 与未知级别保持默认颜色,不追加转义码
}
time_t lt = time(NULL);
tm* loctim = localtime(&lt);
char timestr[250] = {};
sprintf(timestr, "{%04d-%02d-%02d %02d:%02d:%02d} ", loctim->tm_year + 1900, loctim->tm_mon + 1, loctim->tm_mday, loctim->tm_hour, loctim->tm_min, loctim->tm_sec);
hstr = timestr;
hstr += timestr;
switch (LogLevel)
{
case uns::llTrace:
@@ -211,6 +233,40 @@ std::string ServerLogger::GenerateFileInfo(std::string filename, int line_num)
return oss.str();
}
// 高性能、100% 异常安全的 ANSI 颜色码剥离函数
inline std::string StripAnsiCodes(const std::string& input) noexcept
{
std::string result;
// 预分配内存,避免多次 Realloc(即使底层内存极度匮乏,noexcept 也会兜底)
try
{
result.reserve(input.size());
}
catch (...)
{
// 极罕见的内存耗尽情况,直接降级返回原串或空串,绝不崩溃
return input;
}
bool in_escape = false;
for (char c : input)
{
if (c == '\033') // 遇到转义字符 '\033' (ESC)
{
in_escape = true;
continue;
}
if (in_escape)
{
// ANSI 颜色控制码以 'm' 结尾(例如 \033[31m 或 \033[0m)
if (c == 'm')
in_escape = false;
continue; // 跳过转义序列内的所有字符
}
result.push_back(c);
}
return result;
}
void ServerLogger::WriteBatchToOutputs(const std::deque<std::string>& batch)
{
if (batch.empty())
@@ -222,7 +278,7 @@ void ServerLogger::WriteBatchToOutputs(const std::deque<std::string>& batch)
continue;
std::fwrite(item.c_str(), 1, item.size(), stdout);
if (LogStream.is_open())
LogStream << item;
LogStream << StripAnsiCodes(item);
RotateIfNeeded(now, true);
}
std::fflush(stdout);
@@ -366,14 +422,14 @@ inline std::string RewriteFormatString(const std::string& real_format, const uns
// escaped {{
if ((c == '{') && ((i + 1) < real_format.size()) && (real_format[i + 1] == '{'))
{
out += '{';
out += "{{";
i += 2;
continue;
}
// escaped }}
if ((c == '}') && ((i + 1) < real_format.size()) && (real_format[i + 1] == '}'))
{
out += '}';
out += "}}";
i += 2;
continue;
}
@@ -421,6 +477,9 @@ inline std::string RewriteFormatString(const std::string& real_format, const uns
++i;
}
// debug
// std::string debug = "[RewriteFormatString] RAW=|" + real_format + "|, OUT=|" + out + "|\n";
// std::fwrite(debug.c_str(), 1, debug.size(), stdout);
return out;
}
@@ -456,7 +515,7 @@ void ServerLogger::LogImpl(uns::ServerLogLevel level, const std::string& format,
std::string logstr = GenerateLogHeader(level);
logstr += formatted;
logstr += "\n";
logstr += "\033[0m\n";
std::unique_lock<std::mutex> lock(QueueMutex);
LogQueue.push_back(logstr);
@@ -497,7 +556,7 @@ void ServerLogger::LogFImpl(uns::ServerLogLevel level, const std::string& filena
std::string logstr = GenerateLogHeader(level);
logstr += formatted;
logstr += "\n";
logstr += "\033[0m\n";
std::unique_lock<std::mutex> lock(QueueMutex);
LogQueue.push_back(logstr);
@@ -527,7 +586,7 @@ void ServerLogger::LogFMTImpl(uns::ServerLogLevel level, const std::string& form
std::string logstr = GenerateLogHeader(level);
logstr += formatted;
logstr += "\n";
logstr += "\033[0m\n";
std::unique_lock<std::mutex> lock(QueueMutex);
LogQueue.push_back(logstr);
@@ -557,7 +616,7 @@ void ServerLogger::LogFMT_FImpl(uns::ServerLogLevel level, const std::string& fi
std::string logstr = GenerateLogHeader(level);
logstr += formatted;
logstr += "\n";
logstr += "\033[0m\n";
std::unique_lock<std::mutex> lock(QueueMutex);
LogQueue.push_back(logstr);