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