A simple logging library implemented in C99
log.c and log.h should be dropped into an existing project and compiled along with it. The library provides 6 function-like macros for logging:
When using this library on embedded ARM targets, semihosting is supported for output. To enable semihosting in OpenOCD, add the following to your configuration:
arm semihosting enable
This allows log messages to be displayed through the debugger when running on hardware.
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:
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
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.
The current logging level can be set by using the log_set_level()
function.
All logs below the given level will not be written to stderr
. By default the
level is LOG_TRACE
, such that nothing is ignored.
2047-03-11 20:18:26 TRACE src/main.c:11: Hello world
Any messages below the given level
are ignored. If the library failed to add a
file pointer a value less-than-zero is returned.
If the log will be written to from multiple threads a lock function can be set.
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.
Returns the name of the given log level as a string.
This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.