diff --git a/README.en.md b/README.en.md index 9a3cb74..68f6342 100644 --- a/README.en.md +++ b/README.en.md @@ -48,7 +48,7 @@ int main() { int main() { Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG); - logger->addHandler(loggingFileHandler("test1", 1024*1024)); + logger->addHandler(loggingHandlerFile("test1", 1024*1024)); log_info("This is an info message"); log_error("This is an error message%s", "123"); @@ -61,9 +61,9 @@ int main() { } ``` -### Logging Interceptor +### Logging filter > Support adding custom interceptors, currently with built-in substring interceptors -> The function of an interceptor is to redirect intercepted logs to the interceptor's dedicated processor +> The function of an filter is to redirect intercepted logs to the filter's dedicated processor #### example @@ -83,16 +83,16 @@ int main() { char *test1[] = {"123", "tt", NULL}; - log_Interceptor *tint = loggingSubStringInterceptor( + log_filter *tint = loggingFilterSubStr( test1, LOG_DEBUG, - loggingFileHandler("test_interceptor", 1024 * 1024), + loggingHandlerFile("test_interceptor", 1024 * 1024), true); - logger->addInterceptor(tint); + logger->addFilter(tint); printf("\n"); - printf("Interceptor added\n"); + printf("filter added\n"); printf("\n"); log_info("This is an info message"); @@ -124,26 +124,26 @@ int main() { char *test1[] = {"This",NULL}; - log_Interceptor *tint = loggingSubStringInterceptor( + log_filter *tint = loggingFilterSubStr( test1, LOG_DEBUG, - loggingFileHandler("test_interceptor", 1024 * 1024), + loggingHandlerFile("test_interceptor", 1024 * 1024), false); - logger->addInterceptor(tint); + logger->addFilter(tint); char *test2[] = {"123",NULL}; - log_Interceptor *tint1 = loggingSubStringInterceptor( + log_filter *tint1 = loggingFilterSubStr( test2, LOG_DEBUG, - loggingFileHandler("test_interceptor1", 1024 * 1024), + loggingHandlerFile("test_interceptor1", 1024 * 1024), true); - logger->addInterceptor(tint1); + logger->addFilter(tint1); printf("\n"); - printf("Interceptor added\n"); + printf("filter added\n"); printf("\n"); log_info("This is an info message"); diff --git a/README.md b/README.md index 654007a..7337317 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ int main() { int main() { Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG); - logger->addHandler(loggingFileHandler("test1", 1024*1024)); + logger->addHandler(loggingHandlerFile("test1", 1024*1024)); log_info("This is an info message"); log_error("This is an error message%s", "123"); @@ -87,16 +87,16 @@ int main() { char *test1[] = {"123", "tt", NULL}; - log_Interceptor *tint = loggingSubStringInterceptor( + log_filter *tint = loggingFilterSubStr( test1, LOG_DEBUG, - loggingFileHandler("test_interceptor", 1024 * 1024), + loggingHandlerFile("test_interceptor", 1024 * 1024), true); - logger->addInterceptor(tint); + logger->addFilter(tint); printf("\n"); - printf("Interceptor added\n"); + printf("filter added\n"); printf("\n"); log_info("This is an info message"); @@ -128,26 +128,26 @@ int main() { char *test1[] = {"This",NULL}; - log_Interceptor *tint = loggingSubStringInterceptor( + log_filter *tint = loggingFilterSubStr( test1, LOG_DEBUG, - loggingFileHandler("test_interceptor", 1024 * 1024), + loggingHandlerFile("test_interceptor", 1024 * 1024), false); - logger->addInterceptor(tint); + logger->addFilter(tint); char *test2[] = {"123",NULL}; - log_Interceptor *tint1 = loggingSubStringInterceptor( + log_filter *tint1 = loggingFilterSubStr( test2, LOG_DEBUG, - loggingFileHandler("test_interceptor1", 1024 * 1024), + loggingHandlerFile("test_interceptor1", 1024 * 1024), true); - logger->addInterceptor(tint1); + logger->addFilter(tint1); printf("\n"); - printf("Interceptor added\n"); + printf("filter added\n"); printf("\n"); log_info("This is an info message"); diff --git a/conanfile.py b/conanfile.py index 3a0b791..d56ce44 100644 --- a/conanfile.py +++ b/conanfile.py @@ -5,7 +5,7 @@ import os class loggingRecipe(ConanFile): name = "logging" - version = "0.4.0" + version = "0.5.0" license = "MIT" author = "321640253@qq.com" url = "https://github.com/WangZhongDian/logging.git" diff --git a/include/logging.h b/include/logging.h index ce83afe..f995f14 100644 --- a/include/logging.h +++ b/include/logging.h @@ -5,20 +5,20 @@ #include #include "logging/logging-core.h" +#include "logging/logging-filter.h" #include "logging/logging-handler.h" -#include "logging/logging-interceptor.h" #ifdef __cplusplus extern "C" { #endif typedef struct Logger { - log_level level; - log_Handler *handler; - log_Interceptor *interceptor; - const char *name; + log_level level; + log_Handler *handler; + log_filter *filter; + const char *name; bool (*addHandler)(log_Handler *handler); - bool (*addInterceptor)(log_Interceptor *Interceptor); + bool (*addFilter)(log_filter *filter); } Logger; void log_fatal(const char *format, ...); diff --git a/include/logging/logging-filter.h b/include/logging/logging-filter.h new file mode 100644 index 0000000..6f0981e --- /dev/null +++ b/include/logging/logging-filter.h @@ -0,0 +1,41 @@ +#ifndef __LOGGING_INTERCEPTOR_H__ +#define __LOGGING_INTERCEPTOR_H__ + +#include "logging-core.h" +#include "logging-handler.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct log_filter { + log_level level; + log_Handler *handler; + bool jump_out; + bool (*_dispose)(struct log_filter *filter, + log_level level, + const char *message, + ...); + void (*_free)(struct log_filter *filter); + struct log_filter *next; +} log_filter; + +/** + * @brief 子字符串过滤器 + * @param keywords: 关键字数组 + * @param count: 关键字数组长度 + * @param level: 过滤截日志等级 + * @param handler: 日志处理器,用于处理过滤下来的日志 + * @return log_filter * + */ +log_filter *loggingFilterSubStr(char *keywords[], + log_level level, + log_Handler *handler, + bool jump_out); + +#ifdef __cplusplus +} +#endif + +#endif // __LOGGING_INTERCEPTOR_H__ \ No newline at end of file diff --git a/include/logging/logging-handler.h b/include/logging/logging-handler.h index 79f341f..e4683ce 100644 --- a/include/logging/logging-handler.h +++ b/include/logging/logging-handler.h @@ -14,8 +14,20 @@ typedef struct log_Handler { void (*output)(struct log_Handler *handler, const char *message); } log_Handler; -log_Handler *loggingFileHandler(const char *name, unsigned int max_size); -log_Handler *loggingConsoleHandler(); +/** + * @brief 文件处理器 + * @param name 文件名 + * @param max_size 文件最大大小 + * @return + */ +log_Handler *loggingHandlerFile(const char *name, unsigned int max_size); + +/** + * @brief 控制台处理器 + * @param + * @return + */ +log_Handler *loggingHandlerConsole(); #ifdef __cplusplus } diff --git a/include/logging/logging-interceptor.h b/include/logging/logging-interceptor.h deleted file mode 100644 index 716113c..0000000 --- a/include/logging/logging-interceptor.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef __LOGGING_INTERCEPTOR_H__ -#define __LOGGING_INTERCEPTOR_H__ - -#include "logging-core.h" -#include "logging-handler.h" -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct log_Interceptor { - log_level level; - log_Handler *handler; - bool jump_out; - bool (*_dispose)(struct log_Interceptor *Interceptor, - log_level level, - const char *message, - ...); - void (*_free)(struct log_Interceptor *Interceptor); - struct log_Interceptor *next; -} log_Interceptor; - -/** - * @brief 子字符串拦截器 - * @param keywords: 关键字数组 - * @param count: 关键字数组长度 - * @param level: 最低拦截日志等级 - * @param handler: 日志处理器,用于处理拦截下来的日志 - * @return log_Interceptor * - */ -log_Interceptor *loggingSubStringInterceptor(char *keywords[], - log_level level, - log_Handler *handler, - bool jump_out); - -#ifdef __cplusplus -} -#endif - -#ifdef __cplusplus -} -#endif - -#endif // __LOGGING_INTERCEPTOR_H__ \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d01b49c..30a7898 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,7 @@ project(logging) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/handler SRC) -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/interceptor SRC) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/filter SRC) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/utils SRC) if (SHARED) diff --git a/src/interceptor/logging-interceptor-substr.c b/src/filter/logging-filter-substr.c similarity index 61% rename from src/interceptor/logging-interceptor-substr.c rename to src/filter/logging-filter-substr.c index 912bafc..f8c66f7 100644 --- a/src/interceptor/logging-interceptor-substr.c +++ b/src/filter/logging-filter-substr.c @@ -1,4 +1,4 @@ -#include "logging/logging-interceptor.h" +#include "logging/logging-filter.h" #include #include #include @@ -55,20 +55,20 @@ static bool kmp_search(char *substr, char *master) { return false; } -static bool _disposeSubstring(log_Interceptor *interceptor, - log_level level, - const char *message, +static bool _disposeSubstring(log_filter *filter, + log_level level, + const char *message, ...) { int count = 0; - keywords_t *keyword = (keywords_t *)(interceptor + 1); + keywords_t *keyword = (keywords_t *)(filter + 1); if (keyword->key == NULL && keyword->next == NULL) { - if (level <= interceptor->level) + if (level <= filter->level) return true; return false; } - while (keyword != NULL && level <= interceptor->level) { + while (keyword != NULL && level <= filter->level) { if (kmp_search(keyword->key, (char *)message)) return true; keyword = keyword->next; @@ -77,9 +77,9 @@ static bool _disposeSubstring(log_Interceptor *interceptor, return false; } -static void _freeSubstring(log_Interceptor *interceptor) { +static void _freeFilter(log_filter *filter) { keywords_t *it_keyword = - (keywords_t *)(interceptor + 1); // it_keyword 不是起始地址,请勿free + (keywords_t *)(filter + 1); // it_keyword 不是起始地址,请勿free keywords_t *keyword = it_keyword->next; keywords_t *next = NULL; @@ -90,30 +90,31 @@ static void _freeSubstring(log_Interceptor *interceptor) { keyword = next; } - if (interceptor->handler != NULL) { - interceptor->handler->_free(interceptor->handler); + if (filter->handler != NULL) { + filter->handler->_free(filter->handler); } if (it_keyword->key != NULL) free(it_keyword->key); - free(interceptor); + free(filter); } -log_Interceptor *loggingSubStringInterceptor(char *keywords[], - log_level level, - log_Handler *handler, - bool jump_out) { - log_Interceptor *interceptor = - (log_Interceptor *)malloc(sizeof(log_Interceptor) + sizeof(keywords_t)); - interceptor->_dispose = _disposeSubstring; - interceptor->handler = handler; - interceptor->level = level; - interceptor->jump_out = jump_out; - interceptor->_free = _freeSubstring; +log_filter *loggingFilterSubStr(char *keywords[], + log_level level, + log_Handler *handler, + bool jump_out) { + // 分配log_filter和keywords_t的连续内存 + log_filter *filter = + (log_filter *)malloc(sizeof(log_filter) + sizeof(keywords_t)); + filter->_dispose = _disposeSubstring; + filter->handler = handler; + filter->level = level; + filter->jump_out = jump_out; + filter->_free = _freeFilter; - keywords_t *keyword = (keywords_t *)(interceptor + 1); - keyword->key = NULL; - int count = 0; + keywords_t *keyword = (keywords_t *)(filter + 1); + keyword->key = NULL; + int count = 0; if (keywords[count] != NULL) { keyword->key = strdup(keywords[count]); count++; @@ -128,5 +129,5 @@ log_Interceptor *loggingSubStringInterceptor(char *keywords[], } keyword->next = NULL; - return interceptor; + return filter; } \ No newline at end of file diff --git a/src/handler/logging-handler-console.c b/src/handler/logging-handler-console.c index a71c99d..c5790e3 100644 --- a/src/handler/logging-handler-console.c +++ b/src/handler/logging-handler-console.c @@ -2,19 +2,19 @@ #include #include -static void __freeConsoleHandler(log_Handler *handler) { free(handler); } +static void __freeHandlerConsole(log_Handler *handler) { free(handler); } -static void outputConsoleHandler(log_Handler *handler, const char *message) { +static void __outputHandlerConsole(log_Handler *handler, const char *message) { fputs(message, handler->stream); } -log_Handler *loggingConsoleHandler() { +log_Handler *loggingHandlerConsole() { log_Handler *handler = (log_Handler *)malloc(sizeof(log_Handler)); handler->stream = stdout; handler->apply_color = true; - handler->_free = __freeConsoleHandler; - handler->output = outputConsoleHandler; + handler->_free = __freeHandlerConsole; + handler->output = __outputHandlerConsole; return handler; } \ No newline at end of file diff --git a/src/handler/logging-handler-file.c b/src/handler/logging-handler-file.c index ec92abc..07dc60b 100644 --- a/src/handler/logging-handler-file.c +++ b/src/handler/logging-handler-file.c @@ -49,7 +49,7 @@ static void outputFileHandler(log_Handler *handler, const char *message) { changeFile(handler); } -log_Handler *loggingFileHandler(const char *name, unsigned int max_size) { +log_Handler *loggingHandlerFile(const char *name, unsigned int max_size) { char new_file_name[FILE_NAME_MAX_SIZE]; int suffix = 0; unsigned int file_size; diff --git a/src/logging.c b/src/logging.c index c3688c3..3b9a3d9 100644 --- a/src/logging.c +++ b/src/logging.c @@ -38,23 +38,23 @@ static bool addHandler(log_Handler *handler) { return true; } -static bool addInterceptor(log_Interceptor *Interceptor) { - if (G_LOGGER == NULL || Interceptor == NULL) { +static bool addFilter(log_filter *filter) { + if (G_LOGGER == NULL || filter == NULL) { return false; } - if (G_LOGGER->interceptor == NULL) { - G_LOGGER->interceptor = Interceptor; - G_LOGGER->interceptor->next = NULL; + if (G_LOGGER->filter == NULL) { + G_LOGGER->filter = filter; + G_LOGGER->filter->next = NULL; return true; } - log_Interceptor *it = G_LOGGER->interceptor; + log_filter *it = G_LOGGER->filter; while (it->next != NULL) { it = it->next; } - it->next = Interceptor; - Interceptor->next = NULL; + it->next = filter; + filter->next = NULL; return true; } @@ -104,8 +104,8 @@ static void _builtin_cope(log_level level_e, return; } - log_Interceptor *it = G_LOGGER->interceptor; - log_Handler *handler = G_LOGGER->handler; + log_filter *it = G_LOGGER->filter; + log_Handler *handler = G_LOGGER->handler; while (it != NULL) { if (it->_dispose(it, level_e, message)) { @@ -180,17 +180,17 @@ Logger *newDefaultLogger(const char *name, log_level level) { return G_LOGGER; } - Logger *logger = (Logger *)malloc(sizeof(Logger)); + Logger *logger = (Logger *)malloc(sizeof(Logger)); - logger->addHandler = addHandler; - logger->addInterceptor = addInterceptor; + logger->addHandler = addHandler; + logger->addFilter = addFilter; - logger->level = level; - logger->handler = loggingConsoleHandler(); - logger->name = name; - logger->interceptor = NULL; + logger->level = level; + logger->handler = loggingHandlerConsole(); + logger->name = name; + logger->filter = NULL; - G_LOGGER = logger; + G_LOGGER = logger; return G_LOGGER; } @@ -203,9 +203,9 @@ log_status destroyDefaultLogger(void) { G_LOGGER->handler->_free(G_LOGGER->handler); } - if (G_LOGGER->interceptor != NULL) { - log_Interceptor *it = G_LOGGER->interceptor; - log_Interceptor *next = NULL; + if (G_LOGGER->filter != NULL) { + log_filter *it = G_LOGGER->filter; + log_filter *next = NULL; while (it != NULL) { next = it->next; it->_free(it); diff --git a/src/utils/logging-utils.c b/src/utils/logging-utils.c index e9480d2..e71d218 100644 --- a/src/utils/logging-utils.c +++ b/src/utils/logging-utils.c @@ -1,11 +1,11 @@ #include "logging-utils.h" -#include #include +#include /** -* @brief 获取当前时间字符串 -* @param timeStr 存储时间字符串缓冲区指针 -*/ + * @brief 获取当前时间字符串 + * @param timeStr 存储时间字符串缓冲区指针 + */ void getTimeStr(char *timeStr) { time_t t = time(NULL); struct tm *p = localtime(&t); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c226573..88c7f64 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,7 +3,7 @@ project(test) enable_testing() #测试简单基本应用 -add_executable(${PROJECT_NAME}simple test_simple.c) +add_executable(${PROJECT_NAME}simple test-simple.c) target_link_libraries(${PROJECT_NAME}simple logging) if(UNIX) add_test(test_simple ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}simple) @@ -12,10 +12,10 @@ elseif(WIN32) endif() #测试拦截器 -add_executable(${PROJECT_NAME}interceptor test_interceptor.c) -target_link_libraries(${PROJECT_NAME}interceptor logging) +add_executable(${PROJECT_NAME}filter test-filter.c) +target_link_libraries(${PROJECT_NAME}filter logging) if(UNIX) - add_test(test_interceptor ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}interceptor) + add_test(test_filter ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}filter) elseif(WIN32) - add_test(test_interceptor ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}interceptor.exe) + add_test(test_filter ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}filter.exe) endif() \ No newline at end of file diff --git a/tests/test_interceptor.c b/tests/test-filter.c similarity index 60% rename from tests/test_interceptor.c rename to tests/test-filter.c index d8fe69b..4471569 100644 --- a/tests/test_interceptor.c +++ b/tests/test-filter.c @@ -1,5 +1,6 @@ #include "logging.h" #include "logging/logging-core.h" +#include "logging/logging-filter.h" #include #include #include @@ -13,28 +14,28 @@ 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, - LOG_DEBUG, - loggingFileHandler("test_interceptor", 1024 * 1024), - false); + log_filter *tint = + loggingFilterSubStr(test1, + LOG_DEBUG, + loggingHandlerFile("test_interceptor", 1024 * 1024), + false); - logger->addInterceptor(tint); + logger->addFilter(tint); - char *test2[] = {"123", NULL}; + char *test2[] = {"123", NULL}; - log_Interceptor *tint1 = loggingSubStringInterceptor( + log_filter *tint1 = loggingFilterSubStr( test2, LOG_ERROR, - loggingFileHandler("test_interceptor1", 1024 * 1024), + loggingHandlerFile("test_interceptor1", 1024 * 1024), true); - logger->addInterceptor(tint1); + logger->addFilter(tint1); printf("\n"); - printf("Interceptor added\n"); + printf("filter added\n"); printf("\n"); log_info("This is an info message"); diff --git a/tests/test_simple.c b/tests/test-simple.c similarity index 100% rename from tests/test_simple.c rename to tests/test-simple.c