Log Framework API ReferenceΒΆ

This section documents the Log Framework API.

Log DefinitionsΒΆ

enum log_async_policy_tΒΆ

Async buffer full policy.

Values:

enumerator LOG_ASYNC_POLICY_DROP_OLDESTΒΆ

Drop oldest message when full

enumerator LOG_ASYNC_POLICY_DROP_NEWESTΒΆ

Drop newest message when full

enumerator LOG_ASYNC_POLICY_BLOCKΒΆ

Block until space available

LOG_DEFAULT_LEVELΒΆ

Default configuration values.

LOG_MAX_MSG_LENΒΆ
LOG_MAX_BACKENDSΒΆ
LOG_MAX_MODULE_FILTERSΒΆ
LOG_MODULE_NAME_LENΒΆ
LOG_DEFAULT_FORMATΒΆ
LOG_ASYNC_BUFFER_SIZEΒΆ
LOG_ASYNC_QUEUE_SIZEΒΆ
LOG_ASYNC_TASK_STACK_SIZEΒΆ
LOG_ASYNC_TASK_PRIORITYΒΆ
enum log_status_tΒΆ

Log status codes.

Values:

enumerator LOG_OKΒΆ

Operation successful

enumerator LOG_ERRORΒΆ

Generic error

enumerator LOG_ERROR_INVALID_PARAMΒΆ

Invalid parameter

enumerator LOG_ERROR_NOT_INITΒΆ

Not initialized

enumerator LOG_ERROR_NO_MEMORYΒΆ

Out of memory

enumerator LOG_ERROR_FULLΒΆ

Buffer full

enumerator LOG_ERROR_BACKENDΒΆ

Backend error

enumerator LOG_ERROR_ALREADY_INITΒΆ

Already initialized

enum log_level_tΒΆ

Log levels.

Log levels are ordered from most verbose (TRACE) to least verbose (FATAL). LOG_LEVEL_NONE disables all logging.

Values:

enumerator LOG_LEVEL_TRACEΒΆ

Most detailed tracing information

enumerator LOG_LEVEL_DEBUGΒΆ

Debug information

enumerator LOG_LEVEL_INFOΒΆ

General information

enumerator LOG_LEVEL_WARNΒΆ

Warning messages

enumerator LOG_LEVEL_ERRORΒΆ

Error messages

enumerator LOG_LEVEL_FATALΒΆ

Fatal error messages

enumerator LOG_LEVEL_NONEΒΆ

Disable all logging

LOG_COMPILE_LEVELΒΆ

Compile-time log level.

Messages below this level will be compiled out entirely. Set to LOG_LEVEL_NONE to disable all logging at compile time. This reduces code size by eliminating log calls at compile time.

Example usage in build system:

  • CMake: add_definitions(-DLOG_COMPILE_LEVEL=LOG_LEVEL_INFO)

  • GCC: -DLOG_COMPILE_LEVEL=LOG_LEVEL_INFO

Requirements: 7.2, 7.3

LOG_USE_STATIC_ALLOCΒΆ

Static allocation mode.

When enabled (set to 1), the log framework uses static allocation instead of dynamic memory allocation (malloc/free). This is useful for embedded systems where dynamic allocation is not available or not desired.

When static allocation is enabled:

  • Backend structures are allocated from a static pool

  • Memory backend uses a static buffer

  • No malloc/free calls are made

Requirements: 7.4

LOG_STATIC_BACKEND_COUNTΒΆ

Maximum number of statically allocated backends.

Note

Only used when LOG_USE_STATIC_ALLOC is enabled

LOG_STATIC_MEMORY_BUFFER_SIZEΒΆ

Static memory backend buffer size.

Note

Only used when LOG_USE_STATIC_ALLOC is enabled

LOG_IS_OK(status)ΒΆ

Check if status is OK.

Parameters:
  • status – [in] Status to check

Returns:

true if OK, false otherwise

LOG_IS_ERROR(status)ΒΆ

Check if status is error.

Parameters:
  • status – [in] Status to check

Returns:

true if error, false otherwise

LOG_RETURN_IF_ERROR(status)ΒΆ

Return if status is error.

Parameters:
  • status – [in] Status to check

LOG_UNUSED(x)ΒΆ

Unused parameter macro.

Parameters:
  • x – [in] Parameter to mark as unused

Log MacrosΒΆ

LOG_TRACE(fmt, ...)ΒΆ

Log a trace message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_DEBUG(fmt, ...)ΒΆ

Log a debug message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_INFO(fmt, ...)ΒΆ

Log an info message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_WARN(fmt, ...)ΒΆ

Log a warning message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_ERROR(fmt, ...)ΒΆ

Log an error message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_FATAL(fmt, ...)ΒΆ

Log a fatal error message.

Parameters:
  • fmt – [in] Printf-style format string

  • ... – [in] Format arguments

LOG_MODULEΒΆ

Default module name if not defined.

LOG_WRITE(level, fmt, ...)ΒΆ

Internal logging macro.

Log BackendΒΆ

