1use crate::context::Depth;
2use tracing_subscriber::prelude::*;
3
4pub fn enable_by_env() {
5 let is_enabled = std::env::var("RESOLVER_TRACE").map_or(false, |var| {
6 matches!(var.as_str(), "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR")
7 });
8 if !is_enabled {
9 return;
10 }
11 let formatter = Formatter::default();
12 tracing_subscriber::Registry::default()
13 .with(formatter)
14 .with(tracing_subscriber::EnvFilter::from_env("RESOLVER_TRACE"))
15 .init();
16}
17
18#[derive(Default)]
19struct Formatter {}
20
21impl<S> tracing_subscriber::Layer<S> for Formatter
22where
23 S: tracing::Subscriber + std::fmt::Debug,
24{
25 fn on_event(&self, event: &tracing::Event<'_>, _: tracing_subscriber::layer::Context<'_, S>) {
26 event.record(&mut Data);
27 }
28}
29
30struct Data;
31
32impl tracing::field::Visit for Data {
33 fn record_debug(&mut self, _field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
34 eprintln!("{value:?}");
35 }
36}
37
38pub mod color {
40 const BOLD: &str = "\u{001b}[1m";
41 const RED: &str = "\u{001b}[31m";
42 const GREEN: &str = "\u{001b}[32m";
43 const BLUE: &str = "\u{001b}[34m";
44 const CYAN: &str = "\u{001b}[36m";
45 const RESET: &str = "\u{001b}[0m";
46
47 pub fn bold<T: core::fmt::Display>(s: &T) -> String {
48 format!("{BOLD}{s}{RESET}")
49 }
50
51 pub fn red<T: core::fmt::Display>(s: &T) -> String {
52 format!("{RED}{s}{RESET}")
53 }
54
55 pub fn green<T: core::fmt::Display>(s: &T) -> String {
56 format!("{GREEN}{s}{RESET}")
57 }
58
59 pub fn blue<T: core::fmt::Display>(s: &T) -> String {
60 format!("{BLUE}{s}{RESET}")
61 }
62
63 pub fn cyan<T: core::fmt::Display>(s: &T) -> String {
64 format!("{CYAN}{s}{RESET}")
65 }
66}
67
68pub fn depth(depth: &Depth) -> String {
69 format!("Depth: {}", color::bold(&depth.value()))
70}