From 7a0fae8f0141a7fb0c200c5867206454a070405d Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 9 Feb 2017 18:35:39 +0800 Subject: [PATCH] logger judge log_level in function log_it_ex and log_it_ex1 --- HISTORY | 3 +++ src/logger.c | 16 ++++++++++++--- src/tests/Makefile | 2 +- src/tests/test_ini_parser.c | 11 ----------- src/tests/test_logger.c | 39 +++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src/tests/test_logger.c diff --git a/HISTORY b/HISTORY index 2cbc51c..c200413 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.35 2017-02-09 + * logger judge log_level in function log_it_ex and log_it_ex1 + Version 1.34 2017-02-06 * ini_file_reader: LOCAL_IP support CIDR addresses * ini_file_reader: return the last when get single value, diff --git a/src/logger.c b/src/logger.c index 0f8a6b8..e9680b4 100644 --- a/src/logger.c +++ b/src/logger.c @@ -1051,7 +1051,12 @@ void log_it_ex1(LogContext *pContext, const int priority, \ bool bNeedSync; char *caption; - switch(priority) + if (pContext->log_level < priority) + { + return; + } + + switch (priority) { case LOG_DEBUG: bNeedSync = true; @@ -1100,8 +1105,13 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... char text[LINE_MAX]; char *caption; int len; - va_list ap; + + if (pContext->log_level < priority) + { + return; + } + va_start(ap, format); len = vsnprintf(text, sizeof(text), format, ap); va_end(ap); @@ -1110,7 +1120,7 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... len = sizeof(text) - 1; } - switch(priority) + switch (priority) { case LOG_DEBUG: bNeedSync = true; diff --git a/src/tests/Makefile b/src/tests/Makefile index a62a9cb..8d76a32 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -5,7 +5,7 @@ INC_PATH = -I/usr/include/fastcommon LIB_PATH = -lfastcommon -lpthread ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \ - test_id_generator test_ini_parser test_char_convert test_char_convert_loader + test_id_generator test_ini_parser test_char_convert test_char_convert_loader test_logger all: $(ALL_PRGS) .c: diff --git a/src/tests/test_ini_parser.c b/src/tests/test_ini_parser.c index 1044bd2..01f01cc 100644 --- a/src/tests/test_ini_parser.c +++ b/src/tests/test_ini_parser.c @@ -21,17 +21,6 @@ int main(int argc, char *argv[]) } log_init(); - g_log_context.log_level = LOG_DEBUG; - log_take_over_stderr(); - log_take_over_stdout(); - log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD); - - printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, " - "use_file_write_lock: %d\n", (int)sizeof(LogContext), - g_log_context.time_precision, - g_log_context.compress_log_flags, - g_log_context.use_file_write_lock); - if ((result=iniLoadFromFile(szFilename, &context)) != 0) { return result; diff --git a/src/tests/test_logger.c b/src/tests/test_logger.c new file mode 100644 index 0000000..b7062e9 --- /dev/null +++ b/src/tests/test_logger.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "logger.h" + +int main(int argc, char *argv[]) +{ + char buff[256]; + int len; + + log_init(); + //g_log_context.log_level = LOG_DEBUG; + g_log_context.log_level = LOG_INFO; + log_take_over_stderr(); + log_take_over_stdout(); + log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD); + + printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, " + "use_file_write_lock: %d\n", (int)sizeof(LogContext), + g_log_context.time_precision, + g_log_context.compress_log_flags, + g_log_context.use_file_write_lock); + + log_it_ex(&g_log_context, LOG_DEBUG, + "by log_it_ex, timestamp: %d", (int)time(NULL)); + + len = sprintf(buff, "this is by log_it_ex1, " + "timestamp: %d", (int)time(NULL)); + log_it_ex1(&g_log_context, LOG_INFO, buff, len); + + return 0; +} +