xref: /aosp_15_r20/external/crosvm/base/tests/linux/syslog.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::io::Read;
6 use std::io::Seek;
7 use std::io::SeekFrom;
8 use std::sync::Once;
9 
10 use base::syslog::*;
11 
12 static EARLY_INIT_ONCE: Once = Once::new();
13 
setup()14 pub fn setup() {
15     EARLY_INIT_ONCE.call_once(|| {
16         early_init();
17     });
18 }
19 
20 #[test]
fds()21 fn fds() {
22     setup();
23     let mut fds = Vec::new();
24     push_descriptors(&mut fds);
25     assert!(!fds.is_empty());
26     for fd in fds {
27         assert!(fd >= 0);
28     }
29 }
30 
31 #[test]
syslog_file()32 fn syslog_file() {
33     setup();
34     let mut file = tempfile::tempfile().expect("failed to create tempfile");
35 
36     let syslog_file = file.try_clone().expect("error cloning shared memory file");
37     let state = State::new(LogConfig {
38         pipe: Some(Box::new(syslog_file)),
39         ..Default::default()
40     })
41     .unwrap();
42 
43     const TEST_STR: &str = "hello shared memory file";
44     state.log(
45         &log::RecordBuilder::new()
46             .level(Level::Error)
47             .args(format_args!("{}", TEST_STR))
48             .build(),
49     );
50 
51     file.seek(SeekFrom::Start(0))
52         .expect("error seeking shared memory file");
53     let mut buf = String::new();
54     file.read_to_string(&mut buf)
55         .expect("error reading shared memory file");
56     assert!(buf.contains(TEST_STR));
57 }
58 
59 #[test]
macros()60 fn macros() {
61     setup();
62     log::error!("this is an error {}", 3);
63     log::warn!("this is a warning {}", "uh oh");
64     log::info!("this is info {}", true);
65     log::debug!("this is debug info {:?}", Some("helpful stuff"));
66 }
67