sequencer_utils/
logging.rs

1use clap::{Parser, ValueEnum};
2use hotshot::helpers::initialize_logging;
3use log_panics::BacktraceMode;
4
5/// Controls how backtraces are logged on panic.
6///
7/// The values here match the possible values of `RUST_LOG_FORMAT`, and their corresponding behavior
8/// on backtrace logging is:
9/// * `full`: print a prettified dump of the stack trace and span trace to stdout, optimized for
10///   human readability rather than machine parsing
11/// * `compact`: output the default panic message, with backtraces controlled by `RUST_BACKTRACE`
12/// * `json`: output the panic message and stack trace as a tracing event. This in turn works with
13///   the behavior of the tracing subscriber with `RUST_LOG_FORMAT=json` to output the event in a
14///   machine-parseable, JSON format.
15#[derive(Clone, Copy, Debug, Default, ValueEnum)]
16enum BacktraceLoggingMode {
17    #[default]
18    Full,
19    Compact,
20    Json,
21}
22
23/// Logging configuration.
24#[derive(Clone, Debug, Default, Parser)]
25pub struct Config {
26    #[clap(long, env = "RUST_LOG_FORMAT")]
27    backtrace_mode: Option<BacktraceLoggingMode>,
28}
29
30impl Config {
31    /// Get the logging configuration from the environment.
32    pub fn from_env() -> Self {
33        Self::parse_from(std::iter::empty::<String>())
34    }
35
36    /// Initialize logging and panic handlers based on this configuration.
37    pub fn init(&self) {
38        initialize_logging();
39
40        if let BacktraceLoggingMode::Json = self.backtrace_mode.unwrap_or_default() {
41            log_panics::Config::new()
42                .backtrace_mode(BacktraceMode::Resolved)
43                .install_panic_hook();
44        }
45    }
46}