完成拦截器设计

This commit is contained in:
2024-08-13 22:23:34 +08:00
parent a823bab944
commit 2608dfa078
11 changed files with 298 additions and 32 deletions

View File

@@ -5,6 +5,15 @@
#include "logging.h"
/**
* @description :释放文件日志处理器相关资源
* @param
* @return
*/
static void __freeFileHandler(log_Handler* handler){
fclose(handler->out);
}
/**
* @description :文件日志处理器
* @param
@@ -17,6 +26,8 @@ log_Handler* fileHandler(const char* name){
log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler));
handler->out = fp;
handler->apply_color = false;
handler->need_free = true;
handler->_free = __freeFileHandler;
return handler;
}
@@ -30,5 +41,7 @@ log_Handler* consoleHandler(const char* name){
log_Handler* handler = (log_Handler*)malloc(sizeof(log_Handler));
handler->out = stdout;
handler->apply_color = true;
handler->need_free = false;
handler->_free = NULL;
return handler;
}

114
src/logging-interceptor.c Normal file
View File

@@ -0,0 +1,114 @@
/********************************************
* @Date: 2024 08 12
* @Description: 日志拦截器
*********************************************/
#include "logging.h"
static log_Interceptor* G_interceptor = NULL;
static char **G_keywords = NULL;
void get_next(char *str, int *next) {
next[1] = 0;
int i=1;
int j=0;
while (i < strlen(str)) {
if (j==0|| str[i] == str[j]) {
next[++i] = ++j;
}
else {
j = next[j];
}
}
}
bool kmp_search(char *substr, char *master) {
int i = 0;
int j = 0;
int substrlen = strlen(substr);
int masterlen = strlen(master);
int *next = (int *)malloc(sizeof(int) * substrlen + 1);
get_next(substr, next);
while (i < masterlen && j < substrlen) {
if (master[i] == substr[j]) {
i++;
j++;
} else {
if(j == 0) {
i++;
} else {
j = next[j];
}
}
}
free(next);
if (j == substrlen)return true;
else return false;
}
static bool _disposeSubstring(char* level,const char *message, ...){
int count = 0;
if (G_keywords == NULL) {
return false;
}
while (G_keywords[count] != NULL)
{
if (kmp_search(G_keywords[count],(char*)message)) {
if(G_interceptor->handler != NULL) {
}
return true;
}
count++;
}
return false;
}
/**
* @description : 释放内存
*/
static void _freeSubstring(log_Interceptor* interceptor) {
int sum = 0;
while (G_keywords[sum] != NULL) {
free(G_keywords[sum]);
sum++;
}
free(G_keywords);
G_keywords = NULL;
free(interceptor);
}
/**
* @description : 子字符串拦截器
*/
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;
G_keywords = (char**)malloc( (sizeof(G_keywords)*count) + 1);
for (int i = 0; i < count; i++) {
G_keywords[i] = (char*)malloc(strlen(keywords[i])+1);
strcpy(G_keywords[i], keywords[i]);
}
G_keywords[count] = NULL;
G_interceptor = interceptor;
return G_interceptor;
}

View File

@@ -35,13 +35,38 @@ static void addHandler(log_Handler* handler){
if (G_LOGGER == NULL){
return;
}
if (G_LOGGER->handler == NULL){
G_LOGGER->handler = handler;
return;
}
if(G_LOGGER->handler->need_free){
G_LOGGER->handler->_free(G_LOGGER->handler);
}
free(G_LOGGER->handler);
G_LOGGER->handler = handler;
}
void addInterceptor(log_Interceptor* Interceptor){
if (G_LOGGER == NULL){
return;
}
if (G_LOGGER->interceptor == NULL){
G_LOGGER->interceptor = Interceptor;
return;
}
if(G_LOGGER->interceptor->need_free){
G_LOGGER->interceptor->_free(G_LOGGER->interceptor);
}
free(G_LOGGER->interceptor);
G_LOGGER->interceptor = Interceptor;
}
/**
* @description : 内置日志记录函数
*/
static void _builtin_log(char* level, const char *color, const char* format, ...){
static void _builtin_log(char* level, const char *color, const char* message, ...){
if (G_LOGGER == NULL){
return;
}
@@ -51,63 +76,77 @@ static void _builtin_log(char* level, const char *color, const char* format, ...
char timeStr[20];
getTimeStr(timeStr);
char logStr[LOG_BUFFER_SIZE];
if (G_LOGGER->handler->apply_color) sprintf(logStr, "%s %s%s%s %s\n", timeStr, color,level,RESET, format);
else sprintf(logStr, "%s %s %s\n", timeStr, level, format);
fputs(logStr, G_LOGGER->handler->out);
log_Handler * handler = G_LOGGER->handler;
//通过拦截器
if (G_LOGGER->interceptor != NULL){
if(G_LOGGER->interceptor->_dispose(level,message)){
if(G_LOGGER->interceptor->handler != NULL){
handler = G_LOGGER->interceptor->handler;
}
}
}
//判断处理器是否应用颜色
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);
}
//*************************记录日志******************************* */
static void fatal(const char* format, ...){
static void fatal(const char* message, ...){
if (G_LOGGER->level >= LOG_ERROR){
char logStr[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vsprintf(logStr, format, args);
va_start(args, message);
vsprintf(logStr, message, args);
va_end(args);
_builtin_log("Fatal",RED, logStr, args);
}
}
static void error(const char* format, ...){
static void error(const char* message, ...){
if (G_LOGGER->level >= LOG_ERROR){
char logStr[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vsprintf(logStr, format, args);
va_start(args, message);
vsprintf(logStr, message, args);
va_end(args);
_builtin_log("Error",RED, logStr, args);
}
}
static void warning(const char* format, ...){
static void warning(const char* message, ...){
if (G_LOGGER->level >= LOG_WARNING){
char logStr[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vsprintf(logStr, format, args);
va_start(args, message);
vsprintf(logStr, message, args);
va_end(args);
_builtin_log("Warning",YELLOW, logStr, args);
}
}
static void info(const char* format, ...){
static void info(const char* message, ...){
if (G_LOGGER->level >= LOG_INFO){
char logStr[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vsprintf(logStr, format, args);
va_start(args, message);
vsprintf(logStr, message, args);
va_end(args);
_builtin_log("Info",GREEN, logStr, args);
}
}
static void debug(const char* format, ...){
static void debug(const char* message, ...){
if (G_LOGGER->level >= LOG_DEBUG){
char logStr[LOG_BUFFER_SIZE];
va_list args;
va_start(args, format);
vsprintf(logStr, format, args);
va_start(args, message);
vsprintf(logStr, message, args);
va_end(args);
_builtin_log("Debug",CYAN, logStr, args);
}
@@ -132,10 +171,12 @@ static Logger* getLogger(const char* name, log_level level){
logger->debug = debug;
logger->addHandler = addHandler;
logger->addInterceptor = addInterceptor;
logger->level = level;
logger->handler = NULL;
logger->name = name;
logger->interceptor = NULL;
G_LOGGER = logger;
return G_LOGGER;
@@ -160,9 +201,14 @@ log_status destroyLogging(Logging* logging){
}
if (G_LOGGER != NULL){
if (G_LOGGER->handler != NULL){
fclose(G_LOGGER->handler->out);
if(G_LOGGER->handler->need_free)G_LOGGER->handler->_free(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);
}
free(G_LOGGER);
G_LOGGER = NULL;
}
@@ -180,4 +226,6 @@ Logger* getCurrentLogger(void){
return NULL;
}
return G_LOGGER;
}
}