tracing_fast_dev/
lib.rs

1use std::sync::OnceLock;
2
3use tracing::Level;
4use tracing_subscriber::FmtSubscriber;
5
6
7pub fn tfd()-> &'static Tfd{
8    static INSTANCE:OnceLock<Tfd> = OnceLock::new();
9
10    INSTANCE.get_or_init(||{
11        Tfd::new()
12    })
13}
14
15pub struct Tfd {
16    pub format_occupancy: usize,
17}
18
19impl Tfd {
20    fn new() -> Tfd {
21        let tfd = Tfd { format_occupancy: 15 };
22        tfd.init_dev();
23        tfd
24    }
25
26    fn init_dev(&self) {
27        let subscriber = FmtSubscriber::builder()
28            .with_max_level(Level::TRACE)
29            .without_time()
30            .with_target(false)
31            .finish();
32
33        tracing::subscriber::set_global_default(subscriber)
34            .expect("setting default subscriber failed");
35    }
36
37    pub fn info(&self, func_identifier: &str, token: &str) {
38        tracing::info!(
39            "### {:<width$} - {}",
40            token,
41            func_identifier,
42            width = self.format_occupancy
43        );
44    }
45
46    pub fn debug(&self, func_identifier: &str, token: &str) {
47        tracing::debug!(
48            "### {:<width$} - {}",
49            token,
50            func_identifier,
51            width = self.format_occupancy
52        );
53    }
54
55    pub fn error(&self, func_identifier: &str, token: &str) {
56        tracing::error!(
57            "### {:<width$} - {}",
58            token,
59            func_identifier,
60            width = self.format_occupancy
61        );
62    }
63}
64
65impl Default for Tfd {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71
72
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_info_logging() {
80        let tfd = tfd();
81        tfd.info("test_info", "Info message test");
82    }
83
84    #[test]
85    fn test_debug_logging() {
86        let tfd = tfd();
87        tfd.debug("test_debug", "Debug message test");
88    }
89
90    #[test]
91    fn test_error_logging() {
92        let tfd = tfd();
93        tfd.error("test_error", "Error message test");
94    }
95}