pzzld_server/config/kinds/
tracing.rs

1/*
2    Appellation: tracing <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::config::LogLevel;
6
7fn default_true() -> bool {
8    true
9}
10
11#[derive(
12    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
13)]
14#[serde(default)]
15pub struct TracingConfig {
16    #[serde(default = "default_true")]
17    pub(crate) ansi: bool,
18    pub(crate) file: bool,
19    pub(crate) level: LogLevel,
20    pub(crate) line_number: bool,
21    #[serde(default = "default_true")]
22    pub(crate) target: bool,
23    pub(crate) thread_ids: bool,
24    pub(crate) thread_names: bool,
25}
26
27impl TracingConfig {
28    pub fn new(level: LogLevel) -> Self {
29        Self {
30            ansi: true,
31            file: false,
32            level,
33            line_number: false,
34            target: true,
35            thread_ids: false,
36            thread_names: false,
37        }
38    }
39
40    pub fn level(&self) -> LogLevel {
41        self.level
42    }
43
44    setwith! {
45        ansi: bool,
46        level: LogLevel,
47        target: bool,
48        thread_ids: bool,
49        thread_names: bool,
50    }
51    /// Initialize the tracer with the given name
52    pub fn init_tracing(&self, name: &str) {
53        use tracing_subscriber::{filter::EnvFilter, util::SubscriberInitExt};
54
55        let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
56            format!("{name}={level},tower_http={level}", level = self.level).into()
57        });
58
59        tracing_subscriber::fmt()
60            .compact()
61            .with_ansi(self.ansi)
62            .with_env_filter(filter)
63            .with_file(self.file)
64            .with_line_number(self.line_number)
65            .with_max_level(self.level.as_tracing_level())
66            .with_target(self.target)
67            .with_thread_ids(self.thread_ids)
68            .with_thread_names(self.thread_names)
69            .with_timer(tracing_subscriber::fmt::time::uptime())
70            .finish()
71            .init();
72        tracing::debug!("success: initialized tracing modules...");
73    }
74}
75
76impl Default for TracingConfig {
77    fn default() -> Self {
78        Self::new(LogLevel::Trace)
79    }
80}
81
82/// Initialize the tracer with the given name
83fn _init_tracing(config: &TracingConfig, name: &str) {
84    use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
85
86    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
87        format!("{name}={level},tower_http={level}", level = config.level).into()
88    });
89
90    let layer = tracing_subscriber::fmt::layer()
91        .compact()
92        .with_ansi(config.ansi)
93        .with_target(config.target)
94        .with_timer(tracing_subscriber::fmt::time::uptime());
95
96    tracing_subscriber::registry()
97        .with(filter)
98        .with(layer)
99        .init();
100    tracing::trace!("Initialized tracing modules...");
101}