From a60529daab46f2b1b48dc59bace0dcc24f5ca0b5 Mon Sep 17 00:00:00 2001 From: GueLaKais Date: Fri, 21 Feb 2025 16:42:38 +0100 Subject: [PATCH 1/4] Added main example code with info logger --- examples/minimal_logging/src/main.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/minimal_logging/src/main.rs diff --git a/examples/minimal_logging/src/main.rs b/examples/minimal_logging/src/main.rs new file mode 100644 index 000000000..1f599f01b --- /dev/null +++ b/examples/minimal_logging/src/main.rs @@ -0,0 +1,23 @@ +use rclrs::{create_node, log, Context, RclrsError, ToLogParams, QOS_PROFILE_DEFAULT}; +use std::{env, time::Duration}; +use std_msgs::msg::String as StringMsg; +fn main() -> Result<(), RclrsError> { + let context = Context::new(env::args())?; + + let node = create_node(&context, "minimal_logger")?; + + let publisher = node.create_publisher("topic", QOS_PROFILE_DEFAULT)?; + + let mut message = StringMsg::default(); + + let mut publish_count: u32 = 1; + + while context.ok() { + message.data = format!("Hello, world! {}", publish_count); + log!(node.info(), "Publishing: [{}]", message.data); + publisher.publish(&message)?; + publish_count += 1; + std::thread::sleep(std::time::Duration::from_millis(500)); + } + Ok(()) +} From ea0c09271de9b89bf182889b55a69dbb055e5fda Mon Sep 17 00:00:00 2001 From: GueLaKais Date: Fri, 21 Feb 2025 16:42:55 +0100 Subject: [PATCH 2/4] added main rust manifest file --- examples/minimal_logging/Cargo.toml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 examples/minimal_logging/Cargo.toml diff --git a/examples/minimal_logging/Cargo.toml b/examples/minimal_logging/Cargo.toml new file mode 100644 index 000000000..e223b8a8a --- /dev/null +++ b/examples/minimal_logging/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "minimal_logging" +version = "0.1.0" +edition = "2021" + +[dependencies] +rclrs = "*" +std_msgs = "*" From 50bee95f9c811f2d9ff190f1d486667316536169 Mon Sep 17 00:00:00 2001 From: GueLaKais Date: Fri, 21 Feb 2025 16:43:04 +0100 Subject: [PATCH 3/4] added some explanations --- examples/minimal_logging/README.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/minimal_logging/README.md diff --git a/examples/minimal_logging/README.md b/examples/minimal_logging/README.md new file mode 100644 index 000000000..75caccf2b --- /dev/null +++ b/examples/minimal_logging/README.md @@ -0,0 +1,34 @@ +# Differences between the [ROS2](https://docs.ros.org/en/jazzy/index.html) logger and standard [`println!()`](https://doc.rust-lang.org/std/macro.println.html) + +The [ROS2 logger](https://docs.ros.org/en/jazzy/Tutorials/Demos/Logging-and-logger-configuration.html) and +[classic stdout (e.g., `println!`)](https://en.wikipedia.org/wiki/Standard_streams) differ in several key ways: + +1. **Severity Levels**: + ROS2 provides structured log levels (`DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`), enabling filtering based on urgency. `println!` outputs raw text without categorization. + +2. **Dynamic Control**: + ROS2 allows runtime adjustment of logging levels (e.g., enabling `DEBUG` on-the-fly). `println!` statements cannot be disabled without code changes. + +3. **Metadata**: + ROS2 automatically appends context (timestamp, node name, file/line number) to logs. `println!` requires manual addition of such details. + +4. **Integration**: + ROS2 logs are captured by tools like `rqt_console` and can be routed to files/network. `println!` outputs only to stdout unless manually redirected. + +5. **Performance**: + ROS2 optimizes by skipping disabled log levels (e.g., `DEBUG` if not active). `println!` always executes, incurring overhead regardless of need. + +6. **Configuration**: + ROS2 logging is configurable via launch files/parameters (e.g., per-node log levels). `println!` offers no built-in configuration. + +**Example**: + +```rust +// ROS2 logger (Rust example with rclrs) +log!(node.info(), "Sensor value: {}", sensor_data); // Adds metadata, severity, and runtime control + +// Classic stdout +println!("Sensor value: {}", sensor_data); // Simple, unstructured output +``` + +**Use ROS2 logger** for structured, controllable logging within the ROS2 ecosystem; use `println!` for quick, simple text output. From d8a4ccfdbbb1bbd815bf9b82ada7c65e7538fa7e Mon Sep 17 00:00:00 2001 From: GueLaKais Date: Fri, 21 Feb 2025 16:43:13 +0100 Subject: [PATCH 4/4] added main ros2 manifest file --- examples/minimal_logging/package.xml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/minimal_logging/package.xml diff --git a/examples/minimal_logging/package.xml b/examples/minimal_logging/package.xml new file mode 100644 index 000000000..8b20e88fb --- /dev/null +++ b/examples/minimal_logging/package.xml @@ -0,0 +1,19 @@ + + + + + examples_rclrs_minimal_logging + 0.1.0 + Package containing an example of the logging mechanism in rclrs. + Apache License 2.0 + + rclrs + rosidl_runtime_rs + std_msgs + + + ament_cargo + +