Multiple substring interceptors
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static log_Interceptor *G_interceptor = NULL;
|
||||
|
||||
static char **G_keywords = NULL;
|
||||
typedef struct keywords_s {
|
||||
char *key;
|
||||
struct keywords_s *next;
|
||||
} keywords_t;
|
||||
|
||||
static void get_next(char *str, int *next) {
|
||||
next[1] = 0;
|
||||
@@ -22,6 +24,8 @@ static void get_next(char *str, int *next) {
|
||||
}
|
||||
|
||||
static bool kmp_search(char *substr, char *master) {
|
||||
if(substr == NULL)return true; //空串全匹配
|
||||
if(master == NULL)return false;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int substrlen = strlen(substr);
|
||||
@@ -49,62 +53,75 @@ static bool kmp_search(char *substr, char *master) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool _disposeSubstring(char *level, const char *message, ...) {
|
||||
int count = 0;
|
||||
static bool _disposeSubstring(log_Interceptor *interceptor,
|
||||
char *level,
|
||||
const char *message,
|
||||
...) {
|
||||
int count = 0;
|
||||
keywords_t *keyword = (keywords_t *)(interceptor + 1);
|
||||
|
||||
if (G_keywords == NULL) {
|
||||
if (keyword->key == NULL && keyword->next == NULL)
|
||||
return false;
|
||||
}
|
||||
|
||||
while (G_keywords[count] != NULL) {
|
||||
if (kmp_search(G_keywords[count], (char *)message)) {
|
||||
while (keyword != NULL) {
|
||||
if (kmp_search(keyword->key, (char *)message))
|
||||
return true;
|
||||
}
|
||||
count++;
|
||||
keyword = keyword->next;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void _freeSubstring(log_Interceptor *interceptor) {
|
||||
if (G_keywords != NULL) {
|
||||
int sum = 0;
|
||||
while (G_keywords[sum] != NULL) {
|
||||
free(G_keywords[sum]);
|
||||
sum++;
|
||||
}
|
||||
free(G_keywords[sum]);
|
||||
free(G_keywords);
|
||||
G_keywords = NULL;
|
||||
keywords_t *it_keyword =
|
||||
(keywords_t *)(interceptor + 1); // it_keyword 不是起始地址,请勿free
|
||||
keywords_t *keyword = it_keyword->next;
|
||||
keywords_t *next = NULL;
|
||||
|
||||
while (keyword != NULL) {
|
||||
next = keyword->next;
|
||||
free(keyword->key);
|
||||
free(keyword);
|
||||
keyword = next;
|
||||
}
|
||||
|
||||
if (interceptor->handler != NULL) {
|
||||
interceptor->handler->_free(interceptor->handler);
|
||||
}
|
||||
|
||||
if (interceptor != NULL)
|
||||
free(interceptor);
|
||||
if (it_keyword->key != NULL)
|
||||
free(it_keyword->key);
|
||||
free(interceptor);
|
||||
}
|
||||
|
||||
log_Interceptor *loggingSubStringInterceptor(char *keywords[],
|
||||
int count,
|
||||
log_level level,
|
||||
log_Handler *handler) {
|
||||
log_Handler *handler,
|
||||
bool jump_out) {
|
||||
log_Interceptor *interceptor =
|
||||
(log_Interceptor *)malloc(sizeof(log_Interceptor));
|
||||
(log_Interceptor *)malloc(sizeof(log_Interceptor) + sizeof(keywords_t));
|
||||
interceptor->_dispose = _disposeSubstring;
|
||||
interceptor->handler = handler;
|
||||
interceptor->level = level;
|
||||
interceptor->jump_out = jump_out;
|
||||
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]);
|
||||
keywords_t *keyword = (keywords_t *)(interceptor + 1);
|
||||
keyword->key = NULL;
|
||||
int count = 0;
|
||||
if (keywords[count] != NULL) {
|
||||
keyword->key = strdup(keywords[count]);
|
||||
count++;
|
||||
keyword->next = NULL;
|
||||
}
|
||||
G_keywords[count] = NULL;
|
||||
|
||||
G_interceptor = interceptor;
|
||||
return G_interceptor;
|
||||
while (keywords[count] != NULL) {
|
||||
keyword->next = (keywords_t *)malloc(sizeof(keywords_t));
|
||||
keyword = keyword->next;
|
||||
keyword->key = strdup(keywords[count]);
|
||||
count++;
|
||||
}
|
||||
keyword->next = NULL;
|
||||
|
||||
return interceptor;
|
||||
}
|
||||
@@ -42,45 +42,28 @@ static bool addInterceptor(log_Interceptor *Interceptor) {
|
||||
return false;
|
||||
}
|
||||
if (G_LOGGER->interceptor == NULL) {
|
||||
G_LOGGER->interceptor = Interceptor;
|
||||
G_LOGGER->interceptor = Interceptor;
|
||||
G_LOGGER->interceptor->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
G_LOGGER->interceptor->_free(G_LOGGER->interceptor);
|
||||
G_LOGGER->interceptor = Interceptor;
|
||||
log_Interceptor *it = G_LOGGER->interceptor;
|
||||
while (it->next != NULL) {
|
||||
it = it->next;
|
||||
}
|
||||
|
||||
it->next = Interceptor;
|
||||
Interceptor->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 内部日志打印处理核心函数
|
||||
* @param level 日志等级
|
||||
* @param color 应用的颜色
|
||||
* @param message 日志内容
|
||||
* @param ... 格式化参数列表
|
||||
* @return
|
||||
*/
|
||||
static void
|
||||
_builtin_log(char *level, const char *color, const char *message, ...) {
|
||||
if (G_LOGGER == NULL) {
|
||||
return;
|
||||
}
|
||||
if (G_LOGGER->handler == NULL) {
|
||||
return;
|
||||
}
|
||||
static void output_to_handler(log_Handler *handler,
|
||||
char *level,
|
||||
const char *color,
|
||||
const char *message) {
|
||||
char timeStr[20];
|
||||
getTimeStr(timeStr);
|
||||
char logStr[LOG_BUFFER_SIZE];
|
||||
|
||||
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 %s\n",
|
||||
@@ -101,6 +84,36 @@ _builtin_log(char *level, const char *color, const char *message, ...) {
|
||||
handler->output(handler, logStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 内部日志打印处理核心函数
|
||||
* @param level 日志等级
|
||||
* @param color 应用的颜色
|
||||
* @param message 日志内容
|
||||
* @param ... 格式化参数列表
|
||||
* @return
|
||||
*/
|
||||
static void _builtin_cope(char *level, const char *color, const char *message) {
|
||||
if (G_LOGGER == NULL) {
|
||||
return;
|
||||
}
|
||||
if (G_LOGGER->handler == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
log_Interceptor *it = G_LOGGER->interceptor;
|
||||
log_Handler *handler = G_LOGGER->handler;
|
||||
|
||||
while (it != NULL) {
|
||||
if (it->_dispose(it, level, message)) {
|
||||
output_to_handler(it->handler, level, color, message);
|
||||
if (it->jump_out)
|
||||
return;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
output_to_handler(handler, level, color, message);
|
||||
}
|
||||
|
||||
void log_fatal(const char *message, ...) {
|
||||
if (G_LOGGER->level >= LOG_ERROR) {
|
||||
char logStr[LOG_BUFFER_SIZE];
|
||||
@@ -108,7 +121,7 @@ void log_fatal(const char *message, ...) {
|
||||
va_start(args, message);
|
||||
vsprintf(logStr, message, args);
|
||||
va_end(args);
|
||||
_builtin_log("Fatal", RED_B, logStr, args);
|
||||
_builtin_cope("Fatal", RED_B, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +132,7 @@ void log_error(const char *message, ...) {
|
||||
va_start(args, message);
|
||||
vsprintf(logStr, message, args);
|
||||
va_end(args);
|
||||
_builtin_log("Error", RED, logStr, args);
|
||||
_builtin_cope("Error", RED, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +143,7 @@ void log_warning(const char *message, ...) {
|
||||
va_start(args, message);
|
||||
vsprintf(logStr, message, args);
|
||||
va_end(args);
|
||||
_builtin_log("Warning", YELLOW, logStr, args);
|
||||
_builtin_cope("Warning", YELLOW, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +154,7 @@ void log_info(const char *message, ...) {
|
||||
va_start(args, message);
|
||||
vsprintf(logStr, message, args);
|
||||
va_end(args);
|
||||
_builtin_log("Info", GREEN, logStr, args);
|
||||
_builtin_cope("Info", GREEN, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +165,7 @@ void log_debug(const char *message, ...) {
|
||||
va_start(args, message);
|
||||
vsprintf(logStr, message, args);
|
||||
va_end(args);
|
||||
_builtin_log("Debug", CYAN, logStr, args);
|
||||
_builtin_cope("Debug", CYAN, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +200,13 @@ log_status destroyDefaultLogger(void) {
|
||||
}
|
||||
|
||||
if (G_LOGGER->interceptor != NULL) {
|
||||
G_LOGGER->interceptor->_free(G_LOGGER->interceptor);
|
||||
log_Interceptor *it = G_LOGGER->interceptor;
|
||||
log_Interceptor *next = NULL;
|
||||
while (it != NULL) {
|
||||
next = it->next;
|
||||
it->_free(it);
|
||||
it = next;
|
||||
}
|
||||
}
|
||||
|
||||
free(G_LOGGER);
|
||||
|
||||
Reference in New Issue
Block a user