feat log support color
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 31s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 34s

This commit is contained in:
tqcq 2024-03-05 11:47:43 +08:00
parent 5900bdf0f6
commit 0ef5b929c2

View File

@ -7,6 +7,54 @@
namespace sled {
static const char *
GetConsoleColorPrefix(LogLevel level)
{
switch (level) {
case LogLevel::kTrace:
return "\033[0;37m";
case LogLevel::kDebug:
return "\033[0;36m";
case LogLevel::kInfo:
return "\033[0;32m";
case LogLevel::kWarning:
return "\033[0;33m";
case LogLevel::kError:
return "\033[0;31m";
case LogLevel::kFatal:
return "\033[0;31m";
default:
return "\033[0m";
}
}
static const char *
GetConsoleColorSuffix()
{
return "\033[0m";
}
static const char *
ToShortString(LogLevel level)
{
switch (level) {
case LogLevel::kTrace:
return "T";
case LogLevel::kDebug:
return "D";
case LogLevel::kInfo:
return "I";
case LogLevel::kWarning:
return "W";
case LogLevel::kError:
return "E";
case LogLevel::kFatal:
return "F";
default:
return "#";
}
}
class ScopedAtomicWaiter {
public:
ScopedAtomicWaiter(std::atomic_bool &flag) : flag_(flag)
@ -63,9 +111,11 @@ Log(LogLevel level,
int len = file_name ? strlen(file_name) : 0;
while (len > 0 && file_name[len - 1] != '/') { len--; }
auto msg = fmt::format("{} {}:{}@{} {} {}", GetCurrentUTCTime(),
file_name + len, line, func_name, tag, fmt);
std::cout << msg << std::endl;
auto msg = fmt::format("{} {} {} {}:{}@{}: {}", GetCurrentUTCTime(),
ToShortString(level), tag, file_name + len, line,
func_name, fmt);
std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix()
<< std::endl;
}
}// namespace sled