From db23cfa64527a2f81b1fdbb6bc9c3e543a4e57e6 Mon Sep 17 00:00:00 2001 From: tqcq Date: Mon, 11 Dec 2023 16:43:34 +0800 Subject: [PATCH] feature support log control --- src/ulib/log/level.h | 10 ++++++++++ src/ulib/log/log.h | 1 + src/ulib/log/logger.cpp | 17 ++++++++++++++++- src/ulib/log/logger.h | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ulib/log/level.h b/src/ulib/log/level.h index 3d17c49..dec2af3 100644 --- a/src/ulib/log/level.h +++ b/src/ulib/log/level.h @@ -18,9 +18,19 @@ namespace tqcq { #define ULOG_LEVEL_OFF (ULOG_LEVEL_FATAL + 1) #define ULOG_LEVEL_LEVEL ULOG_LEVEL_TRACE +// env control +// trace +// debug +// info +// warn +// error +// fatal + class Level { public: enum LevelEnum { + kALL = ULOG_LEVEL_ALL, + kOFF = ULOG_LEVEL_OFF, kTrace = ULOG_LEVEL_TRACE, kDebug= ULOG_LEVEL_DEBUG, kInfo = ULOG_LEVEL_INFO, diff --git a/src/ulib/log/log.h b/src/ulib/log/log.h index b777e8a..2f7fe0e 100644 --- a/src/ulib/log/log.h +++ b/src/ulib/log/log.h @@ -16,6 +16,7 @@ namespace tqcq { #define ULOG_SET_STRIPPED_PREFIX_LEN(len) ::tqcq::Logger::GetInstance().SetStrippedPrefixLen(len) + #if ULOG_LEVEL <= ULOG_LEVEL_TRACE #define ULOG_TRACE(...) _ULOG(ULOG_LEVEL_TRACE, __VA_ARGS__) #else diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index 2575bab..4f46451 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include namespace tqcq { @@ -19,13 +21,26 @@ Logger::GetInstance() return instance; } -Logger::Logger() {} +Logger::Logger() { + const char* env_level = getenv("ULIB_LOG_LEVEL"); + printf("ULIB_LOG_LEVEL: %s\n", env_level); + if (!env_level) { + level_ = Level::kALL; + } else { + int level = atoi(env_level); + level = std::max(static_cast(Level::kALL), std::min(static_cast(Level::kOFF), level)); + level_ = static_cast(level); + } +} Logger::~Logger() {} void Logger::Log(int32_t level, const char *file, const char *func, int32_t line, const char* tag, const char *msg) { + if (level < level_) { + return; + } const char *level_name = Level::ToString(level); /** * @brief time file:line@func tag level_name msg diff --git a/src/ulib/log/logger.h b/src/ulib/log/logger.h index 3db0edf..9166df0 100644 --- a/src/ulib/log/logger.h +++ b/src/ulib/log/logger.h @@ -24,6 +24,7 @@ private: Logger &operator=(const Logger &); size_t stripped_prefix_len_; + Level::LevelEnum level_; }; }// namespace tqcq