@@ -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;
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "logging.h"
|
||||
|
||||
|
||||
int main() {
|
||||
Logging *log = createLogging();
|
||||
Logger *logger = log->getLogger("testLogger",LOG_DEBUG);
|
||||
|
||||
Reference in New Issue
Block a user