Skip to content

Commit c1b4900

Browse files
committed
allow log macros as expression
This allows using them in e.g. a match statement without a block, and it matches the behavior of std's `print!()` macros. ```rust match true { true => ros_info!("hello"), false => unreachable!(), } ```
1 parent 00fe624 commit c1b4900

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

rosrust/src/log_macros.rs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,181 @@
11
#[macro_export]
22
macro_rules! ros_log {
3-
($level:expr, $($arg:tt)+) => {
3+
($level:expr, $($arg:tt)+) => {{
44
let msg = format!($($arg)*);
55
$crate::log($level, msg, file!(), line!());
6-
}
6+
}}
77
}
88

99
#[macro_export]
1010
macro_rules! ros_debug {
1111
($($arg:tt)*) => {
12-
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
12+
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*)
1313
}
1414
}
1515

1616
#[macro_export]
1717
macro_rules! ros_info {
1818
($($arg:tt)*) => {
19-
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*);
19+
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*)
2020
}
2121
}
2222

2323
#[macro_export]
2424
macro_rules! ros_warn {
2525
($($arg:tt)*) => {
26-
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*);
26+
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*)
2727
}
2828
}
2929

3030
#[macro_export]
3131
macro_rules! ros_err {
3232
($($arg:tt)*) => {
33-
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*);
33+
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*)
3434
}
3535
}
3636

3737
#[macro_export]
3838
macro_rules! ros_fatal {
3939
($($arg:tt)*) => {
40-
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
40+
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*)
4141
}
4242
}
4343

4444
#[macro_export]
4545
macro_rules! ros_log_once {
46-
($level:expr, $($arg:tt)+) => {
46+
($level:expr, $($arg:tt)+) => {{
4747
let msg = format!($($arg)*);
4848
$crate::log_once($level, msg, file!(), line!());
49-
}
49+
}}
5050
}
5151

5252
#[macro_export]
5353
macro_rules! ros_debug_once {
5454
($($arg:tt)*) => {
55-
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
55+
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*)
5656
}
5757
}
5858

5959
#[macro_export]
6060
macro_rules! ros_info_once {
6161
($($arg:tt)*) => {
62-
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*);
62+
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::INFO, $($arg)*)
6363
}
6464
}
6565

6666
#[macro_export]
6767
macro_rules! ros_warn_once {
6868
($($arg:tt)*) => {
69-
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*);
69+
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::WARN, $($arg)*)
7070
}
7171
}
7272

7373
#[macro_export]
7474
macro_rules! ros_err_once {
7575
($($arg:tt)*) => {
76-
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*);
76+
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*)
7777
}
7878
}
7979

8080
#[macro_export]
8181
macro_rules! ros_fatal_once {
8282
($($arg:tt)*) => {
83-
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
83+
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*)
8484
}
8585
}
8686

8787
#[macro_export]
8888
macro_rules! ros_log_throttle {
89-
($period:expr, $level:expr, $($arg:tt)+) => {
89+
($period:expr, $level:expr, $($arg:tt)+) => {{
9090
let msg = format!($($arg)*);
9191
$crate::log_throttle($period, $level, msg, file!(), line!());
92-
}
92+
}}
9393
}
9494

9595
#[macro_export]
9696
macro_rules! ros_debug_throttle {
9797
($period:expr, $($arg:tt)*) => {
98-
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
98+
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*)
9999
}
100100
}
101101

102102
#[macro_export]
103103
macro_rules! ros_info_throttle {
104104
($period:expr, $($arg:tt)*) => {
105-
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*);
105+
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*)
106106
}
107107
}
108108

109109
#[macro_export]
110110
macro_rules! ros_warn_throttle {
111111
($period:expr, $($arg:tt)*) => {
112-
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*);
112+
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*)
113113
}
114114
}
115115

116116
#[macro_export]
117117
macro_rules! ros_err_throttle {
118118
($period:expr, $($arg:tt)*) => {
119-
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*);
119+
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*)
120120
}
121121
}
122122

123123
#[macro_export]
124124
macro_rules! ros_fatal_throttle {
125125
($period:expr, $($arg:tt)*) => {
126-
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
126+
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*)
127127
}
128128
}
129129
#[macro_export]
130130
macro_rules! ros_log_throttle_identical {
131-
($period:expr, $level:expr, $($arg:tt)+) => {
131+
($period:expr, $level:expr, $($arg:tt)+) => {{
132132
let msg = format!($($arg)*);
133133
$crate::log_throttle_identical($period, $level, msg, file!(), line!());
134-
}
134+
}}
135135
}
136136

137137
#[macro_export]
138138
macro_rules! ros_debug_throttle_identical {
139139
($period:expr, $($arg:tt)*) => {
140-
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
140+
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*)
141141
}
142142
}
143143

144144
#[macro_export]
145145
macro_rules! ros_info_throttle_identical {
146146
($period:expr, $($arg:tt)*) => {
147-
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*);
147+
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::INFO, $($arg)*)
148148
}
149149
}
150150

151151
#[macro_export]
152152
macro_rules! ros_warn_throttle_identical {
153153
($period:expr, $($arg:tt)*) => {
154-
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*);
154+
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::WARN, $($arg)*)
155155
}
156156
}
157157

158158
#[macro_export]
159159
macro_rules! ros_err_throttle_identical {
160160
($period:expr, $($arg:tt)*) => {
161-
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*);
161+
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::ERROR, $($arg)*)
162162
}
163163
}
164164

165165
#[macro_export]
166166
macro_rules! ros_fatal_throttle_identical {
167167
($period:expr, $($arg:tt)*) => {
168-
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
168+
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*)
169+
}
170+
}
171+
172+
#[cfg(test)]
173+
fn _macro_in_expr_position() {
174+
// Only tests compilation
175+
match true {
176+
true => ros_info!("hello"),
177+
false => unreachable!(),
169178
}
179+
ros_info! {"hello"}
180+
ros_info!("hello");
170181
}

0 commit comments

Comments
 (0)