log.c/README.md

79 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2017-03-12 17:35:25 +03:00
# log.c
A simple logging library implemented in C99
2024-05-30 22:58:07 +03:00
![Screenshot](./.screenshot.png)
2017-03-12 17:35:25 +03:00
## Usage
**[log.c](src/log.c?raw=1)** and **[log.h](src/log.h?raw=1)** should be dropped
into an existing project and compiled along with it. The library provides 6
function-like macros for logging:
```c
log_trace(const char *fmt, ...);
log_debug(const char *fmt, ...);
log_info(const char *fmt, ...);
log_warn(const char *fmt, ...);
log_error(const char *fmt, ...);
log_fatal(const char *fmt, ...);
```
Each function takes a printf format string followed by additional arguments:
```c
log_trace("Hello %s", "world")
```
Resulting in a line with the given format printed to stderr:
```
20:18:26 TRACE src/main.c:11: Hello world
```
2020-06-20 10:57:22 +03:00
#### log_set_quiet(bool enable)
Quiet-mode can be enabled by passing `true` to the `log_set_quiet()` function.
While this mode is enabled the library will not output anything to `stderr`, but
will continue to write to files and callbacks if any are set.
2017-03-12 17:35:25 +03:00
#### log_set_level(int level)
The current logging level can be set by using the `log_set_level()` function.
2020-06-20 10:57:22 +03:00
All logs below the given level will not be written to `stderr`. By default the
level is `LOG_TRACE`, such that nothing is ignored.
2017-03-12 17:35:25 +03:00
2020-06-20 10:57:22 +03:00
#### log_add_fp(FILE *fp, int level)
One or more file pointers where the log will be written can be provided to the
library by using the `log_add_fp()` function. The data written to the file
output is of the following format:
2017-03-12 17:35:25 +03:00
```
2047-03-11 20:18:26 TRACE src/main.c:11: Hello world
```
2020-06-20 10:57:22 +03:00
Any messages below the given `level` are ignored. If the library failed to add a
file pointer a value less-than-zero is returned.
2017-03-12 17:35:25 +03:00
2020-06-20 10:57:22 +03:00
#### log_add_callback(log_LogFn fn, void *udata, int level)
One or more callback functions which are called with the log data can be
provided to the library by using the `log_add_callback()` function. A callback
function is passed a `log_Event` structure containing the `line` number,
`filename`, `fmt` string, `va` printf va\_list, `level` and the given `udata`.
#### log_set_lock(log_LockFn fn, void *udata)
2017-03-12 17:35:25 +03:00
If the log will be written to from multiple threads a lock function can be set.
2020-06-20 10:57:22 +03:00
The function is passed the boolean `true` if the lock should be acquired or
`false` if the lock should be released and the given `udata` value.
#### const char* log_level_string(int level)
Returns the name of the given log level as a string.
2017-03-12 17:35:25 +03:00
#### LOG_USE_COLOR
If the library is compiled with `-DLOG_USE_COLOR` ANSI color escape codes will
be used when printing.
2024-05-30 22:58:07 +03:00
#### LOG_DONT_CODE
If the library is compiled with `-DLOG_DONT_CODE`, then the source code file name
and line number will be removed from the output.
2017-03-12 17:35:25 +03:00
## License
This library is free software; you can redistribute it and/or modify it under
the terms of the MIT license. See [LICENSE](LICENSE) for details.