Debian Codegraph @

third_party/rust/rusqlite/tests/config_log.rs

raw
1 //! This file contains unit tests for `rusqlite::trace::config_log`. This
2 //! function affects SQLite process-wide and so is not safe to run as a normal
3 //! #[test] in the library.
4
5 #[cfg(feature = "trace")]
6 fn main() {
7 use std::os::raw::c_int;
8 use std::sync::{LazyLock, Mutex};
9
10 static LOGS_RECEIVED: LazyLock<Mutex<Vec<(c_int, String)>>> =
11 LazyLock::new(|| Mutex::new(Vec::new()));
12
13 fn log_handler(err: c_int, message: &str) {
14 let mut logs_received = LOGS_RECEIVED.lock().unwrap();
15 logs_received.push((err, message.to_owned()));
16 }
17
18 use rusqlite::trace;
19
20 unsafe { trace::config_log(Some(log_handler)) }.unwrap();
21 trace::log(10, "First message from rusqlite");
22 unsafe { trace::config_log(None) }.unwrap();
23 trace::log(11, "Second message from rusqlite");
24
25 let logs_received = LOGS_RECEIVED.lock().unwrap();
26 assert_eq!(logs_received.len(), 1);
27 assert_eq!(logs_received[0].0, 10);
28 assert_eq!(logs_received[0].1, "First message from rusqlite");
29 }
30
31 #[cfg(not(feature = "trace"))]
32 fn main() {}