Skip to content

Added a simple ros2 rust logger example #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/minimal_logging/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "minimal_logging"
version = "0.1.0"
edition = "2021"

[dependencies]
rclrs = "*"
std_msgs = "*"
34 changes: 34 additions & 0 deletions examples/minimal_logging/README.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions examples/minimal_logging/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

<?xml version="1.0"?>
<?xml-model
href="http://download.ros.org/schema/package_format3.xsd"
schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>examples_rclrs_minimal_logging</name>
<version>0.1.0</version>
<description>Package containing an example of the logging mechanism in rclrs.</description>
<license>Apache License 2.0</license>

<depend>rclrs</depend>
<depend>rosidl_runtime_rs</depend>
<depend>std_msgs</depend>

<export>
<build_type>ament_cargo</build_type>
</export>
</package>
23 changes: 23 additions & 0 deletions examples/minimal_logging/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading