diff --git a/include/logging.h b/include/logging.h index f10ab5a..fc79b75 100644 --- a/include/logging.h +++ b/include/logging.h @@ -23,19 +23,19 @@ typedef enum { } log_status; typedef struct log_Handler { - void* out; + void* stream; bool apply_color; - bool need_free; //是否有资源释放需求 void (*_free)(struct log_Handler* handler);//释放资源 + void (*output)(struct log_Handler* handler,const char* message); } log_Handler; typedef struct log_Interceptor { log_level level; //拦截级别 log_Handler* handler; //拦截目标处理器 - bool need_free; //是否有资源释放需求 bool (*_dispose)(char* level,const char *message, ...); //拦截触发器 void (*_free)(struct log_Interceptor* Interceptor); //释放资源 + } log_Interceptor; diff --git a/src/logging-handler.c b/src/logging-handler.c index a9d9570..17023b9 100644 --- a/src/logging-handler.c +++ b/src/logging-handler.c @@ -11,7 +11,12 @@ * @return */ static void __freeFileHandler(log_Handler* handler){ - fclose(handler->out); + fclose(handler->stream); + free(handler); +} + +static void outputFileHandler(log_Handler *handler, const char * message){ + fputs(message,handler->stream); } /** @@ -24,14 +29,24 @@ log_Handler* fileHandler(const char* name){ sprintf(new_file_name, "%s.log", name); FILE* fp = fopen(new_file_name, "at"); log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler)); - handler->out = fp; + handler->stream = fp; handler->apply_color = false; - handler->need_free = true; handler->_free = __freeFileHandler; + handler->output = outputFileHandler; return handler; } + +static void __freeConsoleHandler(log_Handler* handler){ + free(handler); +} + + +static void outputConsoleHandler(log_Handler* handler,const char * message){ + fputs(message,handler->stream); +} + /** * @description :控制台日志处理器 * @param @@ -39,9 +54,9 @@ log_Handler* fileHandler(const char* name){ */ log_Handler* consoleHandler(const char* name){ log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler)); - handler->out = stdout; + handler->stream = stdout; handler->apply_color = true; - handler->need_free = false; - handler->_free = NULL; + handler->_free = __freeConsoleHandler; + handler->output = outputConsoleHandler; return handler; } \ No newline at end of file diff --git a/src/logging-interceptor.c b/src/logging-interceptor.c index 19758ff..41f96db 100644 --- a/src/logging-interceptor.c +++ b/src/logging-interceptor.c @@ -9,7 +9,7 @@ static log_Interceptor* G_interceptor = NULL; static char **G_keywords = NULL; -void get_next(char *str, int *next) { +static void get_next(char *str, int *next) { next[1] = 0; int i=1; int j=0; @@ -24,7 +24,7 @@ void get_next(char *str, int *next) { } -bool kmp_search(char *substr, char *master) { +static bool kmp_search(char *substr, char *master) { int i = 0; int j = 0; int substrlen = strlen(substr); @@ -51,6 +51,11 @@ bool kmp_search(char *substr, char *master) { } +/** +* @description 处理 +* @param +* @return +*/ static bool _disposeSubstring(char* level,const char *message, ...){ int count = 0; @@ -61,9 +66,6 @@ static bool _disposeSubstring(char* level,const char *message, ...){ while (G_keywords[count] != NULL) { if (kmp_search(G_keywords[count],(char*)message)) { - if(G_interceptor->handler != NULL) { - - } return true; } count++; @@ -73,7 +75,7 @@ static bool _disposeSubstring(char* level,const char *message, ...){ } /** -* @description : 释放内存 +* @description : 完成拦截器自我释放内存 */ static void _freeSubstring(log_Interceptor* interceptor) { int sum = 0; @@ -84,6 +86,10 @@ static void _freeSubstring(log_Interceptor* interceptor) { free(G_keywords); G_keywords = NULL; + if(interceptor->handler != NULL){ + interceptor->handler->_free(interceptor->handler); + } + free(interceptor); } @@ -93,7 +99,6 @@ static void _freeSubstring(log_Interceptor* interceptor) { log_Interceptor* substringInterceptor(char *keywords[], int count, log_level level, log_Handler* handler) { log_Interceptor* interceptor = (log_Interceptor*)malloc(sizeof(log_Interceptor)); interceptor->_dispose = _disposeSubstring; - interceptor->need_free = true; interceptor->handler = handler; interceptor->level = level; interceptor->_free = _freeSubstring; diff --git a/src/logging.c b/src/logging.c index 5442907..6d16545 100644 --- a/src/logging.c +++ b/src/logging.c @@ -40,10 +40,8 @@ static void addHandler(log_Handler* handler){ return; } - if(G_LOGGER->handler->need_free){ - G_LOGGER->handler->_free(G_LOGGER->handler); - } - free(G_LOGGER->handler); + G_LOGGER->handler->_free(G_LOGGER->handler); + G_LOGGER->handler = handler; } @@ -56,10 +54,8 @@ void addInterceptor(log_Interceptor* Interceptor){ return; } - if(G_LOGGER->interceptor->need_free){ - G_LOGGER->interceptor->_free(G_LOGGER->interceptor); - } - free(G_LOGGER->interceptor); + G_LOGGER->interceptor->_free(G_LOGGER->interceptor); + G_LOGGER->interceptor = Interceptor; } @@ -92,7 +88,7 @@ static void _builtin_log(char* level, const char *color, const char* message, .. if (handler->apply_color) sprintf(logStr, "%s %s%s%s %s\n", timeStr, color,level,RESET, message); else sprintf(logStr, "%s %s %s\n", timeStr, level, message); - fputs(logStr, handler->out); + handler->output(handler,logStr); } @@ -182,6 +178,8 @@ static Logger* getLogger(const char* name, log_level level){ return G_LOGGER; } + + /** * @description :创建一个日志对象 * @return :Logging* 返回一个日志对象 @@ -201,12 +199,11 @@ log_status destroyLogging(Logging* logging){ } if (G_LOGGER != NULL){ if (G_LOGGER->handler != NULL){ - if(G_LOGGER->handler->need_free)G_LOGGER->handler->_free(G_LOGGER->handler); - free(G_LOGGER->handler); + G_LOGGER->handler->_free(G_LOGGER->handler); } if (G_LOGGER->interceptor != NULL){ - if(G_LOGGER->interceptor->need_free)G_LOGGER->interceptor->_free(G_LOGGER->interceptor); + G_LOGGER->interceptor->_free(G_LOGGER->interceptor); } free(G_LOGGER); diff --git a/test_package/test_interceptor.c b/test_package/test_interceptor.c index 7261f6f..befddb3 100644 --- a/test_package/test_interceptor.c +++ b/test_package/test_interceptor.c @@ -1,6 +1,5 @@ #include "logging.h" - int main() { Logging *log = createLogging(); Logger *logger = log->getLogger("testLogger",LOG_DEBUG);