fix: 优化代码格式和类型安全性
This commit is contained in:
@@ -24,15 +24,17 @@ static void get_next(char *str, int *next) {
|
||||
}
|
||||
|
||||
static bool kmp_search(char *substr, char *master) {
|
||||
if (substr == NULL)
|
||||
if (substr == NULL) {
|
||||
return true; // 空串全匹配
|
||||
if (master == NULL)
|
||||
}
|
||||
if (master == NULL) {
|
||||
return false;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int substrlen = strlen(substr);
|
||||
int masterlen = strlen(master);
|
||||
int *next = (int *)malloc(sizeof(int) * (substrlen + 1));
|
||||
}
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
size_t substrlen = strlen(substr);
|
||||
size_t masterlen = strlen(master);
|
||||
int *next = (int *)malloc(sizeof(int) * (substrlen + 1));
|
||||
get_next(substr, next);
|
||||
|
||||
while (i < masterlen && j < substrlen) {
|
||||
@@ -49,10 +51,11 @@ static bool kmp_search(char *substr, char *master) {
|
||||
}
|
||||
|
||||
free(next);
|
||||
if (j == substrlen)
|
||||
if (j == substrlen) {
|
||||
return true;
|
||||
else
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool _disposeSubstring(log_filter *filter,
|
||||
@@ -63,14 +66,16 @@ static bool _disposeSubstring(log_filter *filter,
|
||||
keywords_t *keyword = (keywords_t *)(filter + 1);
|
||||
|
||||
if (keyword->key == NULL && keyword->next == NULL) {
|
||||
if (level <= filter->level)
|
||||
if (level <= filter->level) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
while (keyword != NULL && level <= filter->level) {
|
||||
if (kmp_search(keyword->key, (char *)message))
|
||||
if (kmp_search(keyword->key, (char *)message)) {
|
||||
return true;
|
||||
}
|
||||
keyword = keyword->next;
|
||||
}
|
||||
|
||||
@@ -94,8 +99,9 @@ static void _freeFilter(log_filter *filter) {
|
||||
filter->handler->_free(filter->handler);
|
||||
}
|
||||
|
||||
if (it_keyword->key != NULL)
|
||||
if (it_keyword->key != NULL) {
|
||||
free(it_keyword->key);
|
||||
}
|
||||
free(filter);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
// log_Handler_file_ex_t与log_Handler处于连续内存中
|
||||
// 使用char*指针进行偏移,达到以偏移1个字节为单位的偏移
|
||||
#define Handler_file_EX_PRT(handler) \
|
||||
((log_Handler_file_ex_t *)((char *)handler + sizeof(log_Handler)))
|
||||
((log_Handler_file_ex_t *)((char *)(handler) + sizeof(log_Handler)))
|
||||
|
||||
#define FILE_NAME_MAX_SIZE 50
|
||||
|
||||
@@ -46,9 +46,10 @@ static void outputFileHandler(log_Handler *handler, const char *message) {
|
||||
fputs(message, handler->stream);
|
||||
log_Handler_file_ex_t *handler_ex = Handler_file_EX_PRT(handler);
|
||||
handler_ex->file_size += strlen(message);
|
||||
if (handler_ex->file_size > handler_ex->file_size_max)
|
||||
if (handler_ex->file_size > handler_ex->file_size_max) {
|
||||
changeFile(handler);
|
||||
}
|
||||
}
|
||||
|
||||
log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) {
|
||||
char new_file_name[FILE_NAME_MAX_SIZE];
|
||||
@@ -62,24 +63,27 @@ log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) {
|
||||
do {
|
||||
sprintf(new_file_name, "%s_%d.log", file_name, suffix++);
|
||||
fp = fopen(new_file_name, "at");
|
||||
if (fp == NULL)
|
||||
if (fp == NULL) {
|
||||
goto ERROR;
|
||||
}
|
||||
file_size = getFileSize(fp);
|
||||
} while (file_size > max_size);
|
||||
|
||||
/// 分配log_Handler与记录文件大小的空间
|
||||
handler = (log_Handler *)malloc(sizeof(log_Handler) +
|
||||
sizeof(log_Handler_file_ex_t));
|
||||
if (handler == NULL)
|
||||
if (handler == NULL) {
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
handler_ex = Handler_file_EX_PRT(handler);
|
||||
handler_ex->file_size_max = max_size;
|
||||
handler_ex->file_size = file_size;
|
||||
handler_ex->suffix = suffix;
|
||||
handler_ex->file_name = strdup(file_name);
|
||||
if (handler_ex->file_name == NULL)
|
||||
if (handler_ex->file_name == NULL) {
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
handler->stream = fp;
|
||||
handler->apply_color = false;
|
||||
@@ -88,8 +92,9 @@ log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) {
|
||||
return handler;
|
||||
|
||||
ERROR:
|
||||
if (fp)
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
if (handler) {
|
||||
free(Handler_file_EX_PRT(handler)->file_name); // 直接释放,无需检查NULL
|
||||
free(handler);
|
||||
|
||||
@@ -87,7 +87,7 @@ static void output_to_handler(log_Handler *handler,
|
||||
char timeStr[20];
|
||||
getTimeStr(timeStr);
|
||||
char logStr[LOG_BUFFER_SIZE * 2];
|
||||
if (handler->apply_color)
|
||||
if (handler->apply_color) {
|
||||
snprintf(logStr,
|
||||
LOG_BUFFER_SIZE * 2,
|
||||
"[%s]: %s %s%s%s %s\n",
|
||||
@@ -97,7 +97,7 @@ static void output_to_handler(log_Handler *handler,
|
||||
level,
|
||||
RESET,
|
||||
message);
|
||||
else
|
||||
} else {
|
||||
snprintf(logStr,
|
||||
LOG_BUFFER_SIZE * 2,
|
||||
"[%s]: %s %s %s\n",
|
||||
@@ -105,6 +105,7 @@ static void output_to_handler(log_Handler *handler,
|
||||
timeStr,
|
||||
level,
|
||||
message);
|
||||
}
|
||||
|
||||
handler->output(handler, logStr);
|
||||
}
|
||||
@@ -134,8 +135,9 @@ static void log_cope(log_level level_e,
|
||||
while (it != NULL) {
|
||||
if (it->_dispose(it, level_e, message)) {
|
||||
output_to_handler(it->handler, level, color, message);
|
||||
if (it->jump_out)
|
||||
if (it->jump_out) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user