From 5c24b070bbb20f481e1cffa7f46b7c35d33b0773 Mon Sep 17 00:00:00 2001 From: youmetme <321640253@qq.com> Date: Thu, 21 Nov 2024 15:39:50 +0800 Subject: [PATCH] =?UTF-8?q?#fix=20=E6=8B=A6=E6=88=AA=E5=99=A8=E5=AF=B9leve?= =?UTF-8?q?l=E5=8F=82=E6=95=B0=E6=97=A0=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/logging/logging-core.h | 2 +- include/logging/logging-interceptor.h | 2 +- src/interceptor/logging-interceptor-substr.c | 15 ++++++++++----- src/logging.c | 18 +++++++++++------- tests/test_interceptor.c | 7 ++++--- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/include/logging/logging-core.h b/include/logging/logging-core.h index 5418900..8bcb614 100644 --- a/include/logging/logging-core.h +++ b/include/logging/logging-core.h @@ -6,7 +6,7 @@ extern "C" { #endif typedef enum { - LOG_FATAL, + LOG_FATAL = 0, LOG_ERROR, LOG_WARNING, LOG_INFO, diff --git a/include/logging/logging-interceptor.h b/include/logging/logging-interceptor.h index a552afd..716113c 100644 --- a/include/logging/logging-interceptor.h +++ b/include/logging/logging-interceptor.h @@ -18,7 +18,7 @@ typedef struct log_Interceptor { log_Handler *handler; bool jump_out; bool (*_dispose)(struct log_Interceptor *Interceptor, - char *level, + log_level level, const char *message, ...); void (*_free)(struct log_Interceptor *Interceptor); diff --git a/src/interceptor/logging-interceptor-substr.c b/src/interceptor/logging-interceptor-substr.c index b1226b0..912bafc 100644 --- a/src/interceptor/logging-interceptor-substr.c +++ b/src/interceptor/logging-interceptor-substr.c @@ -24,8 +24,10 @@ static void get_next(char *str, int *next) { } static bool kmp_search(char *substr, char *master) { - if(substr == NULL)return true; //空串全匹配 - if(master == NULL)return false; + if (substr == NULL) + return true; // 空串全匹配 + if (master == NULL) + return false; int i = 0; int j = 0; int substrlen = strlen(substr); @@ -54,16 +56,19 @@ static bool kmp_search(char *substr, char *master) { } static bool _disposeSubstring(log_Interceptor *interceptor, - char *level, + log_level level, const char *message, ...) { int count = 0; keywords_t *keyword = (keywords_t *)(interceptor + 1); - if (keyword->key == NULL && keyword->next == NULL) + if (keyword->key == NULL && keyword->next == NULL) { + if (level <= interceptor->level) + return true; return false; + } - while (keyword != NULL) { + while (keyword != NULL && level <= interceptor->level) { if (kmp_search(keyword->key, (char *)message)) return true; keyword = keyword->next; diff --git a/src/logging.c b/src/logging.c index 352e8a8..c3688c3 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1,4 +1,5 @@ #include "logging.h" +#include "logging/logging-core.h" #include "logging/logging-handler.h" #include "utils/logging-utils.h" #include @@ -92,7 +93,10 @@ static void output_to_handler(log_Handler *handler, * @param ... 格式化参数列表 * @return */ -static void _builtin_cope(char *level, const char *color, const char *message) { +static void _builtin_cope(log_level level_e, + char *level, + const char *color, + const char *message) { if (G_LOGGER == NULL) { return; } @@ -104,7 +108,7 @@ static void _builtin_cope(char *level, const char *color, const char *message) { log_Handler *handler = G_LOGGER->handler; while (it != NULL) { - if (it->_dispose(it, level, message)) { + if (it->_dispose(it, level_e, message)) { output_to_handler(it->handler, level, color, message); if (it->jump_out) return; @@ -121,7 +125,7 @@ void log_fatal(const char *message, ...) { va_start(args, message); vsprintf(logStr, message, args); va_end(args); - _builtin_cope("Fatal", RED_B, logStr); + _builtin_cope(LOG_FATAL, "Fatal", RED_B, logStr); } } @@ -132,7 +136,7 @@ void log_error(const char *message, ...) { va_start(args, message); vsprintf(logStr, message, args); va_end(args); - _builtin_cope("Error", RED, logStr); + _builtin_cope(LOG_ERROR, "Error", RED, logStr); } } @@ -143,7 +147,7 @@ void log_warning(const char *message, ...) { va_start(args, message); vsprintf(logStr, message, args); va_end(args); - _builtin_cope("Warning", YELLOW, logStr); + _builtin_cope(LOG_WARNING, "Warning", YELLOW, logStr); } } @@ -154,7 +158,7 @@ void log_info(const char *message, ...) { va_start(args, message); vsprintf(logStr, message, args); va_end(args); - _builtin_cope("Info", GREEN, logStr); + _builtin_cope(LOG_INFO, "Info", GREEN, logStr); } } @@ -165,7 +169,7 @@ void log_debug(const char *message, ...) { va_start(args, message); vsprintf(logStr, message, args); va_end(args); - _builtin_cope("Debug", CYAN, logStr); + _builtin_cope(LOG_DEBUG, "Debug", CYAN, logStr); } } diff --git a/tests/test_interceptor.c b/tests/test_interceptor.c index 64728bb..d8fe69b 100644 --- a/tests/test_interceptor.c +++ b/tests/test_interceptor.c @@ -1,4 +1,5 @@ #include "logging.h" +#include "logging/logging-core.h" #include #include #include @@ -12,7 +13,7 @@ int main() { log_debug("This is a debug message"); log_warning("This is a warning message%s", "123"); - char *test1[] = {"This",NULL}; + char *test1[] = {"This", NULL}; log_Interceptor *tint = loggingSubStringInterceptor( test1, @@ -22,11 +23,11 @@ int main() { logger->addInterceptor(tint); - char *test2[] = {"123",NULL}; + char *test2[] = {"123", NULL}; log_Interceptor *tint1 = loggingSubStringInterceptor( test2, - LOG_DEBUG, + LOG_ERROR, loggingFileHandler("test_interceptor1", 1024 * 1024), true);