Skip to main content

Module logger

Module logger 

Source
Expand description

ROS 2 compatible logging utilities

This module provides a Logger type that wraps the log crate with node-specific context, matching the rclrs logging patterns.

§Example

let logger = node.logger();
logger.info("Node started");
logger.warn("Low battery");
logger.error("Connection failed");

§Conditional Logging

The logger supports modifiers for conditional logging:

use nros_core::{Logger, OnceFlag};

let logger = Logger::new("my_node");

// Log only once (requires a static flag)
static LOGGED: OnceFlag = OnceFlag::new();
logger.info_once(&LOGGED, "This logs only once");

// Skip the first occurrence
static SKIP: OnceFlag = OnceFlag::new();
logger.warn_skip_first(&SKIP, "This skips the first call");

// Rate-limited logging (requires tracking elapsed time)
let mut last_log_ms: u64 = 0;
let current_time_ms: u64 = 1000; // From your clock
logger.info_throttle(
    &mut last_log_ms,
    current_time_ms,
    1000,
    "Rate limited to 1Hz",
);

§Embedded Integration

For embedded targets, this logger integrates with the log crate facade, allowing you to use any log-compatible backend:

§Desktop/std targets

Use env_logger or similar:

env_logger::init();

§Embedded targets with defmt

Use defmt-log to bridge log calls to defmt for minimal overhead:

// In Cargo.toml:
// defmt = "0.3"
// defmt-log = "0.1"

// In your code:
use defmt_log as _;  // Links the logger

This approach stores format strings on the host (not target), making it ideal for resource-constrained microcontrollers.

§RTIC/Embassy

Both frameworks commonly use defmt directly. The defmt-log bridge allows nros logging to integrate seamlessly with your existing defmt setup.

Structs§

Logger
A logger associated with a ROS node
OnceFlag
A flag for tracking one-time logging