Dev (#19)
Some checks failed
test on Windows / test (push) Has been cancelled
test on Linux / test (push) Failing after 7m1s

* #feat 增强Fatal级别的底色,修改logging类的方法

* 更新版本号

* 加入test脚本

* fix:conanfile

* test action

* 修复错别字

* add test on windows action

* fix test on windows action

* fix action on windows

* fix

* fix 内存分配错误

* fix msvc 不支持中文注释,删除中文注释

* test on windows and test  chinese char

* ersion 0.2.4

* feature:根据文件大小分割日志

* fix:内存泄露

* fix:使用char偏移单位

* add english brief

* 修改api函数名,加入Default关键字,减除歧义

* Multiple substring interceptors

* 更新版本号

* #fix 拦截器对level参数无效

* fix

* #将拦截器改名为过滤器,更加接近职能

* 更新自述文件

* 更新一些自述文件
This commit is contained in:
youmetme
2024-11-24 20:26:04 +08:00
committed by GitHub
parent 9d3a7bbb8d
commit 8e512563e4
9 changed files with 75 additions and 5 deletions

View File

@@ -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")

View File

@@ -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

View File

@@ -14,12 +14,28 @@ logging是一个轻量级的简单易用C语言日志库支持日志级别、
- 支持日志文件:自动创建、自动滚动、日志分割
## 安装
- conan安装使用
### conan安装使用
```shell
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 .
```
## 使用方法

6
makefile Normal file
View File

@@ -0,0 +1,6 @@
.PHONY:format
format:
bash script/format.sh

3
script/format.sh Normal file
View File

@@ -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 {} +

View File

@@ -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");

View File

@@ -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,

View File

@@ -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)

17
tests/test-log-file.c Normal file
View File

@@ -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;
}