Merge pull request 'dev' (#2) from dev into main
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
92
.clang-tidy
Normal file
92
.clang-tidy
Normal file
@@ -0,0 +1,92 @@
|
||||
---
|
||||
Checks: '-*,
|
||||
clang-analyzer-core.*,
|
||||
clang-analyzer-cplusplus.*,
|
||||
modernize-redundant-void-arg,
|
||||
modernize-use-bool-literals,
|
||||
modernize-use-equals-default,
|
||||
modernize-use-nullptr,
|
||||
modernize-use-override,
|
||||
google-explicit-constructor,
|
||||
google-readability-casting,
|
||||
readability-braces-around-statements,
|
||||
readability-identifier-naming.ClassCase,
|
||||
readability-identifier-naming.StructCase,
|
||||
readability-identifier-naming.TypedefCase,
|
||||
readability-identifier-naming.EnumCase,
|
||||
readability-non-const-parameter,
|
||||
cert-dcl21-cpp,
|
||||
bugprone-undelegated-constructor,
|
||||
bugprone-macro-parentheses,
|
||||
bugprone-macro-repeated-side-effects,
|
||||
bugprone-forward-declaration-namespace,
|
||||
bugprone-bool-pointer-implicit-conversion,
|
||||
bugprone-misplaced-widening-cast,
|
||||
cppcoreguidelines-narrowing-conversions,
|
||||
misc-unconventional-assign-operator,
|
||||
misc-unused-parameters'
|
||||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: ''
|
||||
CheckOptions:
|
||||
# 现代化(Modernize)
|
||||
- key: modernize-redundant-void-arg
|
||||
value: 'true' # 检查并移除函数声明中冗余的 void 参数。
|
||||
- key: modernize-use-bool-literals
|
||||
value: 'true' # 建议使用布尔字面量 true 和 false 代替整数值 0 和 1。
|
||||
- key: modernize-use-equals-default
|
||||
value: 'true' # 建议在默认构造函数、复制构造函数和赋值运算符中使用 = default,以简化代码。
|
||||
- key: modernize-use-nullptr
|
||||
value: 'true' # 建议使用 nullptr 代替 NULL 或 0 来表示空指针。
|
||||
- key: modernize-use-override
|
||||
value: 'true' # 建议在覆盖基类虚函数时使用 override 关键字,以增加代码的清晰性和安全性。
|
||||
|
||||
# Google 代码风格(Google)
|
||||
- key: google-explicit-constructor
|
||||
value: 'true' # 检查并建议在单参数构造函数中使用 explicit 关键字,以防止隐式转换。
|
||||
- key: google-readability-casting
|
||||
value: 'true' # 检查并建议使用 C++ 风格的类型转换(如 static_cast、dynamic_cast、const_cast 和 reinterpret_cast)代替 C 风格的类型转换。
|
||||
|
||||
# 可读性(Readability)
|
||||
- key: readability-braces-around-statements
|
||||
value: 'true' # 建议在单行语句周围添加大括号,以提高代码的可读性和一致性。
|
||||
- key: readability-identifier-naming.ClassCase
|
||||
value: 'CamelCase' # 类名应使用 CamelCase 风格,例如 MyClassName。
|
||||
- key: readability-identifier-naming.StructCase
|
||||
value: 'CamelCase' # 结构体名应使用 CamelCase 风格,例如 MyStructName。
|
||||
- key: readability-identifier-naming.TypedefCase
|
||||
value: 'CamelCase' # 类型定义应使用 CamelCase 风格,例如 MyTypeDef。
|
||||
- key: readability-identifier-naming.EnumCase
|
||||
value: 'CamelCase' # 枚举名应使用 CamelCase 风格,例如 MyEnumName。
|
||||
- key: readability-non-const-parameter
|
||||
value: 'true' # 检查并标识非 const 参数,以提高代码的可读性和安全性。
|
||||
|
||||
# CERT 安全编码标准(CERT)
|
||||
- key: cert-dcl21-cpp
|
||||
value: 'true' # 检查并标识在头文件中不应包含无命名空间的 using 声明和指令,以防止命名空间污染。
|
||||
|
||||
# Bug 检测(Bugprone)
|
||||
- key: bugprone-undelegated-constructor
|
||||
value: 'true' # 检查并标识未委托的构造函数,以确保构造函数的正确性。
|
||||
- key: bugprone-macro-parentheses
|
||||
value: 'true' # 检查并建议在宏定义中使用括号,以防止潜在的错误。
|
||||
- key: bugprone-macro-repeated-side-effects
|
||||
value: 'true' # 检查并标识宏中重复的副作用,以防止潜在的错误。
|
||||
- key: bugprone-forward-declaration-namespace
|
||||
value: 'true' # 检查并标识命名空间前向声明的潜在问题。
|
||||
- key: bugprone-bool-pointer-implicit-conversion
|
||||
value: 'true' # 检查并标识布尔指针的隐式转换,以防止潜在的错误。
|
||||
- key: bugprone-misplaced-widening-cast
|
||||
value: 'true' # 检查并标识错误的宽化转换,以防止潜在的错误。
|
||||
|
||||
# C++ 核心指南(CppCoreGuidelines)
|
||||
- key: cppcoreguidelines-narrowing-conversions
|
||||
value: 'true' # 检查并标识可能导致数据丢失的窄化转换。
|
||||
|
||||
# 杂项(Miscellaneous)
|
||||
- key: misc-unconventional-assign-operator
|
||||
value: 'true' # 检查并标识不常见的赋值操作符重载,以确保代码的一致性和可维护性。
|
||||
- key: misc-unused-parameters
|
||||
value: 'true' # 检测未使用的参数。
|
||||
...
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.28...3.30)
|
||||
project(logging)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_CLANG_TIDY "clang-tidy")
|
||||
|
||||
if(MSVC)
|
||||
add_compile_options("/source-charset:utf-8")
|
||||
|
||||
@@ -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