fix log
Some checks failed
linux-mips64-gcc / linux-gcc-mips64el (push) Failing after 1m1s
linux-x64-gcc / linux-gcc (push) Failing after 50s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m1s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m7s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m24s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m5s
rpcrypto-build / build (Debug, mipsel-openwrt-linux-musl.toolchain.cmake) (push) Failing after 1m7s
rpcrypto-build / build (Release, mipsel-openwrt-linux.toolchain.cmake) (push) Failing after 1m7s
rpcrypto-build / build (Release, mipsel-openwrt-linux-musl.toolchain.cmake) (push) Failing after 1m12s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m7s
rpcrypto-build / build (Debug, mipsel-openwrt-linux.toolchain.cmake) (push) Failing after 1m16s

This commit is contained in:
tqcq 2023-12-11 12:57:02 +08:00
parent d6627c3aa8
commit 03ccb8bdec
5 changed files with 32 additions and 14 deletions

View File

@ -6,10 +6,14 @@
#define ULIB_SRC_LOG_LOG_H_
#include "logger.h"
#include <fmt/format.h>
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

View File

@ -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

View File

@ -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_;
};

View File

@ -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

View File

@ -0,0 +1,12 @@
#include <gtest/gtest.h>
#include <ulib/log/log.h>
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");
}