From 6b17e468595af689abfd115cba6e40afca32ef25 Mon Sep 17 00:00:00 2001 From: Boram Park Date: Mon, 5 Feb 2018 17:56:25 +0900 Subject: [PATCH] log: check log level Change-Id: I314e2b31ecfd402b75dabaa66ffd8d6198a3d5cd --- common/tdm_log.c | 15 ++++++++------- include/tdm_log.h | 50 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/common/tdm_log.c b/common/tdm_log.c index 7cce09c..2f7152b 100644 --- a/common/tdm_log.c +++ b/common/tdm_log.c @@ -63,13 +63,14 @@ static unsigned int dlog_enable = 1; static unsigned int color_enable = 1; -static unsigned int debug_level = TDM_LOG_LEVEL_INFO; static unsigned int need_check_env = 1; static unsigned int log_lock_init; static pthread_mutex_t log_lock; +unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO; + static void _tdm_log_check_env(void) { @@ -78,11 +79,11 @@ _tdm_log_check_env(void) str = getenv("TDM_DEBUG_LEVEL"); if (str) - debug_level = strtol(str, &end, 10); + tdm_log_debug_level = strtol(str, &end, 10); str = getenv("TDM_DEBUG"); if (str && (strstr(str, "1"))) - debug_level = TDM_LOG_LEVEL_DBG; + tdm_log_debug_level = TDM_LOG_LEVEL_DBG; str = getenv("TDM_DLOG"); if (str && (strstr(str, "0"))) @@ -105,15 +106,15 @@ EXTERN void tdm_log_enable_debug(unsigned int enable) { if (enable) - debug_level = TDM_LOG_LEVEL_DBG; + tdm_log_debug_level = TDM_LOG_LEVEL_DBG; else - debug_level = TDM_LOG_LEVEL_INFO; + tdm_log_debug_level = TDM_LOG_LEVEL_INFO; } EXTERN void tdm_log_set_debug_level(int level) { - debug_level = level; + tdm_log_debug_level = level; } EXTERN void @@ -132,7 +133,7 @@ tdm_log_print(int level, const char *fmt, ...) _tdm_log_check_env(); } - if (level > debug_level) + if (level > tdm_log_debug_level) return; if (dlog_enable) { diff --git a/include/tdm_log.h b/include/tdm_log.h index d402a99..1cab32d 100644 --- a/include/tdm_log.h +++ b/include/tdm_log.h @@ -70,37 +70,47 @@ void tdm_log_enable_debug(unsigned int enable); void tdm_log_set_debug_level(int level); void tdm_log_print(int level, const char *fmt, ...); +extern unsigned int tdm_log_debug_level; + #define TDM_DBG(fmt, args...) \ do { \ - struct timespec ts; \ - clock_gettime(CLOCK_MONOTONIC, &ts); \ - tdm_log_print(TDM_LOG_LEVEL_DBG, "[%5d.%06d][%d][%s %d]"fmt"\n", \ - (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ - (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + if (tdm_log_debug_level >= TDM_LOG_LEVEL_DBG) { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tdm_log_print(TDM_LOG_LEVEL_DBG, "[%5d.%06d][%d][%s %d]"fmt"\n", \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } \ } while (0) #define TDM_INFO(fmt, args...) \ do { \ - struct timespec ts; \ - clock_gettime(CLOCK_MONOTONIC, &ts); \ - tdm_log_print(TDM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d]"fmt"\n", \ - (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ - (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + if (tdm_log_debug_level >= TDM_LOG_LEVEL_INFO) { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tdm_log_print(TDM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d]"fmt"\n", \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } \ } while (0) #define TDM_WRN(fmt, args...) \ do { \ - struct timespec ts; \ - clock_gettime(CLOCK_MONOTONIC, &ts); \ - tdm_log_print(TDM_LOG_LEVEL_WRN, "[%5d.%06d][%d][%s %d]"fmt"\n", \ - (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ - (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + if (tdm_log_debug_level >= TDM_LOG_LEVEL_WRN) { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tdm_log_print(TDM_LOG_LEVEL_WRN, "[%5d.%06d][%d][%s %d]"fmt"\n", \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } \ } while (0) #define TDM_ERR(fmt, args...) \ do { \ - struct timespec ts; \ - clock_gettime(CLOCK_MONOTONIC, &ts); \ - tdm_log_print(TDM_LOG_LEVEL_ERR, "[%5d.%06d][%d][%s %d]"fmt"\n", \ - (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ - (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + if (tdm_log_debug_level >= TDM_LOG_LEVEL_ERR) { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tdm_log_print(TDM_LOG_LEVEL_ERR, "[%5d.%06d][%d][%s %d]"fmt"\n", \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } \ } while (0) #ifdef __cplusplus -- 2.7.4