STM32 Logger
Loading...
Searching...
No Matches
logger.h File Reference

Modular and flexible logging system for STM32-based MCU projects. More...

#include <stdint.h>
#include <stdarg.h>

Go to the source code of this file.

Enumerations

enum  LogLevel { LOG_LEVEL_ERROR = 0 , LOG_LEVEL_WARN , LOG_LEVEL_INFO , LOG_LEVEL_DEBUG }
 Defines severity levels for logging. More...

Functions

void Log_Init (void)
 Initialize the logging system.
void Log_SetLevel (LogLevel level)
 Set the active logging level.
void Log (LogLevel level, const char *format,...)
 Log a formatted message if the level passes the threshold.
void Log_Flush (void)
 Flush output buffers.
void Log_Disable (void)
 Disable all logging at runtime.
 __attribute__ ((weak)) void Log_Write_UART(const char *msg)
 UART output hook. Can be overridden by user.

Detailed Description

Modular and flexible logging system for STM32-based MCU projects.

Features:

  • Output to UART, SD card, or both (selectable via #define LOG_USE_* macros).
  • Supports blocking and non-blocking UART (DMA or IT).
  • Circular ring buffer for non-blocking operation.
  • Verbosity control with log levels: ERROR, WARN, INFO, DEBUG.
  • Runtime enable/disable of logging via Log_Disable().
  • Flush function for SD log file.
  • User-overridable hooks for output backends.

Configuration Macros:

  • LOG_USE_UART: Enable/disable UART output (default: 1)
  • LOG_USE_SD: Enable/disable SD card output (default: 0)
  • LOG_USE_IT: Enable interrupt-based UART TX (default: 1)
  • LOG_USE_DMA: Enable DMA-based UART TX (default: 0)
  • LOG_BUFFER_SIZE: Buffer size for log formatting
  • LOG_RING_BUFFER_SIZE: Ring buffer size for non-blocking TX

Example:

Log(LOG_LEVEL_INFO, "System initialized.\n");
Log(LOG_LEVEL_DEBUG, "ADC value: %d\n", adc_value);
Log_Disable(); // Temporarily stop logging
Log(LOG_LEVEL_ERROR, "This won't be printed.\n");
Log_Flush(); // Sync SD card if enabled
void Log_Disable(void)
Disable all logging at runtime.
Definition logger.c:135
void Log(LogLevel level, const char *format,...)
Log a formatted message if the level passes the threshold.
Definition logger.c:149
void Log_Init(void)
Initialize the logging system.
Definition logger.c:113
void Log_SetLevel(LogLevel level)
Set the active logging level.
Definition logger.c:128
@ LOG_LEVEL_DEBUG
Definition logger.h:53
@ LOG_LEVEL_ERROR
Definition logger.h:50
@ LOG_LEVEL_INFO
Definition logger.h:52
void Log_Flush(void)
Flush output buffers.
Definition logger.c:169

Enumeration Type Documentation

◆ LogLevel

enum LogLevel

Defines severity levels for logging.

Enumerator
LOG_LEVEL_ERROR 

Critical error

LOG_LEVEL_WARN 

Warning but not fatal

LOG_LEVEL_INFO 

Informational message

LOG_LEVEL_DEBUG 

Debug-level message

Function Documentation

◆ __attribute__()

__attribute__ ( (weak) ) const

UART output hook. Can be overridden by user.

Default SD card log output (weak). Can be overridden for filters, timestamps, or buffering.

SD card output hook. Can be overridden by user.

Parameters
msgNull-terminated string to send via UART.
msgNull-terminated string to write to file.
msgNull-terminated string to append to log file.

◆ Log()

void Log ( LogLevel level,
const char * format,
... )

Log a formatted message if the level passes the threshold.

Parameters
levelThe severity of the message.
formatprintf-style format string.
...Arguments to format.

Log a formatted message if the level passes the threshold.

  • Message is dropped if below current log level or if logging is disabled.
  • Output is routed to UART and/or SD depending on configuration.
Parameters
levelSeverity level (LOG_LEVEL_ERROR, etc.)
formatprintf-style format string.
...Arguments to format.

◆ Log_Disable()

void Log_Disable ( void )

Disable all logging at runtime.

Logging can be re-enabled by calling Log_SetLevel().

Disable all logging at runtime.

◆ Log_Flush()

void Log_Flush ( void )

Flush output buffers.

For SD logging, this calls f_sync() to commit to disk.

Flush output buffers.

◆ Log_Init()

void Log_Init ( void )

Initialize the logging system.

Sets up buffers, SD card (if enabled), and clears state.

Initialize the logging system.

  • Resets the ring buffer
  • Enables logging
  • Opens SD file if SD logging is enabled

◆ Log_SetLevel()

void Log_SetLevel ( LogLevel level)

Set the active logging level.

Messages below this level will be ignored.

Parameters
levelOne of the values from LogLevel.

Set the active logging level.

Parameters
levelDesired log level (e.g., LOG_LEVEL_DEBUG).