diff --git a/CMakeLists.txt b/CMakeLists.txt index 299931c..b62db78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.28...3.30) project(logging) +set(CMAKE_C_STANDARD 99) + if(MSVC) add_compile_options("/source-charset:utf-8") add_compile_options("/execution-charset:utf-8") diff --git a/README.en.md b/README.en.md index d48c5c7..7102038 100644 --- a/README.en.md +++ b/README.en.md @@ -17,6 +17,10 @@ conan create . ``` - cmake ```shell +git clone https://github.com/WangZhongDian/logging.git +cd logging +cmake build -B build . && cd build && cmake --build . +cmake --install . ``` ## usage diff --git a/README.md b/README.md index 1031651..e2521bf 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,28 @@ logging是一个轻量级的简单易用C语言日志库,支持日志级别、 - 支持日志文件:自动创建、自动滚动、日志分割 ## 安装 -- conan安装使用 +### conan安装使用 ```shell -conan create . +git clone https://github.com/WangZhongDian/logging.git +cd logging +conan create . ``` -- cmake安装使用 +在你的项目的conanfile.txt中添加 +```txt +[requires] +logging/0.5.0 +``` + ```shell +conan install . --build=missing +``` + +### cmake安装使用 +```shell +git clone https://github.com/WangZhongDian/logging.git +cd logging +cmake build -B build . && cd build && cmake --build . +cmake --install . ``` ## 使用方法 diff --git a/makefile b/makefile new file mode 100644 index 0000000..aa2e3d3 --- /dev/null +++ b/makefile @@ -0,0 +1,6 @@ + + +.PHONY:format + +format: + bash script/format.sh \ No newline at end of file diff --git a/script/format.sh b/script/format.sh new file mode 100644 index 0000000..e57d74f --- /dev/null +++ b/script/format.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Run clang-format on all C/C++ files in this directory and below. +find . -name "*.c" -o -name "*.h" -exec clang-format -style=file -i {} + \ No newline at end of file diff --git a/src/handler/logging-handler-file.c b/src/handler/logging-handler-file.c index 07dc60b..1e7cc0f 100644 --- a/src/handler/logging-handler-file.c +++ b/src/handler/logging-handler-file.c @@ -11,6 +11,7 @@ #define FILE_NAME_MAX_SIZE 50 +// 文件日志处理器的扩展 typedef struct log_Handler_file_ex_s { unsigned int file_size; unsigned int file_size_max; @@ -57,6 +58,7 @@ log_Handler *loggingHandlerFile(const char *name, unsigned int max_size) { log_Handler *handler = NULL; log_Handler_file_ex_t *handler_ex = NULL; + /// 获取未写满于设置最大文件大小的文件名 do { sprintf(new_file_name, "%s_%d.log", name, suffix++); fp = fopen(new_file_name, "at"); diff --git a/src/logging.c b/src/logging.c index 3b9a3d9..c23d8b6 100644 --- a/src/logging.c +++ b/src/logging.c @@ -16,9 +16,9 @@ #define RESET "\033[0m" #define CYAN "\033[0;36m" -#define LOG_BUFFER_SIZE 1024 +#define LOG_BUFFER_SIZE 1024 // 日志缓冲区大小,单个日志长度不能超过该值 -Logger *G_LOGGER = NULL; +static Logger *G_LOGGER = NULL; // 全局日志对象,唯一实例 /** * @brief 为日志添加一个handler @@ -38,6 +38,10 @@ static bool addHandler(log_Handler *handler) { return true; } +/** + * @brief 为日志添加一个filter + * @param filter 过滤器对象 + */ static bool addFilter(log_filter *filter) { if (G_LOGGER == NULL || filter == NULL) { return false; @@ -58,6 +62,13 @@ static bool addFilter(log_filter *filter) { return true; } +/** + * @brief 输出到handler + * @param handler 处理器对象 + * @param level 日志等级 + * @param color 应用的颜色 + * @param message 日志内容 + */ static void output_to_handler(log_Handler *handler, char *level, const char *color, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 88c7f64..cd06c92 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,6 +11,15 @@ elseif(WIN32) add_test(test_simple ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}simple.exe) endif() +#测试简单基本应用 +add_executable(${PROJECT_NAME}file test-log-file.c) +target_link_libraries(${PROJECT_NAME}file logging) +if(UNIX) + add_test(test_file ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}file) +elseif(WIN32) + add_test(test_file ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}file.exe) +endif() + #测试拦截器 add_executable(${PROJECT_NAME}filter test-filter.c) target_link_libraries(${PROJECT_NAME}filter logging) diff --git a/tests/test-log-file.c b/tests/test-log-file.c new file mode 100644 index 0000000..3b04534 --- /dev/null +++ b/tests/test-log-file.c @@ -0,0 +1,17 @@ +#include "logging.h" +#include "logging/logging-handler.h" + +int main() { + Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG); + log_Handler *hander = loggingHandlerFile("test_log", 1024 * 1024 * 10); + logger->addHandler(hander); + + log_info("This is an info message"); + log_error("This is an error message%s", "123"); + log_fatal("This is an fatal message"); + log_debug("This is a debug message"); + log_warning("This is a warning message%s", "123"); + + destroyDefaultLogger(); + return 0; +} \ No newline at end of file