From 03ccb8bdecf416292351180c239d4678102e63d7 Mon Sep 17 00:00:00 2001 From: tqcq Date: Mon, 11 Dec 2023 12:57:02 +0800 Subject: [PATCH] fix log --- src/ulib/log/log.h | 6 +++++- src/ulib/log/logger.cpp | 15 +++++++-------- src/ulib/log/logger.h | 9 +++++---- tests/CMakeLists.txt | 4 +++- tests/src/ulib/log/log_test.cpp | 12 ++++++++++++ 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 tests/src/ulib/log/log_test.cpp diff --git a/src/ulib/log/log.h b/src/ulib/log/log.h index f92b6ea..aaf9db0 100644 --- a/src/ulib/log/log.h +++ b/src/ulib/log/log.h @@ -6,10 +6,14 @@ #define ULIB_SRC_LOG_LOG_H_ #include "logger.h" +#include namespace tqcq { -#define _ULOG(level, ...) ::tqcq::Logger::GetInstance().Log(level, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) +#define _ULOG(level, fmt_str, ...) \ + tqcq::Logger::GetInstance().Log(level, __FILE__, __FUNCTION__, __LINE__, \ + fmt::format(fmt_str, ##__VA_ARGS__).c_str()) + #define ULOG_SET_STRIPPED_PREFIX_LEN(len) ::tqcq::Logger::GetInstance().SetStrippedPrefixLen(len) #if ULOG_LEVEL <= ULOG_LEVEL_TRACE diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index f1bd221..6812e50 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -22,17 +22,16 @@ Logger::Logger() {} Logger::~Logger() {} void -Logger::Log(int32_t level, const char *file, const char *func, int32_t line, const char *fmt, ...) +Logger::Log(int32_t level, const char *file, const char *func, int32_t line, const char *msg) { const char *level_name = Level::ToString(level); - char buffer[8192]; - { - va_list args; - va_start(args, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, args); - va_end(args); + fmt::print("[{}] {}:{}@{} msg: {}", level_name, file + stripped_prefix_len_, line, func, msg); + + // auto add CR + if (msg) { + size_t len = strlen(msg); + if (len > 0 && msg[len - 1] != '\n') { fmt::print("\n"); } } - fmt::print("[{}] {}:{}@{} msg: {}", level_name, file + stripped_prefix_len_, line, func, buffer); } void diff --git a/src/ulib/log/logger.h b/src/ulib/log/logger.h index 3c18044..11f2857 100644 --- a/src/ulib/log/logger.h +++ b/src/ulib/log/logger.h @@ -15,12 +15,13 @@ class Logger { public: Logger(); ~Logger(); - static Logger& GetInstance(); - void Log(int32_t level, const char* file, const char* func, int32_t line, const char* fmt, ...); + static Logger &GetInstance(); + void Log(int32_t level, const char *file, const char *func, int32_t line, const char *msg); void SetStrippedPrefixLen(size_t len); + private: - Logger(const Logger&); - Logger& operator=(const Logger&); + Logger(const Logger &); + Logger &operator=(const Logger &); size_t stripped_prefix_len_; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5903c99..db0a34a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,7 +2,9 @@ set(CMAKE_CXX_STANDARD 98) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(ulib_test - src/ulib/base/types_test.cpp) + src/ulib/base/types_test.cpp + src/ulib/log/log_test.cpp +) target_link_libraries(ulib_test PRIVATE ulib gtest diff --git a/tests/src/ulib/log/log_test.cpp b/tests/src/ulib/log/log_test.cpp new file mode 100644 index 0000000..f3eb38e --- /dev/null +++ b/tests/src/ulib/log/log_test.cpp @@ -0,0 +1,12 @@ +#include +#include + +TEST(ULIB_LOG, 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"); +}