Skip to main content

anathema_debug/
lib.rs

1use std::fmt::{Result, Write};
2
3pub trait DebugWriter {
4    fn write(&mut self, output: &mut impl Write) -> Result;
5}
6
7pub struct Debug<O>(pub O);
8
9impl<O: Write> Debug<O> {
10    pub fn new(output: O) -> Self {
11        Self(output)
12    }
13
14    pub fn heading(mut self) -> Self {
15        let _ = writeln!(&mut self.0, "=== Debug ===");
16        self
17    }
18
19    pub fn debug(mut self, title: &str, mut item: impl DebugWriter) -> Self {
20        let _ = writeln!(&mut self.0, "--- {title} ---");
21        let _ = item.write(&mut self.0);
22        self
23    }
24
25    pub fn footer(mut self) -> Self {
26        let _ = writeln!(&mut self.0, "--- EO Debug ---");
27        self
28    }
29
30    pub fn sep(mut self) -> Self {
31        let _ = writeln!(&mut self.0, "----------------");
32        self
33    }
34
35    pub fn finish(self) -> O {
36        self.0
37    }
38}
39
40#[cfg(feature = "filelog")]
41pub mod macros {
42    #[macro_export]
43    macro_rules! debug_to_file {
44        ($($arg:tt)*) => {
45            use ::std::io::Write as _;
46            let mut file = std::fs::OpenOptions::new().create(true).append(true).open("/tmp/log.lol").unwrap();
47            let payload = format!($($arg)*);
48            file.write_all(payload.as_bytes()).unwrap();
49            file.write(b"\n").unwrap();
50            file.flush();
51        }
52    }
53
54    #[macro_export]
55    macro_rules! debug_tree {
56        ($tree:expr) => {
57            let mut d = anathema_widgets::tree::debug::DebugTree::new();
58            $tree.apply_visitor(&mut d);
59            $crate::debug_to_file!("{}", d.output);
60        };
61    }
62}
63
64#[cfg(not(feature = "filelog"))]
65pub mod macros {
66    #[macro_export]
67    macro_rules! debug_to_file {
68        ($($arg:tt)*) => {};
69    }
70
71    #[macro_export]
72    macro_rules! debug_tree {
73        ($tree:expr) => {};
74    }
75}