sc/logger/README.md

63 lines
1.4 KiB
Markdown
Raw Normal View History

2021-02-07 22:31:04 +03:00
### Logger
2020-12-27 16:14:34 +03:00
### Overview
- Log destination can be stdout, file and user callback.
2021-02-05 20:46:59 +03:00
- You can pass logs to all destinations at the same time.
2020-12-27 16:14:34 +03:00
- Log files are rotated.
- Thread-safe.
2021-02-07 22:31:04 +03:00
### Usage
2020-12-27 16:14:34 +03:00
```c
#include "sc_log.h"
int log_callback(void *arg, enum sc_log_level level,
const char *fmt, va_list va)
{
const char *my_app = arg;
const char *level_str = sc_log_levels[level].str;
fprintf(stdout, " %s received log : level = [%s] ", my_app, level_str);
vfprintf(stdout, fmt, va);
return 0;
}
int main(int argc, char *argv[])
{
const char* my_app_name = "my app";
2021-02-06 21:36:18 +03:00
sc_log_init(); // Call once when your app starts.
2020-12-27 16:14:34 +03:00
//Default log-level is 'info' and default destination is 'stdout'
2021-02-06 21:36:18 +03:00
sc_log_info("Hello world!\n");
2020-12-27 16:14:34 +03:00
//Enable logging to file.
sc_log_set_file("log.0.txt", "log-latest.txt");
2021-02-06 21:36:18 +03:00
//stdout and file will get the log line
sc_log_info("to stdout and file!\n");
2020-12-27 16:14:34 +03:00
//Enable callback
2021-02-06 21:36:18 +03:00
sc_log_set_callback((void*) my_app_name, log_callback);
2020-12-27 16:14:34 +03:00
2021-02-06 21:36:18 +03:00
//stdout, file and callback will get the log line
sc_log_info("to all!\n");
2020-12-27 16:14:34 +03:00
2021-02-06 21:36:18 +03:00
sc_log_term(); // Call once on shutdown.
2020-12-27 16:14:34 +03:00
return 0;
}
```
2021-02-03 08:09:50 +03:00
Output is like :
```
[2021-02-03 04:46:22][INFO ][my app thread] (log_example.c:28) Hello world!
[2021-02-03 04:46:22][INFO ][my app thread] (log_example.c:34) to stdout and file!
[2021-02-03 04:46:22][INFO ][my app thread] (log_example.c:40) to all!
```