diff --git a/src/main.rs b/src/main.rs index de9ed94..d25fa52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,11 +52,22 @@ cfg_if!{ type Pointer = *const Never; -fn install() -> eyre::Result<()> +/// Install things and get other things +fn install() -> eyre::Result<(progress::logging::Level,)> { + let logv = std::env::var("RUST_LOG"); color_eyre::install()?; - Ok(()) + let lv = match logv { + Ok(log) if log.trim().len() !=0 => log.parse().unwrap_or_else(|e| { + let def = Default::default(); + eprintln!("Failed to parse logging level, using default `{:?}`: {}", def, e); + def + }), + _ => Default::default(), + }; + + Ok((lv,)) } /// Currently a mock impl for process @@ -113,7 +124,7 @@ async fn validate_config(config: config::Config) -> eyre::Result async fn begin() -> eyre::Result { use futures::prelude::*; - install()?; + let (log_level,) = install()?; { let (progress, progress_join) = { let bar = termprogress::progress::Bar::new(50); @@ -132,11 +143,8 @@ async fn begin() -> eyre::Result let state = { use progress::logging; let mut logger = logging::Logger::new(progress, - logging::Level::Info); //TODO: Get level from env var + log_level); - //let task_id = progress.send_command_and_downcast(progress::CommandKind::AddTask("Task name".to_owned())).await?.unwrap(); - //progress.send_command_and_wait(progress::CommandKind::AddTask("Task 2 name".to_owned())).await?; - let state = Arc::new(state::State::new(validate_config(config::Config::default()).await .wrap_err(eyre!("Failed to validate config"))?, logger.clone())); @@ -154,7 +162,6 @@ async fn begin() -> eyre::Result { args::usage(); } - //progress.send_command_and_wait(progress::CommandKind::RemoveTask(task_id)).await?; //Cleanup deferred in new `async` block to drop `state` before this block is ran. async move { diff --git a/src/progress/logging.rs b/src/progress/logging.rs index 9e5843e..10f1201 100644 --- a/src/progress/logging.rs +++ b/src/progress/logging.rs @@ -1,5 +1,10 @@ //! Logging use super::*; +use std::{ + str, + fmt, + error, +}; /// The logging level #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -28,6 +33,36 @@ impl fmt::Display for Level } } +impl str::FromStr for Level +{ + type Err = LevelParseError; + + fn from_str(s: &str) -> Result { + let s = s.trim().to_lowercase(); + Ok(match s.as_str() { + "trace" => Level::Trace, + "debug" => Level::Debug, + "info" => Level::Info, + "warning" => Level::Warning, + "error" => Level::Error, + "fatal" => Level::Fatal, + _ => return Err(LevelParseError(s)), + }) + } +} + +#[derive(Debug)] +pub struct LevelParseError(String); + +impl error::Error for LevelParseError{} +impl fmt::Display for LevelParseError +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "{:?} is not a valid logging level", self.0) + } +} + impl Level {