diff --git a/src/ulib/log/log.h b/src/ulib/log/log.h index aaf9db0..b777e8a 100644 --- a/src/ulib/log/log.h +++ b/src/ulib/log/log.h @@ -10,8 +10,8 @@ namespace tqcq { -#define _ULOG(level, fmt_str, ...) \ - tqcq::Logger::GetInstance().Log(level, __FILE__, __FUNCTION__, __LINE__, \ +#define _ULOG(level, tag, fmt_str, ...) \ + tqcq::Logger::GetInstance().Log(level, __FILE__, __FUNCTION__, __LINE__, tag, \ fmt::format(fmt_str, ##__VA_ARGS__).c_str()) #define ULOG_SET_STRIPPED_PREFIX_LEN(len) ::tqcq::Logger::GetInstance().SetStrippedPrefixLen(len) diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index f6f9040..542fd93 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -22,17 +22,20 @@ Logger::Logger() {} Logger::~Logger() {} void -Logger::Log(int32_t level, const char *file, const char *func, int32_t line, const char *msg) +Logger::Log(int32_t level, const char *file, const char *func, int32_t line, const char* tag, const char *msg) { const char *level_name = Level::ToString(level); - std::string pattern = "[{}] {}:{}@{} msg: {}"; + /** + * @brief level_name, file:line@func tag msg + */ + std::string pattern = "[{:<5}] {} {}:{}@{} {}"; // auto add CR bool need_append_line_break = !msg || *msg == '\0' || msg[strlen(msg) - 1] != '\n'; if (need_append_line_break) { pattern.append(1, '\n'); } - fmt::print(pattern, level_name, file + stripped_prefix_len_, line, func, msg); + fmt::print(pattern, level_name, tag, file + stripped_prefix_len_, line, func, msg); } void diff --git a/src/ulib/log/logger.h b/src/ulib/log/logger.h index 11f2857..3db0edf 100644 --- a/src/ulib/log/logger.h +++ b/src/ulib/log/logger.h @@ -16,7 +16,7 @@ public: Logger(); ~Logger(); static Logger &GetInstance(); - void Log(int32_t level, const char *file, const char *func, int32_t line, const char *msg); + void Log(int32_t level, const char *file, const char *func, int32_t line, const char* tag,const char *msg); void SetStrippedPrefixLen(size_t len); private: diff --git a/tests/src/ulib/log/log_test.cpp b/tests/src/ulib/log/log_test.cpp index 05b4d21..3971a3f 100644 --- a/tests/src/ulib/log/log_test.cpp +++ b/tests/src/ulib/log/log_test.cpp @@ -1,12 +1,14 @@ #include #include +static const char* kLogTag = "UnitTest"; + TEST(ULIB_LOG_TEST, base_test) { - ULOG_TRACE("trace log"); - ULOG_DEBUG("debug log"); - ULOG_INFO("info log"); - ULOG_WARN("warn log"); - ULOG_ERROR("error log"); - ULOG_FATAL("fatal log"); + ULOG_TRACE(kLogTag, "trace log"); + ULOG_DEBUG(kLogTag, "debug log"); + ULOG_INFO(kLogTag, "info log"); + ULOG_WARN(kLogTag, "warn log"); + ULOG_ERROR(kLogTag, "error log"); + ULOG_FATAL(kLogTag, "fatal log"); }