Skip to content

Commit 887af75

Browse files
committed
advanced_logging
1 parent c463201 commit 887af75

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "advanced_logging"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rclrs = "*"
8+
std_msgs = "*"
9+
anyhow = { version = "1", features = ["backtrace"] }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<?xml-model
3+
href="http://download.ros.org/schema/package_format3.xsd"
4+
schematypens="http://www.w3.org/2001/XMLSchema"?>
5+
<package format="3">
6+
<name>examples_rclrs_advanced_logging</name>
7+
<version>0.1.0</version>
8+
<description>Package containing an advanced example of the logging mechanism in rclrs.</description>
9+
<maintainer email="[email protected]">Antoine Van Malleghem</maintainer>
10+
<license>Apache License 2.0</license>
11+
12+
<depend>rclrs</depend>
13+
<depend>rosidl_runtime_rs</depend>
14+
<depend>std_msgs</depend>
15+
16+
<export>
17+
<build_type>ament_cargo</build_type>
18+
</export>
19+
</package>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use anyhow::{Error, Result};
2+
use rclrs::*;
3+
use std::time::Duration;
4+
5+
fn main() -> Result<(), Error> {
6+
let context = Context::default_from_env()?;
7+
let executor = context.create_basic_executor();
8+
9+
let node = executor.create_node("advanced_logger")?;
10+
11+
let publisher = node.create_publisher::<std_msgs::msg::String>("topic", QOS_PROFILE_DEFAULT)?;
12+
13+
let mut message = std_msgs::msg::String::default();
14+
15+
let mut publish_count: u32 = 1;
16+
17+
while context.ok() {
18+
message.data = format!("Hello, world! {}", publish_count);
19+
// log_fatal!(&node.name(), "Simple message from {}", node.name());
20+
log!(
21+
node.info().skip_first(),
22+
"Publish every message but the first one: [{}]",
23+
message.data
24+
);
25+
log!(
26+
node.warn().throttle(Duration::from_millis(3000)),
27+
"Publish with 3s throttling: [{}]",
28+
message.data
29+
);
30+
log!(
31+
node.error().only_if(publish_count % 10 == 0),
32+
"Publishing every 10 messages: [{}]",
33+
message.data
34+
);
35+
publisher.publish(&message)?;
36+
publish_count += 1;
37+
std::thread::sleep(std::time::Duration::from_millis(500));
38+
}
39+
Ok(())
40+
}

0 commit comments

Comments
 (0)