Skip to content

Commit

Permalink
Merge pull request #1435 from zthompson47/fail-on-RUST_LOG-parse-error
Browse files Browse the repository at this point in the history
Fail on error parsing RUST_LOG
  • Loading branch information
hannobraun authored Dec 10, 2022
2 parents aed0b9a + 6c1b605 commit ac8a8da
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions crates/fj-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod args;
mod config;
mod path;

use std::{env, error::Error};

use anyhow::{anyhow, Context};
use fj_export::export;
use fj_host::Parameters;
Expand All @@ -28,16 +30,9 @@ use tracing_subscriber::EnvFilter;
use crate::{args::Args, config::Config};

fn main() -> anyhow::Result<()> {
// Respect `RUST_LOG`. If that's not defined or erroneous, log warnings and
// above.
//
// It would be better to fail, if `RUST_LOG` is erroneous, but I don't know
// how to distinguish between that and the "not defined" case.
// Respect `RUST_LOG`. If that's not defined, log warnings and above. Fail if it's erroneous.
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("WARN")),
)
.with_env_filter(try_default_env_filter()?)
.event_format(format().pretty())
.init();

Expand Down Expand Up @@ -75,3 +70,29 @@ fn no_model_error() -> anyhow::Error {
- Specify a default model in the configuration file."
)
}

fn try_default_env_filter() -> anyhow::Result<EnvFilter> {
let env_filter = EnvFilter::try_from_default_env();

match env_filter {
Ok(env_filter) => Ok(env_filter),

Err(err) => {
if let Some(kind) = err.source() {
if let Some(env::VarError::NotPresent) =
kind.downcast_ref::<env::VarError>()
{
return Ok(EnvFilter::new("WARN"));
}
} else {
// `tracing_subscriber::filter::FromEnvError` currently returns a source
// in all cases.
unreachable!()
}

Err(anyhow!(
"There was an error parsing the RUST_LOG environment variable."
))
}
}
}

0 comments on commit ac8a8da

Please sign in to comment.