From 51f923757d248ac21ebcaeace3a18641edc4126f Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Thu, 28 Dec 2023 16:11:10 +0800 Subject: [PATCH] feat log add color --- src/ulib/log/level.h | 22 ++++++++++++++++++++++ src/ulib/log/logger.cpp | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ulib/log/level.h b/src/ulib/log/level.h index 6c9ae0e..b35f7f6 100644 --- a/src/ulib/log/level.h +++ b/src/ulib/log/level.h @@ -42,6 +42,28 @@ public: FATAL = ULOG_LEVEL_FATAL }; + static const char *GetConsoleColorPrefix(int level) + { + switch (level) { + case kTrace: + return "\033[0;37m"; + case kDebug: + return "\033[0;36m"; + case kInfo: + return "\033[0;32m"; + case kWarn: + return "\033[0;33m"; + case kError: + return "\033[0;31m"; + case kFatal: + return "\033[0;31m"; + default: + return "\033[0m"; + } + } + + static const char *GetConsoleColorSuffix(int level) { return "\033[0m"; } + static const char *ToShortString(int level) { switch (level) { diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index d87aa2f..85b923d 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -62,7 +62,8 @@ Logger::Log(int32_t level, /** * @brief time level_name tag file:line@func msg */ - std::string pattern = "{} {} {} {}:{}@{}: {}"; + std::string pattern = std::string() + Level::GetConsoleColorPrefix(level) + + "{} {} {} {}:{}@{}: {}" + Level::GetConsoleColorSuffix(level); std::string log_time; { std::time_t now = time(NULL);