* #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
160 lines
3.9 KiB
Markdown
160 lines
3.9 KiB
Markdown
# C language logging library logging
|
|
|
|
## brief
|
|
|
|
Logging is a lightweight and easy-to-use C language log library that supports log level, log format, log output, log files, and other functions.
|
|
|
|
## function
|
|
- Support log levels: DEBUG, INFO, Warning, ERROR, FATAL
|
|
- Support log formats: timestamp, log level, log content
|
|
- Support log output: console, file
|
|
- Support log files: automatic creation, automatic scrolling, log segmentation
|
|
|
|
## install
|
|
- Conan
|
|
```shell
|
|
conan create .
|
|
```
|
|
- cmake
|
|
```shell
|
|
```
|
|
|
|
## usage
|
|

|
|

|
|
|
|
### console log
|
|
```c
|
|
#include "logging.h"
|
|
|
|
int main() {
|
|
Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);
|
|
|
|
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;
|
|
}
|
|
```
|
|
|
|
### file log
|
|
```c
|
|
#include "logging.h"
|
|
#include "logging/logging-handler.h"
|
|
|
|
int main() {
|
|
Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);
|
|
logger->addHandler(loggingFileHandler("test1", 1024*1024));
|
|
|
|
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;
|
|
}
|
|
```
|
|
|
|
### Logging Interceptor
|
|
> Support adding custom interceptors, currently with built-in substring interceptors
|
|
> The function of an interceptor is to redirect intercepted logs to the interceptor's dedicated processor
|
|
|
|
|
|
#### example
|
|
Redirects intercepted logs to a dedicated file processor
|
|
```c
|
|
#include "logging.h"
|
|
#include <stdio.h>
|
|
|
|
int main() {
|
|
Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);
|
|
|
|
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");
|
|
|
|
char *test1[] = {"123", "tt", NULL};
|
|
|
|
log_Interceptor *tint = loggingSubStringInterceptor(
|
|
test1,
|
|
LOG_DEBUG,
|
|
loggingFileHandler("test_interceptor", 1024 * 1024),
|
|
true);
|
|
|
|
logger->addInterceptor(tint);
|
|
|
|
printf("\n");
|
|
printf("Interceptor added\n");
|
|
printf("\n");
|
|
|
|
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;
|
|
}
|
|
```
|
|
|
|
#### Multiple substring interceptors
|
|
```c
|
|
#include "logging.h"
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
int main() {
|
|
Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);
|
|
|
|
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");
|
|
|
|
char *test1[] = {"This",NULL};
|
|
|
|
log_Interceptor *tint = loggingSubStringInterceptor(
|
|
test1,
|
|
LOG_DEBUG,
|
|
loggingFileHandler("test_interceptor", 1024 * 1024),
|
|
false);
|
|
|
|
logger->addInterceptor(tint);
|
|
|
|
char *test2[] = {"123",NULL};
|
|
|
|
log_Interceptor *tint1 = loggingSubStringInterceptor(
|
|
test2,
|
|
LOG_DEBUG,
|
|
loggingFileHandler("test_interceptor1", 1024 * 1024),
|
|
true);
|
|
|
|
logger->addInterceptor(tint1);
|
|
|
|
printf("\n");
|
|
printf("Interceptor added\n");
|
|
printf("\n");
|
|
|
|
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;
|
|
}
|
|
```
|
|
|