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.

enumerator UNKNOWN
[source]
enumerator LOGGER
[source]
enumerator QP
[source]
enumerator SCROLL
[source]
enumerator SIPO
[source]
enumerator SPLIT
[source]
enumerator SPI
[source]
enumerator TOUCH
[source]
enumerator MAP
[source]
enumerator ALLOC
[source]
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.

enumerator LOG_NONE
[source]
enumerator LOG_DEBUG
[source]
enumerator LOG_INFO
[source]
enumerator LOG_WARN
[source]
enumerator LOG_ERROR
[source]

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 if UNKNOWN).

  • %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 by msg and ... passed to logging(). With its regular format.

  • %T: Current time, you can override log_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.


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 dump_stack(void)
[source]

Utility to print previous execution’s crash traceback.

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.

log_success(success, feature, msg, args...)
[source]

Run feature, msg and args... through logging().

Severity will be LOG_DEBUG if success is true, else LOG_ERROR.