Skip to content

Commit 6f392b1

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 6f392b1

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

rosrust/src/log_macros.rs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,180 @@
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 {
11-
($($arg:tt)*) => {
11+
($($arg:tt)*) => {{
1212
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
13-
}
13+
}}
1414
}
1515

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

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

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

3737
#[macro_export]
3838
macro_rules! ros_fatal {
39-
($($arg:tt)*) => {
39+
($($arg:tt)*) => {{
4040
$crate::ros_log!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
41-
}
41+
}}
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 {
54-
($($arg:tt)*) => {
54+
($($arg:tt)*) => {{
5555
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
56-
}
56+
}}
5757
}
5858

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

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

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

8080
#[macro_export]
8181
macro_rules! ros_fatal_once {
82-
($($arg:tt)*) => {
82+
($($arg:tt)*) => {{
8383
$crate::ros_log_once!($crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
84-
}
84+
}}
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 {
97-
($period:expr, $($arg:tt)*) => {
97+
($period:expr, $($arg:tt)*) => {{
9898
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
99-
}
99+
}}
100100
}
101101

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

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

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

123123
#[macro_export]
124124
macro_rules! ros_fatal_throttle {
125-
($period:expr, $($arg:tt)*) => {
125+
($period:expr, $($arg:tt)*) => {{
126126
$crate::ros_log_throttle!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
127-
}
127+
}}
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 {
139-
($period:expr, $($arg:tt)*) => {
139+
($period:expr, $($arg:tt)*) => {{
140140
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::DEBUG, $($arg)*);
141-
}
141+
}}
142142
}
143143

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

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

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

165165
#[macro_export]
166166
macro_rules! ros_fatal_throttle_identical {
167-
($period:expr, $($arg:tt)*) => {
167+
($period:expr, $($arg:tt)*) => {{
168168
$crate::ros_log_throttle_identical!($period, $crate::msg::rosgraph_msgs::Log::FATAL, $($arg)*);
169+
}}
170+
}
171+
172+
#[test]
173+
fn macro_in_expr_position() {
174+
match true {
175+
true => ros_info!("hello"),
176+
false => unreachable!(),
169177
}
178+
ros_info! {"hello"}
179+
ros_info!("hello");
170180
}

0 commit comments

Comments
 (0)