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        .append(true)
13        .open("tui_debug.log")
14        .ok();
15
16    let _ = DEBUG_FILE.set(Mutex::new(file));
17}
18
19#[macro_export]
20macro_rules! debug_log {
21    ($($arg:tt)*) => {
22        #[cfg(debug_assertions)]
23        {
24            if let Some(debug_file) = $crate::utils::debug_helpers::DEBUG_FILE.get() {
25                if let Ok(mut guard) = debug_file.lock() {
26                    if let Some(ref mut file) = *guard {
27                        let _ = writeln!(file, "[{}] {}",
28                            chrono::Local::now().format("%H:%M:%S%.3f"),
29                            format!($($arg)*));
30                        let _ = file.flush();
31                    }
32                }
33            }
34        }
35    };
36}
37
38pub fn debug_breakpoint(label: &str) {
39    #[cfg(debug_assertions)]
40    {
41        debug_log!("BREAKPOINT: {}", label);
42
43        // This allows you to set a breakpoint here in RustRover
44        // The label will be logged so you know which point was hit
45        let _debug_marker = format!("Debug point: {label}");
46    }
47}