修复资源未释放bug,简化释放流程

This commit is contained in:
2024-08-13 22:43:54 +08:00
parent 2608dfa078
commit d2a2b933a5
4 changed files with 22 additions and 20 deletions

View File

@@ -25,7 +25,6 @@ typedef enum {
typedef struct log_Handler { typedef struct log_Handler {
void* out; void* out;
bool apply_color; bool apply_color;
bool need_free; //是否有资源释放需求
void (*_free)(struct log_Handler* handler);//释放资源 void (*_free)(struct log_Handler* handler);//释放资源
} log_Handler; } log_Handler;
@@ -33,7 +32,6 @@ typedef struct log_Handler {
typedef struct log_Interceptor { typedef struct log_Interceptor {
log_level level; //拦截级别 log_level level; //拦截级别
log_Handler* handler; //拦截目标处理器 log_Handler* handler; //拦截目标处理器
bool need_free; //是否有资源释放需求
bool (*_dispose)(char* level,const char *message, ...); //拦截触发器 bool (*_dispose)(char* level,const char *message, ...); //拦截触发器
void (*_free)(struct log_Interceptor* Interceptor); //释放资源 void (*_free)(struct log_Interceptor* Interceptor); //释放资源
} log_Interceptor; } log_Interceptor;

View File

@@ -12,6 +12,7 @@
*/ */
static void __freeFileHandler(log_Handler* handler){ static void __freeFileHandler(log_Handler* handler){
fclose(handler->out); fclose(handler->out);
free(handler);
} }
/** /**
@@ -26,12 +27,16 @@ log_Handler* fileHandler(const char* name){
log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler)); log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler));
handler->out = fp; handler->out = fp;
handler->apply_color = false; handler->apply_color = false;
handler->need_free = true;
handler->_free = __freeFileHandler; handler->_free = __freeFileHandler;
return handler; return handler;
} }
static void __freeConsoleHandler(log_Handler* handler){
free(handler);
}
/** /**
* @description :控制台日志处理器 * @description :控制台日志处理器
* @param * @param
@@ -41,7 +46,6 @@ log_Handler* consoleHandler(const char* name){
log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler)); log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler));
handler->out = stdout; handler->out = stdout;
handler->apply_color = true; handler->apply_color = true;
handler->need_free = false; handler->_free = __freeConsoleHandler;
handler->_free = NULL;
return handler; return handler;
} }

View File

@@ -9,7 +9,7 @@ static log_Interceptor* G_interceptor = NULL;
static char **G_keywords = NULL; static char **G_keywords = NULL;
void get_next(char *str, int *next) { static void get_next(char *str, int *next) {
next[1] = 0; next[1] = 0;
int i=1; int i=1;
int j=0; 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 i = 0;
int j = 0; int j = 0;
int substrlen = strlen(substr); int substrlen = strlen(substr);
@@ -73,7 +73,7 @@ static bool _disposeSubstring(char* level,const char *message, ...){
} }
/** /**
* @description : 释放内存 * @description : 完成拦截器自我释放内存
*/ */
static void _freeSubstring(log_Interceptor* interceptor) { static void _freeSubstring(log_Interceptor* interceptor) {
int sum = 0; int sum = 0;
@@ -84,6 +84,10 @@ static void _freeSubstring(log_Interceptor* interceptor) {
free(G_keywords); free(G_keywords);
G_keywords = NULL; G_keywords = NULL;
if(interceptor->handler != NULL){
interceptor->handler->_free(interceptor->handler);
}
free(interceptor); free(interceptor);
} }
@@ -93,7 +97,6 @@ static void _freeSubstring(log_Interceptor* interceptor) {
log_Interceptor* substringInterceptor(char *keywords[], int count, log_level level, log_Handler* handler) { log_Interceptor* substringInterceptor(char *keywords[], int count, log_level level, log_Handler* handler) {
log_Interceptor* interceptor = (log_Interceptor*)malloc(sizeof(log_Interceptor)); log_Interceptor* interceptor = (log_Interceptor*)malloc(sizeof(log_Interceptor));
interceptor->_dispose = _disposeSubstring; interceptor->_dispose = _disposeSubstring;
interceptor->need_free = true;
interceptor->handler = handler; interceptor->handler = handler;
interceptor->level = level; interceptor->level = level;
interceptor->_free = _freeSubstring; interceptor->_free = _freeSubstring;

View File

@@ -40,10 +40,8 @@ static void addHandler(log_Handler* handler){
return; return;
} }
if(G_LOGGER->handler->need_free){ G_LOGGER->handler->_free(G_LOGGER->handler);
G_LOGGER->handler->_free(G_LOGGER->handler);
}
free(G_LOGGER->handler);
G_LOGGER->handler = handler; G_LOGGER->handler = handler;
} }
@@ -56,10 +54,8 @@ void addInterceptor(log_Interceptor* Interceptor){
return; return;
} }
if(G_LOGGER->interceptor->need_free){ G_LOGGER->interceptor->_free(G_LOGGER->interceptor);
G_LOGGER->interceptor->_free(G_LOGGER->interceptor);
}
free(G_LOGGER->interceptor);
G_LOGGER->interceptor = Interceptor; G_LOGGER->interceptor = Interceptor;
} }
@@ -182,6 +178,8 @@ static Logger* getLogger(const char* name, log_level level){
return G_LOGGER; return G_LOGGER;
} }
/** /**
* @description :创建一个日志对象 * @description :创建一个日志对象
* @return :Logging* 返回一个日志对象 * @return :Logging* 返回一个日志对象
@@ -201,12 +199,11 @@ log_status destroyLogging(Logging* logging){
} }
if (G_LOGGER != NULL){ if (G_LOGGER != NULL){
if (G_LOGGER->handler != NULL){ if (G_LOGGER->handler != NULL){
if(G_LOGGER->handler->need_free)G_LOGGER->handler->_free(G_LOGGER->handler); G_LOGGER->handler->_free(G_LOGGER->handler);
free(G_LOGGER->handler);
} }
if (G_LOGGER->interceptor != NULL){ 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); free(G_LOGGER);