varch/doc/vlog.en.md

108 lines
3.1 KiB
Markdown

### Introduction
The `vlog` is a simple log output module that adopts the channel filtering mode. In other words, it provides log output for multiple channels, and only when a channel is enabled will there be log output for that particular channel.
### Interfaces
#### vlog Log Output
```c
int vlog(vlogChnType channel, const char *format,...);
```
The `channel` parameter provides 8 channels, which are defined as follows:
```c
#define VLOG_CHANNEL_0
#define VLOG_CHANNEL_1
#define VLOG_CHANNEL_2
#define VLOG_CHANNEL_3
#define VLOG_CHANNEL_4
#define VLOG_CHANNEL_5
#define VLOG_CHANNEL_6
#define VLOG_CHANNEL_7
```
It's possible to perform multi-channel output. Multiple channels can be combined for output by using the `|` operator. By default, only `VLOG_CHANNEL_0` is opened.
For example:
```c
static void test_vlog(void)
{
vlog(VLOG_CHANNEL_0, "[VLOG_CHANNEL_0] vlog!\r\n");
vlog(VLOG_CHANNEL_1, "[VLOG_CHANNEL_1] vlog!\r\n");
vlog(VLOG_CHANNEL_2, "[VLOG_CHANNEL_2] vlog!\r\n");
}
```
The result is:
```
[VLOG_CHANNEL_0] vlog!
```
#### vlog Setting and Getting Channel Filters
```c
void vlog_set_filter(vlogChnType mask);
vlogChnType vlog_get_filter(void);
```
These two functions can be used to set and get the enabled channel filters respectively. For more convenience, the following macro definition methods are provided to open and close specific channels:
```c
#define VLOG_ENABALE(c)
#define VLOG_DISABALE(c)
```
Here's an example:
```c
static void test_channel(void)
{
VLOG_DISABALE(VLOG_CHANNEL_0);
VLOG_ENABALE(VLOG_CHANNEL_1);
vlog(VLOG_CHANNEL_0, "[VLOG_CHANNEL_0] vlog!\r\n");
vlog(VLOG_CHANNEL_1, "[VLOG_CHANNEL_1] vlog!\r\n");
vlog(VLOG_CHANNEL_2, "[VLOG_CHANNEL_2] vlog!\r\n");
}
```
The result is:
```
[VLOG_CHANNEL_1] vlog!
```
#### vlog Console Output
```c
int vlog_set_console(vlogChnType channel, int console);
```
This method is used to set whether to enable console output for the specified channel. By default, console output for each channel is already enabled.
#### vlog Offline Saving
```c
int vlog_set_offline(vlogChnType channel, const char *filename);
```
This method can start offline saving or stop it (by passing in a null file name).
For example:
```c
static void test_offline(void)
{
vlog_set_offline(VLOG_CHANNEL_0, "./test/file/log.txt");
for (int i = 0; i < 10; i++)
{
vlog(VLOG_CHANNEL_0, "vlog %d!\r\n", i);
}
vlog_set_offline(VLOG_CHANNEL_0, NULL);
}
```
The result is saved in the file [link](/test/file/log.txt).
#### vlog Function Callback
```c
int vlog_set_func(vlogChnType channel, vlogFuncType func);
```
This method is used to set the `vlog` callback function, which provides greater flexibility. That means the content of the `vlog` function will be output to the callback function.
The callback function type is defined as follows:
```c
typedef void (*vlogFuncType)(char *buf, int len);
```
#### vlog Getting Channel Status
```c
vlogChnType vlog_get_console(void);
vlogChnType vlog_get_offline(void);
vlogChnType vlog_get_func(void);
```
These functions can be used to get the status masks of each channel for the three output directions respectively.