elpekenin/logging.h
¶
Custom logging utilities, inspired by Python’s logging
module.
Note
Under the hood, this is just a wrapper on top of printf()
.
Default format for logging messages.
-
enum feature_t¶
[source] Different features that may emit log messages.
Warning
If you want to add a new one, it has to be the last element.
-
enum log_level_t¶
[source] Different level of severity. Used to filter out messages.
Warning
If you want to add a new one, it has to be the last element.
Hint
The logging()
function will apply an extra transformation to your input, based on a custom format.
Its specifiers being:
%F
: The feature’s name (nothing ifUNKNOWN
).%LL
: The message’s level (long). Eg:DEBUG
.These strings are set in
level_str
.
%LS
: Print only the first char of the previous string. Eg:D
.%M
: The actual message created bymsg
and...
passed tologging()
. With its regular format.%T
: Current time, you can overridelog_time()
to hook it with a RTC or whatever.Default implementation is seconds since boot.
%%
: Write a literal%
.
For example, with format of [%F] (%LL) -- %M | %T
, messages would look like: [QP] (DEBUG) -- <msg%args> | 3s
-
int logging(feature_t feature, log_level_t level, const char *msg, ...)¶
[source] Emit a logging message.
- Parameters:
feature – Piece of code being logged.
level – Severity of the message.
msg – Format string for the message.
... – Variadic arguments to fill the specifiers in
msg
.
- Returns:
Whether message could be emitted.
See also
-
log_level_t get_level_for(feature_t feature)¶
[source] Get the current level for a feature. Messages with a lower severity are dropped.
-
void set_level_for(feature_t feature, log_level_t level)¶
[source] Change the level set to for a feature.
-
void step_level_for(feature_t feature, bool increase)¶
[source] Increase (or decrease) by one the level set for a feature.
The direction is based on
increase
.
Attention
From this point, the functions are mostly implementation details.
You, most likely, don’t need to know anything about them.
-
log_level_t get_message_level(void)¶
[source] Get the severity of the message being emitted.
This may be used by a
sendchar_func_t
internally.
-
const char *log_time(void)¶
[source] Get a string representing the current time.
By default, seconds since boot, but it can be overwritten.
-
ASSERT_FEATURES(__array)¶
[source] Check that an array has as many elements as features are defined.
If not, compilation will error out.
-
ASSERT_LEVELS(__array)¶
[source] Check that an array has as many elements as logging levels are defined.
If not, compilation will error out.
-
void print_str(const char *str, const sendchar_func_t func)¶
[source] Print a string using the given function.
Caution
Logging’s own formatting is not applied.
- Parameters:
str – Regular string (no format specifiers).
func – The function used to process the string.
-
void print_u8(const uint8_t val, const sendchar_func_t func)¶
[source] Print a number using the given function.
Caution
Logging’s own formatting is not applied.
- Parameters:
val – Number to be printed.
func – The function used to process the string.
-
void print_u8_array(const uint8_t *list, const size_t len, const sendchar_func_t func)¶
[source] Print a list of numbers using the given function.
Caution
Logging’s own formatting is not applied.
- Parameters:
list – Start of the array.
len – How long
list
is.func – The function used to process the string.