Skip to main content

base_console/
console_init_error.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use std::{error::Error, fmt};
5use tracing::subscriber::SetGlobalDefaultError;
6use tracing_subscriber::filter::FromEnvError;
7
8/// Console setup failed before normal application logging began.
9#[derive(Debug)]
10pub enum ConsoleInitError {
11    /// `RUST_LOG` could not be read or parsed.
12    Filter(FromEnvError),
13    /// A global subscriber could not be installed.
14    Subscriber(SetGlobalDefaultError),
15}
16
17impl fmt::Display for ConsoleInitError {
18    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::Filter(_) => formatter.write_str("could not configure the console filter"),
21            Self::Subscriber(_) => formatter.write_str("could not install the console subscriber"),
22        }
23    }
24}
25
26impl Error for ConsoleInitError {
27    fn source(&self) -> Option<&(dyn Error + 'static)> {
28        Some(match self {
29            Self::Filter(error) => error,
30            Self::Subscriber(error) => error,
31        })
32    }
33}
34
35impl From<FromEnvError> for ConsoleInitError {
36    fn from(error: FromEnvError) -> Self {
37        Self::Filter(error)
38    }
39}
40
41impl From<SetGlobalDefaultError> for ConsoleInitError {
42    fn from(error: SetGlobalDefaultError) -> Self {
43        Self::Subscriber(error)
44    }
45}