torrust_index/bootstrap/
logging.rs1use std::sync::Once;
10
11use tracing::info;
12use tracing::level_filters::LevelFilter;
13
14use crate::config::Threshold;
15
16static INIT: Once = Once::new();
17
18pub fn setup(threshold: &Threshold) {
19 let tracing_level_filter: LevelFilter = threshold.clone().into();
20
21 if tracing_level_filter == LevelFilter::OFF {
22 return;
23 }
24
25 INIT.call_once(|| {
26 tracing_stdout_init(tracing_level_filter, &TraceStyle::Default);
27 });
28}
29
30fn tracing_stdout_init(filter: LevelFilter, style: &TraceStyle) {
31 let builder = tracing_subscriber::fmt().with_max_level(filter);
32
33 let () = match style {
34 TraceStyle::Default => builder.init(),
35 TraceStyle::Pretty(display_filename) => builder.pretty().with_file(*display_filename).init(),
36 TraceStyle::Compact => builder.compact().init(),
37 TraceStyle::Json => builder.json().init(),
38 };
39
40 info!("Logging initialized");
41}
42
43#[derive(Debug)]
44pub enum TraceStyle {
45 Default,
46 Pretty(bool),
47 Compact,
48 Json,
49}
50
51impl std::fmt::Display for TraceStyle {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 let style = match self {
54 TraceStyle::Default => "Default Style",
55 TraceStyle::Pretty(path) => match path {
56 true => "Pretty Style with File Paths",
57 false => "Pretty Style without File Paths",
58 },
59
60 TraceStyle::Compact => "Compact Style",
61 TraceStyle::Json => "Json Format",
62 };
63
64 f.write_str(style)
65 }
66}