调整处理器结构体

This commit is contained in:
2024-08-14 13:02:27 +08:00
parent d2a2b933a5
commit 96a417ba93
5 changed files with 23 additions and 9 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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++;

View File

@@ -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);
}

View File

@@ -1,6 +1,5 @@
#include "logging.h"
int main() {
Logging *log = createLogging();
Logger *logger = log->getLogger("testLogger",LOG_DEBUG);