diff --git a/.github/workflows/linux_test.yml b/.github/workflows/linux_test.yml index 88483ca..5a997eb 100644 --- a/.github/workflows/linux_test.yml +++ b/.github/workflows/linux_test.yml @@ -1,4 +1,3 @@ - name: test on Linux on: @@ -14,4 +13,4 @@ jobs: - name: checkout code uses: actions/checkout@v4 - name: test - run: bash ./test.sh \ No newline at end of file + run: bash ./script/test.sh \ No newline at end of file diff --git a/.github/workflows/windows_test.yml b/.github/workflows/windows_test.yml new file mode 100644 index 0000000..ad6fc79 --- /dev/null +++ b/.github/workflows/windows_test.yml @@ -0,0 +1,16 @@ +name: test on Windows + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + +jobs: + test: + runs-on: windows-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + - name: test + run: ./script/test_windows.ps1 \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index dc18cd3..f390aec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,12 @@ -cmake_minimum_required( VERSION 3.28) +cmake_minimum_required(VERSION 3.28...3.30) + +set(CMAKE_EXPORT_COMPILE_COMMANDS yes) + project(logging) option(TEST "是否启动单元测试" ON) option(SHARED "是否编译为动态库" OFF) -set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) -set(ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) -set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) - include_directories(${CMAKE_SOURCE_DIR}/include) #编译库文件 diff --git a/include/logging.h b/include/logging.h index 7f2d8c8..387b071 100644 --- a/include/logging.h +++ b/include/logging.h @@ -8,7 +8,7 @@ #include "logging/logging-handler.h" #include "logging/logging-interceptor.h" -// 日志操作器 + typedef struct Logger { log_level level; log_Handler *handler; @@ -24,7 +24,7 @@ typedef struct Logger { void (*addInterceptor)(log_Interceptor *Interceptor); } Logger; -// 日志类对象 + typedef struct Logging { Logger *(*getLogger)(const char *name, log_level level); log_status (*setLevel)(Logger *logger, log_level level); @@ -32,6 +32,6 @@ typedef struct Logging { log_status (*destroyLogging)(struct Logging *logging); } Logging; -Logging *newLogging(); // 创建日志类对象 +Logging *newLogging(); #endif // __LOGGING_H__ \ No newline at end of file diff --git a/include/logging/logging-core.h b/include/logging/logging-core.h index 3f8008c..25e8f6a 100644 --- a/include/logging/logging-core.h +++ b/include/logging/logging-core.h @@ -14,5 +14,4 @@ typedef enum { L_OK, } log_status; - #endif // __LOGGING_CORE_H__ diff --git a/include/logging/logging-handler.h b/include/logging/logging-handler.h index 06c1dd7..9254266 100644 --- a/include/logging/logging-handler.h +++ b/include/logging/logging-handler.h @@ -4,14 +4,13 @@ #include typedef struct log_Handler { - void* stream; - bool apply_color; - void (*_free)(struct log_Handler* handler);//释放资源 - void (*output)(struct log_Handler* handler,const char* message); + void *stream; + bool apply_color; + void (*_free)(struct log_Handler *handler); + void (*output)(struct log_Handler *handler, const char *message); } log_Handler; +log_Handler *loggingFileHandler(const char *name); +log_Handler *loggingConsoleHandler(); -log_Handler* loggingFileHandler(const char* name); -log_Handler* loggingConsoleHandler(); - -#endif //__LOGGING_HANDLER_H__ \ No newline at end of file +#endif //__LOGGING_HANDLER_H__ \ No newline at end of file diff --git a/include/logging/logging-interceptor.h b/include/logging/logging-interceptor.h index a8f2fd6..a365027 100644 --- a/include/logging/logging-interceptor.h +++ b/include/logging/logging-interceptor.h @@ -5,10 +5,10 @@ #include "logging-handler.h" typedef struct log_Interceptor { - log_level level; // 拦截级别 - log_Handler *handler; // 拦截目标处理器 - bool (*_dispose)(char *level, const char *message, ...); // 拦截触发器 - void (*_free)(struct log_Interceptor *Interceptor); // 释放资源 + log_level level; + log_Handler *handler; + bool (*_dispose)(char *level, const char *message, ...); + void (*_free)(struct log_Interceptor *Interceptor); } log_Interceptor; diff --git a/script/test.sh b/script/test.sh new file mode 100755 index 0000000..7b6a526 --- /dev/null +++ b/script/test.sh @@ -0,0 +1,2 @@ +#!/bin/bash +cmake build -B build . && cd build && cmake --build . && ctest \ No newline at end of file diff --git a/script/test_windows.ps1 b/script/test_windows.ps1 new file mode 100644 index 0000000..f5c07c7 --- /dev/null +++ b/script/test_windows.ps1 @@ -0,0 +1,5 @@ +cmake build -B build . +Set-Location -Path "./build" +cmake --build . +ctest +Set-Location -Path ".." diff --git a/src/handler/logging-handler-console.c b/src/handler/logging-handler-console.c index f8e9482..a71c99d 100644 --- a/src/handler/logging-handler-console.c +++ b/src/handler/logging-handler-console.c @@ -1,29 +1,13 @@ -/******************************************** - * @Date: 2024 09 18 - * @Description: 控制台日志处理器 - ********************************************/ #include "logging/logging-handler.h" #include #include -/** - * @brief 释放组件 - * @param handler 处理器 - */ static void __freeConsoleHandler(log_Handler *handler) { free(handler); } -/** - * @brief 输出组件 - * @param handler 处理器 - * @param message 消息 - */ static void outputConsoleHandler(log_Handler *handler, const char *message) { fputs(message, handler->stream); } -/** - * @brief :控制台日志处理器 - */ log_Handler *loggingConsoleHandler() { log_Handler *handler = (log_Handler *)malloc(sizeof(log_Handler)); diff --git a/src/handler/logging-handler-file.c b/src/handler/logging-handler-file.c index 58b1d51..b2bbc2a 100644 --- a/src/handler/logging-handler-file.c +++ b/src/handler/logging-handler-file.c @@ -1,33 +1,16 @@ -/******************************************** - * @Date: 2024 09 18 - * @Description: 文件日志处理器 - ********************************************/ #include "logging/logging-handler.h" #include #include -/** - * @brief 文件日志处理器释放组件 - */ static void __freeFileHandler(log_Handler *handler) { fclose(handler->stream); free(handler); } -/** - * @brief 文件日志处理器输出组件 - * @param handler 文件日志处理器 - * @param message 消息 - */ static void outputFileHandler(log_Handler *handler, const char *message) { fputs(message, handler->stream); } -/** - * @brief 文件日志处理器 - * @param name 文件名 - * @return log_Handler * - */ log_Handler *loggingFileHandler(const char *name) { char new_file_name[100]; sprintf(new_file_name, "%s.log", name); diff --git a/src/interceptor/logging-interceptor-substr.c b/src/interceptor/logging-interceptor-substr.c index c7deb19..f21dc10 100644 --- a/src/interceptor/logging-interceptor-substr.c +++ b/src/interceptor/logging-interceptor-substr.c @@ -1,8 +1,3 @@ -/******************************************** - * @Date: 2024 08 12 - * @Description: 子串拦截器 - *********************************************/ - #include "logging/logging-interceptor.h" #include #include @@ -31,7 +26,7 @@ static bool kmp_search(char *substr, char *master) { int j = 0; int substrlen = strlen(substr); int masterlen = strlen(master); - int *next = (int *)malloc(sizeof(int) * substrlen + 1); + int *next = (int *)malloc(sizeof(int) * (substrlen + 1)); get_next(substr, next); while (i < masterlen && j < substrlen) { @@ -54,11 +49,6 @@ static bool kmp_search(char *substr, char *master) { return false; } -/** - * @description 处理 - * @param - * @return - */ static bool _disposeSubstring(char *level, const char *message, ...) { int count = 0; @@ -76,28 +66,26 @@ static bool _disposeSubstring(char *level, const char *message, ...) { return false; } -/** - * @description : 完成拦截器自我释放内存 - */ static void _freeSubstring(log_Interceptor *interceptor) { - int sum = 0; - while (G_keywords[sum] != NULL) { + if (G_keywords != NULL) { + int sum = 0; + while (G_keywords[sum] != NULL) { + free(G_keywords[sum]); + sum++; + } free(G_keywords[sum]); - sum++; + free(G_keywords); + G_keywords = NULL; } - free(G_keywords); - G_keywords = NULL; if (interceptor->handler != NULL) { interceptor->handler->_free(interceptor->handler); } - free(interceptor); + if (interceptor != NULL) + free(interceptor); } -/** - * @description : 子字符串拦截器 - */ log_Interceptor *loggingSubStringInterceptor(char *keywords[], int count, log_level level, @@ -109,7 +97,7 @@ log_Interceptor *loggingSubStringInterceptor(char *keywords[], interceptor->level = level; interceptor->_free = _freeSubstring; - G_keywords = (char **)malloc((sizeof(G_keywords) * count) + 1); + 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); diff --git a/src/logging.c b/src/logging.c index 471c8a2..6b7c455 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1,7 +1,3 @@ -/******************************************** - * @Date: 2024 08 12 - * @Description: 日志模块 - ********************************************/ #include "logging.h" #include "logging/logging-handler.h" #include @@ -9,7 +5,6 @@ #include #include - #define RED "\033[0;31m" #define RED_B "\033[0;41m" #define GREEN "\033[0;32m" @@ -18,7 +13,6 @@ #define RESET "\033[0m" #define CYAN "\033[0;36m" - #define LOG_BUFFER_SIZE 1024 Logger *G_LOGGER = NULL; @@ -31,9 +25,6 @@ static void getTimeStr(char *timeStr) { strcpy(timeStr, _timeStr); } -/** - * @description : 添加日志处理器 - */ static void addHandler(log_Handler *handler) { if (G_LOGGER == NULL) { return; @@ -48,11 +39,6 @@ static void addHandler(log_Handler *handler) { G_LOGGER->handler = handler; } -/** - * @description : 添加日志拦截器 - * @param - * @return - */ void addInterceptor(log_Interceptor *Interceptor) { if (G_LOGGER == NULL) { return; @@ -67,9 +53,6 @@ void addInterceptor(log_Interceptor *Interceptor) { G_LOGGER->interceptor = Interceptor; } -/** - * @description : 内置日志记录函数 - */ static void _builtin_log(char *level, const char *color, const char *message, ...) { if (G_LOGGER == NULL) { @@ -84,7 +67,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { log_Handler *handler = G_LOGGER->handler; - // 通过拦截器 if (G_LOGGER->interceptor != NULL) { if (G_LOGGER->interceptor->_dispose(level, message)) { if (G_LOGGER->interceptor->handler != NULL) { @@ -93,7 +75,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { } } - // 判断处理器是否应用颜色 if (handler->apply_color) sprintf(logStr, "%s: %s %s%s%s %s\n", @@ -110,7 +91,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { handler->output(handler, logStr); } -//*************************记录日志******************************* */ static void fatal(const char *message, ...) { if (G_LOGGER->level >= LOG_ERROR) { char logStr[LOG_BUFFER_SIZE]; @@ -165,13 +145,7 @@ static void debug(const char *message, ...) { _builtin_log("Debug", CYAN, logStr, args); } } -//*************************记录日志******************************* */ -/** - * @description :获取一个日志操作对象 - * @param - * @return - */ static Logger *getLogger(const char *name, log_level level) { if (G_LOGGER != NULL) { G_LOGGER->name = name; @@ -180,7 +154,7 @@ static Logger *getLogger(const char *name, log_level level) { } Logger *logger = (Logger *)malloc(sizeof(Logger)); - // 方法 + logger->fatal = fatal; logger->error = error; logger->warning = warning; @@ -190,7 +164,6 @@ static Logger *getLogger(const char *name, log_level level) { logger->addHandler = addHandler; logger->addInterceptor = addInterceptor; - // 属性 logger->level = level; logger->handler = loggingConsoleHandler(); logger->name = name; @@ -223,10 +196,6 @@ log_status destroyLogging(Logging *logging) { return L_OK; } -/** - * @description :获取当前日志操作对象 - * @return 当前唯一的日志操作对象 - */ Logger *getCurrentLogger(void) { if (G_LOGGER == NULL) { return NULL; @@ -234,10 +203,6 @@ Logger *getCurrentLogger(void) { return G_LOGGER; } -/** - * @description :创建一个日志对象 - * @return :Logging* 返回一个日志对象 - */ Logging *newLogging() { Logging *logging = (Logging *)malloc(sizeof(Logging)); logging->getLogger = getLogger; diff --git a/test.sh b/test.sh deleted file mode 100755 index e372a43..0000000 --- a/test.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -cmake build -B build . && cd build && make && ctest \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 74e1515..c226573 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,10 +5,17 @@ enable_testing() #测试简单基本应用 add_executable(${PROJECT_NAME}simple test_simple.c) target_link_libraries(${PROJECT_NAME}simple logging) -add_test(test_simple ${CMAKE_SOURCE_DIR}/bin/${PROJECT_NAME}simple) - +if(UNIX) + add_test(test_simple ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}simple) +elseif(WIN32) + add_test(test_simple ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}simple.exe) +endif() #测试拦截器 add_executable(${PROJECT_NAME}interceptor test_interceptor.c) target_link_libraries(${PROJECT_NAME}interceptor logging) -add_test(test_interceptor ${CMAKE_SOURCE_DIR}/bin/${PROJECT_NAME}interceptor) \ No newline at end of file +if(UNIX) + add_test(test_interceptor ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}interceptor) +elseif(WIN32) + add_test(test_interceptor ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}interceptor.exe) +endif() \ No newline at end of file diff --git a/tests/test_interceptor.c b/tests/test_interceptor.c index 8ba1541..08889b3 100644 --- a/tests/test_interceptor.c +++ b/tests/test_interceptor.c @@ -6,15 +6,15 @@ int main() { Logger *logger = log->getLogger("testLogger", LOG_DEBUG); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123"); - char *test1[] = {"123", "你好"}; // 要拦截的字符串 - // 添加拦截器,将拦截到的日志重定向到拦截器的专属处理器中 + char *test1[] = {"123", "tt"}; + log_Interceptor *tint = - loggingSubStringInterceptor(test1, 2, LOG_DEBUG, loggingFileHandler("被拦截")); + loggingSubStringInterceptor(test1, 2, LOG_DEBUG, loggingFileHandler("test_interceptor")); logger->addInterceptor(tint); @@ -23,7 +23,7 @@ int main() { printf("\n"); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123"); diff --git a/tests/test_simple.c b/tests/test_simple.c index 71628ba..9abc796 100644 --- a/tests/test_simple.c +++ b/tests/test_simple.c @@ -5,7 +5,7 @@ int main() { Logger *logger = log->getLogger("testLogger", LOG_DEBUG); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123");