sql_cli/utils/
debug_helpers.rs

1use std::fs::OpenOptions;
2#[allow(unused_imports)]
3use std::io::Write;
4use std::sync::Mutex;
5use std::sync::OnceLock;
6
7pub static DEBUG_FILE: OnceLock<Mutex<Option<std::fs::File>>> = OnceLock::new();
8
9pub fn init_debug_log() {
10    let file = OpenOptions::new()
11        .create(true)
12        .write(true)
13        .append(true)
14        .open("tui_debug.log")
15        .ok();
16
17    let _ = DEBUG_FILE.set(Mutex::new(file));
18}
19
20#[macro_export]
21macro_rules! debug_log {
22    ($($arg:tt)*) => {
23        #[cfg(debug_assertions)]
24        {
25            if let Some(debug_file) = $crate::utils::debug_helpers::DEBUG_FILE.get() {
26                if let Ok(mut guard) = debug_file.lock() {
27                    if let Some(ref mut file) = *guard {
28                        let _ = writeln!(file, "[{}] {}",
29                            chrono::Local::now().format("%H:%M:%S%.3f"),
30                            format!($($arg)*));
31                        let _ = file.flush();
32                    }
33                }
34            }
35        }
36    };
37}
38
39pub fn debug_breakpoint(label: &str) {
40    #[cfg(debug_assertions)]
41    {
42        debug_log!("BREAKPOINT: {}", label);
43
44        // This allows you to set a breakpoint here in RustRover
45        // The label will be logged so you know which point was hit
46        let _debug_marker = format!("Debug point: {}", label);
47    }
48}