Setup tracing subscriber based on the provided trace level and format. # Arguments `trace_level` - The level of information of to output `trace_format` - The format of the output # Errors If unable to initialize the tracing subscriber, an error message is printed and tracing is disabled.
(trace_level: &TraceLevel, trace_format: &TraceFormat)
| 63 | /// |
| 64 | /// If unable to initialize the tracing subscriber, an error message is printed and tracing is disabled. |
| 65 | pub fn enable_tracing(trace_level: &TraceLevel, trace_format: &TraceFormat) { |
| 66 | // originally implemented in dsc/src/util.rs |
| 67 | let tracing_level = match trace_level { |
| 68 | TraceLevel::Error => Level::ERROR, |
| 69 | TraceLevel::Warn => Level::WARN, |
| 70 | TraceLevel::Info => Level::INFO, |
| 71 | TraceLevel::Debug => Level::DEBUG, |
| 72 | TraceLevel::Trace => Level::TRACE, |
| 73 | }; |
| 74 | |
| 75 | let filter = EnvFilter::try_from_default_env() |
| 76 | .or_else(|_| EnvFilter::try_new("warn")) |
| 77 | .unwrap_or_default() |
| 78 | .add_directive(tracing_level.into()); |
| 79 | let layer = tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr); |
| 80 | let fmt = match trace_format { |
| 81 | TraceFormat::Default => { |
| 82 | layer |
| 83 | .with_ansi(true) |
| 84 | .with_level(true) |
| 85 | .with_line_number(true) |
| 86 | .boxed() |
| 87 | }, |
| 88 | TraceFormat::Plaintext => { |
| 89 | layer |
| 90 | .with_ansi(false) |
| 91 | .with_level(true) |
| 92 | .with_line_number(false) |
| 93 | .boxed() |
| 94 | }, |
| 95 | TraceFormat::Json => { |
| 96 | layer |
| 97 | .with_ansi(false) |
| 98 | .with_level(true) |
| 99 | .with_line_number(true) |
| 100 | .json() |
| 101 | .boxed() |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter); |
| 106 | |
| 107 | if tracing::subscriber::set_global_default(subscriber).is_err() { |
| 108 | eprintln!("{}", t!("utils.unableToTrace")); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// Invoke a command and return the exit code, stdout, and stderr. |
| 113 | /// |