xref: /third_party/rust/crates/log/tests/macros.rs (revision 2d8ae3ab)
1#[cfg(not(lib_build))]
2#[macro_use]
3extern crate log;
4
5macro_rules! all_log_macros {
6    ($($arg:tt)*) => ({
7        trace!($($arg)*);
8        debug!($($arg)*);
9        info!($($arg)*);
10        warn!($($arg)*);
11        error!($($arg)*);
12    });
13}
14
15#[test]
16fn no_args() {
17    for lvl in log::Level::iter() {
18        log!(lvl, "hello");
19        log!(lvl, "hello",);
20
21        log!(target: "my_target", lvl, "hello");
22        log!(target: "my_target", lvl, "hello",);
23
24        log!(lvl, "hello");
25        log!(lvl, "hello",);
26    }
27
28    all_log_macros!("hello");
29    all_log_macros!("hello",);
30
31    all_log_macros!(target: "my_target", "hello");
32    all_log_macros!(target: "my_target", "hello",);
33}
34
35#[test]
36fn anonymous_args() {
37    for lvl in log::Level::iter() {
38        log!(lvl, "hello {}", "world");
39        log!(lvl, "hello {}", "world",);
40
41        log!(target: "my_target", lvl, "hello {}", "world");
42        log!(target: "my_target", lvl, "hello {}", "world",);
43
44        log!(lvl, "hello {}", "world");
45        log!(lvl, "hello {}", "world",);
46    }
47
48    all_log_macros!("hello {}", "world");
49    all_log_macros!("hello {}", "world",);
50
51    all_log_macros!(target: "my_target", "hello {}", "world");
52    all_log_macros!(target: "my_target", "hello {}", "world",);
53}
54
55#[test]
56fn named_args() {
57    for lvl in log::Level::iter() {
58        log!(lvl, "hello {world}", world = "world");
59        log!(lvl, "hello {world}", world = "world",);
60
61        log!(target: "my_target", lvl, "hello {world}", world = "world");
62        log!(target: "my_target", lvl, "hello {world}", world = "world",);
63
64        log!(lvl, "hello {world}", world = "world");
65        log!(lvl, "hello {world}", world = "world",);
66    }
67
68    all_log_macros!("hello {world}", world = "world");
69    all_log_macros!("hello {world}", world = "world",);
70
71    all_log_macros!(target: "my_target", "hello {world}", world = "world");
72    all_log_macros!(target: "my_target", "hello {world}", world = "world",);
73}
74
75#[test]
76fn enabled() {
77    for lvl in log::Level::iter() {
78        let _enabled = if log_enabled!(target: "my_target", lvl) {
79            true
80        } else {
81            false
82        };
83    }
84}
85
86#[test]
87fn expr() {
88    for lvl in log::Level::iter() {
89        let _ = log!(lvl, "hello");
90    }
91}
92
93#[test]
94#[cfg(feature = "kv_unstable")]
95fn kv_no_args() {
96    for lvl in log::Level::iter() {
97        log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello");
98
99        log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello");
100    }
101
102    all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello");
103    all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello");
104    all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello");
105}
106
107#[test]
108#[cfg(feature = "kv_unstable")]
109fn kv_expr_args() {
110    for lvl in log::Level::iter() {
111        log!(target: "my_target", lvl, cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
112
113        log!(lvl, target = "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
114        log!(lvl, cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
115    }
116
117    all_log_macros!(target: "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
118    all_log_macros!(target = "my_target", cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
119    all_log_macros!(cat_math = { let mut x = 0; x += 1; x + 1 }; "hello");
120}
121
122#[test]
123#[cfg(feature = "kv_unstable")]
124fn kv_anonymous_args() {
125    for lvl in log::Level::iter() {
126        log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
127        log!(lvl, target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
128
129        log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
130    }
131
132    all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
133    all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
134    all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {}", "world");
135}
136
137#[test]
138#[cfg(feature = "kv_unstable")]
139fn kv_named_args() {
140    for lvl in log::Level::iter() {
141        log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
142        log!(lvl, target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
143
144        log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
145    }
146
147    all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
148    all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
149    all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}", world = "world");
150}
151
152#[test]
153#[cfg(feature = "kv_unstable")]
154fn kv_expr_context() {
155    match "chashu" {
156        cat_1 => {
157            info!(target: "target", cat_1 = cat_1, cat_2 = "nori"; "hello {}", "cats")
158        }
159    };
160}
161
162#[test]
163fn implicit_named_args() {
164    #[rustversion::since(1.58)]
165    fn _check() {
166        let world = "world";
167
168        for lvl in log::Level::iter() {
169            log!(lvl, "hello {world}");
170            log!(lvl, "hello {world}",);
171
172            log!(target: "my_target", lvl, "hello {world}");
173            log!(target: "my_target", lvl, "hello {world}",);
174
175            log!(lvl, "hello {world}");
176            log!(lvl, "hello {world}",);
177        }
178
179        all_log_macros!("hello {world}");
180        all_log_macros!("hello {world}",);
181
182        all_log_macros!(target: "my_target", "hello {world}");
183        all_log_macros!(target: "my_target", "hello {world}",);
184
185        all_log_macros!(target = "my_target"; "hello {world}");
186        all_log_macros!(target = "my_target"; "hello {world}",);
187    }
188}
189
190#[test]
191#[cfg(feature = "kv_unstable")]
192fn kv_implicit_named_args() {
193    #[rustversion::since(1.58)]
194    fn _check() {
195        let world = "world";
196
197        for lvl in log::Level::iter() {
198            log!(target: "my_target", lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
199
200            log!(lvl, cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
201        }
202
203        all_log_macros!(target: "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
204        all_log_macros!(target = "my_target", cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
205        all_log_macros!(cat_1 = "chashu", cat_2 = "nori", cat_count = 2; "hello {world}");
206    }
207}
208
209#[test]
210#[cfg(feature = "kv_unstable")]
211fn kv_string_keys() {
212    for lvl in log::Level::iter() {
213        log!(target: "my_target", lvl, "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
214    }
215
216    all_log_macros!(target: "my_target", "also dogs" = "Fílos", "key/that-can't/be/an/ident" = "hi"; "hello {world}", world = "world");
217}
218
219#[test]
220#[cfg(feature = "kv_unstable")]
221fn kv_common_value_types() {
222    all_log_macros!(
223        u8 = 42u8,
224        u16 = 42u16,
225        u32 = 42u32,
226        u64 = 42u64,
227        u128 = 42u128,
228        i8 = -42i8,
229        i16 = -42i16,
230        i32 = -42i32,
231        i64 = -42i64,
232        i128 = -42i128,
233        f32 = 4.2f32,
234        f64 = -4.2f64,
235        bool = true,
236        str = "string";
237        "hello world"
238    );
239}
240
241/// Some and None (from Option) are used in the macros.
242#[derive(Debug)]
243enum Type {
244    Some,
245    None,
246}
247
248#[test]
249fn regression_issue_494() {
250    use self::Type::*;
251    all_log_macros!("some message: {:?}, {:?}", None, Some);
252}
253