1 #[macro_use]
2 extern crate log;
3 extern crate env_logger;
4 
5 use std::env;
6 use std::process;
7 use std::str;
8 
main()9 fn main() {
10     if env::var("LOG_REGEXP_TEST").ok() == Some(String::from("1")) {
11         child_main();
12     } else {
13         parent_main()
14     }
15 }
16 
child_main()17 fn child_main() {
18     env_logger::init();
19     info!("XYZ Message");
20 }
21 
run_child(rust_log: String) -> bool22 fn run_child(rust_log: String) -> bool {
23     let exe = env::current_exe().unwrap();
24     let out = process::Command::new(exe)
25         .env("LOG_REGEXP_TEST", "1")
26         .env("RUST_LOG", rust_log)
27         .output()
28         .unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
29     str::from_utf8(out.stderr.as_ref())
30         .unwrap()
31         .contains("XYZ Message")
32 }
33 
assert_message_printed(rust_log: &str)34 fn assert_message_printed(rust_log: &str) {
35     if !run_child(rust_log.to_string()) {
36         panic!("RUST_LOG={} should allow the test log message", rust_log)
37     }
38 }
39 
assert_message_not_printed(rust_log: &str)40 fn assert_message_not_printed(rust_log: &str) {
41     if run_child(rust_log.to_string()) {
42         panic!(
43             "RUST_LOG={} should not allow the test log message",
44             rust_log
45         )
46     }
47 }
48 
parent_main()49 fn parent_main() {
50     // test normal log severity levels
51     assert_message_printed("info");
52     assert_message_not_printed("warn");
53 
54     // test of regular expression filters
55     assert_message_printed("info/XYZ");
56     assert_message_not_printed("info/XXX");
57 }
58