Structured Logging

group Logging System

Structured logging with deferred transmission.

A lightweight logging framework designed for embedded systems with limited resources. Stores log entries in a ring buffer for deferred transmission.

Features:

  • Multiple severity levels (DEBUG through CRITICAL)

  • Automatic timestamp and source line capture

  • Component-based organization

  • Generic flush interface via callbacks (transport-agnostic)

  • Ring buffer storage with overwrite support

  • Printf-style formatting

group Structures

Structures and types used by the Logging System.

Defines

OSUSAT_SLOG_MAX_MESSAGE_LEN

Maximum length of a single log message (including null terminator).

Typedefs

typedef uint32_t (*osusat_slog_timestamp_fn_t)(void)

Timestamp provider function type.

Return:

Current timestamp in milliseconds.

typedef void (*osusat_slog_flush_fn_t)(const osusat_slog_entry_t *entry, const char *message, void *ctx)

Flush callback function type.

Called during osusat_slog_flush() for each log entry. Implementation should handle framing, packetization, and transmission as needed.

Param entry:

[in] Pointer to log entry header.

Param message:

[in] Pointer to null-terminated message string.

Param ctx:

[in] User context pointer passed during flush.

Enums

enum osusat_slog_level_t

Log severity levels.

Values:

enumerator OSUSAT_SLOG_DEBUG

Verbose debug information

enumerator OSUSAT_SLOG_INFO

Informational messages

enumerator OSUSAT_SLOG_WARN

Warning conditions

enumerator OSUSAT_SLOG_ERROR

Error conditions

enumerator OSUSAT_SLOG_CRITICAL

Critical failures

Functions

struct __attribute__ ((packed))

Variables

osusat_slog_entry_t
struct osusat_slog_entry_t
#include <slog.h>

Log entry header stored in ring buffer.

Each log entry consists of this header followed by a null-terminated message string.

group Public API

External interface for interacting with the Logging System.

Defines

OSUSAT_SLOG(level, component, fmt, ...)

Main logging macro.

Automatically captures the source line number and formats the message.

Example:

OSUSAT_SLOG(OSUSAT_SLOG_WARN, EPS_BATTERY, "Voltage low: %dmV", voltage);

Parameters:
  • level[in] Severity level (osusat_slog_level_t).

  • component[in] Component identifier (uint8_t).

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments for format string.

LOG_DEBUG(component, fmt, ...)

Log a DEBUG level message.

Parameters:
  • component[in] Component identifier.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments.

LOG_INFO(component, fmt, ...)

Log an INFO level message.

Parameters:
  • component[in] Component identifier.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments.

LOG_WARN(component, fmt, ...)

Log a WARN level message.

Parameters:
  • component[in] Component identifier.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments.

LOG_ERROR(component, fmt, ...)

Log an ERROR level message.

Parameters:
  • component[in] Component identifier.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments.

LOG_CRITICAL(component, fmt, ...)

Log a CRITICAL level message.

Parameters:
  • component[in] Component identifier.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments.

Functions

void osusat_slog_init(osusat_ring_buffer_t *log_buf, osusat_slog_timestamp_fn_t timestamp_fn, osusat_slog_level_t min_level)

Initialize the logging subsystem.

Sets up the log storage buffer and configures timestamp and filtering.

Parameters:
  • log_buf[in] Ring buffer for storing log entries. Should be initialized with overwrite mode enabled.

  • timestamp_fn[in] Function to get current timestamp in milliseconds (e.g., HAL_GetTick). Can be NULL if timestamps are not needed.

  • min_level[in] Minimum log level to record. Logs below this level are silently discarded.

void osusat_slog_change_min_log_level(osusat_slog_level_t min_level)

Change the minimum logging level.

Modifies the minimum log level to record during runtime. Useful for maintenance mode

Parameters:

min_level[in] Minimum log level to record. Logs below this level are silently discarded.

void osusat_slog_write_internal(osusat_slog_level_t level, uint8_t component_id, uint16_t line, const char *fmt, ...)

Write a log entry (internal function).

Formats the log message and stores it in the ring buffer along with metadata. Use the osusat_slog() macro instead of calling this directly.

Parameters:
  • level[in] Severity level of the log.

  • component_id[in] Component/subsystem identifier.

  • line[in] Source code line number.

  • fmt[in] Printf-style format string.

  • ...[in] Variable arguments for format string.

size_t osusat_slog_flush(osusat_slog_flush_fn_t flush_fn, void *ctx)

Flush all pending log entries.

Drains the ring buffer and calls the provided callback for each log entry. The callback is responsible for transmission/packetization.

Parameters:
  • flush_fn[in] Callback function to handle each log entry.

  • ctx[in] User context passed to callback. Can be NULL.

Returns:

Number of log entries flushed.

size_t osusat_slog_pending_count(void)

Get number of log entries currently buffered.

Note

This is an estimate based on buffer usage and average entry size.

Returns:

Approximate count of pending log entries.