diff --git a/include/logging.h b/include/logging.h index b3e706e..fc79b75 100644 --- a/include/logging.h +++ b/include/logging.h @@ -23,9 +23,10 @@ typedef enum { } log_status; typedef struct log_Handler { - void* out; + void* stream; bool apply_color; void (*_free)(struct log_Handler* handler);//释放资源 + void (*output)(struct log_Handler* handler,const char* message); } log_Handler; @@ -34,6 +35,7 @@ typedef struct log_Interceptor { log_Handler* handler; //拦截目标处理器 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 2a0e958..17023b9 100644 --- a/src/logging-handler.c +++ b/src/logging-handler.c @@ -11,10 +11,14 @@ * @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); +} + /** * @description :文件日志处理器 * @param @@ -25,18 +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->_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 @@ -44,8 +54,9 @@ static void __freeConsoleHandler(log_Handler* handler){ */ 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->_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 51d0a58..41f96db 100644 --- a/src/logging-interceptor.c +++ b/src/logging-interceptor.c @@ -51,6 +51,11 @@ static 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++; diff --git a/src/logging.c b/src/logging.c index 8fd3d3e..6d16545 100644 --- a/src/logging.c +++ b/src/logging.c @@ -88,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); } 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);