typedef struct log_backend log_backend_tΒΆ

Forward declaration of backend structure.

typedef log_status_t (*log_backend_init_fn)(void *ctx)ΒΆ

Backend initialization function type.

Param ctx:

[in] Backend context

Return:

LOG_OK on success, error code otherwise

typedef log_status_t (*log_backend_write_fn)(void *ctx, const char *msg, size_t len)ΒΆ

Backend write function type.

Param ctx:

[in] Backend context

Param msg:

[in] Message to write

Param len:

[in] Message length

Return:

LOG_OK on success, error code otherwise

typedef log_status_t (*log_backend_flush_fn)(void *ctx)ΒΆ

Backend flush function type.

Param ctx:

[in] Backend context

Return:

LOG_OK on success, error code otherwise

typedef log_status_t (*log_backend_deinit_fn)(void *ctx)ΒΆ

Backend deinitialization function type.

Param ctx:

[in] Backend context

Return:

LOG_OK on success, error code otherwise

log_status_t log_backend_register(log_backend_t *backend)ΒΆ

Register a backend with the log system.

Note

The backend structure must remain valid for the lifetime of the registration

Parameters:

backend – [in] Pointer to backend structure

Returns:

LOG_OK on success, error code otherwise

log_status_t log_backend_unregister(const char *name)ΒΆ

Unregister a backend from the log system.

Parameters:

name – [in] Name of the backend to unregister

Returns:

LOG_OK on success, error code otherwise

log_status_t log_backend_enable(const char *name, bool enable)ΒΆ

Enable or disable a backend.

Parameters:
  • name – [in] Name of the backend

  • enable – [in] true to enable, false to disable

Returns:

LOG_OK on success, error code otherwise

log_backend_t *log_backend_get(const char *name)ΒΆ

Get a registered backend by name.

Parameters:

name – [in] Name of the backend

Returns:

Pointer to backend, or NULL if not found

struct log_backendΒΆ
#include <log_backend.h>

Log backend structure.

Defines the interface for log output backends. Each backend must implement at least the write function.

Public Members

const char *nameΒΆ

Backend name (must be unique)

log_backend_init_fn initΒΆ

Initialization function (optional)

log_backend_write_fn writeΒΆ

Write function (required)

log_backend_flush_fn flushΒΆ

Flush function (optional)

log_backend_deinit_fn deinitΒΆ

Deinitialization function (optional)

void *ctxΒΆ

Backend-specific context

log_level_t min_levelΒΆ

Minimum level for this backend

bool enabledΒΆ

Whether backend is enabled

Console BackendΒΆ

log_backend_t *log_backend_console_create(void)ΒΆ

Create a console backend.

Note

Uses stdout for output (Native platform)

Returns:

Pointer to backend structure, or NULL on failure

void log_backend_console_destroy(log_backend_t *backend)ΒΆ

Destroy a console backend.

Parameters:

backend – [in] Pointer to backend to destroy

UART BackendΒΆ

log_backend_t *log_backend_uart_create(nx_uart_t *uart)ΒΆ

Create a UART backend.

Parameters:

uart – [in] UART interface pointer (nx_uart_t*)

Returns:

Pointer to backend structure, or NULL on failure

void log_backend_uart_destroy(log_backend_t *backend)ΒΆ

Destroy a UART backend.

Parameters:

backend – [in] Pointer to backend to destroy

log_status_t log_backend_uart_set_timeout(log_backend_t *backend, uint32_t timeout_ms)ΒΆ

Set UART backend transmit timeout.

Parameters:
  • backend – [in] Pointer to UART backend

  • timeout_ms – [in] Timeout in milliseconds

Returns:

LOG_OK on success, error code otherwise

nx_uart_t *log_backend_uart_get_interface(log_backend_t *backend)ΒΆ

Get UART interface from backend.

Parameters:

backend – [in] Pointer to UART backend

Returns:

UART interface pointer, or NULL on error

Memory BackendΒΆ

log_backend_t *log_backend_memory_create(size_t size)ΒΆ

Create a memory backend.

Parameters:

size – [in] Buffer size in bytes

Returns:

Pointer to backend structure, or NULL on failure

void log_backend_memory_destroy(log_backend_t *backend)ΒΆ

Destroy a memory backend.

Parameters:

backend – [in] Pointer to backend to destroy

size_t log_backend_memory_read(log_backend_t *backend, char *buf, size_t len)ΒΆ

Read data from memory backend buffer.

Parameters:
  • backend – [in] Pointer to memory backend

  • buf – [out] Buffer to read into

  • len – [in] Maximum bytes to read

Returns:

Number of bytes read

void log_backend_memory_clear(log_backend_t *backend)ΒΆ

Clear the memory backend buffer.

Parameters:

backend – [in] Pointer to memory backend

size_t log_backend_memory_size(log_backend_t *backend)ΒΆ

Get the number of bytes in the memory backend buffer.

Parameters:

backend – [in] Pointer to memory backend

Returns:

Number of bytes in buffer