feature support log control

This commit is contained in:
tqcq 2023-12-11 16:43:34 +08:00
parent 15fa29c4c5
commit db23cfa645
4 changed files with 28 additions and 1 deletions

View File

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

View File

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

View File

@ -8,7 +8,9 @@
#include <sstream>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <algorithm>
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<int>(Level::kALL), std::min(static_cast<int>(Level::kOFF), level));
level_ = static_cast<Level::LevelEnum>(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

View File

@ -24,6 +24,7 @@ private:
Logger &operator=(const Logger &);
size_t stripped_prefix_len_;
Level::LevelEnum level_;
};
}// namespace tqcq