MCU-Common
Modules useful for embedded (MCU) programming
Logger

Universal logger module with deferred processing. More...

Data Structures

struct  logger
 Logger instance. More...
 

Macros

#define LOGGER_MAX_ARGC   6
 The maximum number of logger_put() arguments supported by the implementation. More...
 
#define LOGGER_INIT(log, log_write_cb, log_capacity, str_capacity)
 Initializes the logger instance and allocates its string and FIFO buffers. More...
 
#define LOGGER_PUT(log, ...)    logger_put(log, VA_ARGC(__VA_ARGS__), __VA_ARGS__)
 Logs a message. More...
 

Functions

bool logger_init (struct logger *log)
 Initializes logger. More...
 
bool logger_put (const struct logger *log, int argc, const char *fmt,...)
 Logs a message (puts it into internal buffer for later processing). More...
 
bool logger_process (const struct logger *log)
 Processes a single logged message from the buffer. More...
 

Detailed Description

Universal logger module with deferred processing.

Macro Definition Documentation

◆ LOGGER_MAX_ARGC

#define LOGGER_MAX_ARGC   6

The maximum number of logger_put() arguments supported by the implementation.

It is possible to reduce the number by providing a custom LOGGER_MAX_ARGC definition to save the space required by logger_entry.

◆ LOGGER_INIT

#define LOGGER_INIT (   log,
  log_write_cb,
  log_capacity,
  str_capacity 
)
Value:
do { \
static struct fifo logger_fifo; \
FIFO_INIT(&logger_fifo, sizeof(struct logger_entry), \
(log_capacity)); \
static char str[(str_capacity)]; \
(log)->fifo = &logger_fifo; \
(log)->write_cb = (log_write_cb); \
(log)->str = (str); \
(log)->str_size = (str_capacity); \
(log)->write_cb = (log_write_cb); \
logger_init((log)); \
} while (0)
FIFO instance.
Definition: fifo.h:63
Logger entry (used internally)
Definition: logger.h:79

Initializes the logger instance and allocates its string and FIFO buffers.

Parameters
logPointer to the logger structure
log_write_cbPointer to write callback implemented by driver (see logger.write_cb for details)
log_capacityCapacity of the internal FIFO (maximum number of messages to be stored, see fifo.capacity)
str_capacityCapacity of the internal string buffer (should be large enough to store a message composed by snprintf, see logger.str and logger.str_size)

◆ LOGGER_PUT

#define LOGGER_PUT (   log,
  ... 
)     logger_put(log, VA_ARGC(__VA_ARGS__), __VA_ARGS__)

Logs a message.

This is a macro which automatically determines the number of arguments and is replaced by the proper function logger_put0() to logger_put6().

Parameters
logPointer to the logger structure
...Format string and up to LOGGER_MAX_ARGC optional arguments to be passed to sprintf (the format string is mandatory).

Function Documentation

◆ logger_init()

bool logger_init ( struct logger log)

Initializes logger.

Parameters
logPointer to the logger structure
Returns
true if initialization succeeds, false otherwise

◆ logger_put()

bool logger_put ( const struct logger log,
int  argc,
const char *  fmt,
  ... 
)

Logs a message (puts it into internal buffer for later processing).

The message consists of a format string and variable number of arguments which will be processed by sprintf in logger_process(). To determine the number of arguments (argc) automatically, use macro LOGGER_PUT() instead.

The access to internal fifo is protected by a critical section so logger_put() can be called from multiple threads or interrupt handlers.

Parameters
logPointer to the logger structure
argcNumber of arguments (0 to LOGGER_MAX_ARGC)
[in]fmtFormat string to be passed to sprintf
...Optional arguments to be passed to sprintf (only argc parameters will be processed)
Returns
true if initialization succeeds, false otherwise (internal FIFO is full)

◆ logger_process()

bool logger_process ( const struct logger log)

Processes a single logged message from the buffer.

This function calls logger.write_cb callback (implemented by the driver) to write the message to an actual output interface. It is meant to defer log processing from high priority contexts (such as interrupts) and should be therefore called in a low-priority context (e.g. a main loop).

It is assumed this function is called from multiple threads (the internal fifo is not protected by any locking mechanism).

Parameters
logPointer to the logger structure
Returns
true if a message has been processed, false otherwise (internal FIFO is empty)