logging level got at runtime from envvar 'RUST_LOG'

progress
Avril 4 years ago
parent e4ff08ef8f
commit 950bf6db88
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -52,11 +52,22 @@ cfg_if!{
type Pointer = *const Never; 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()?; 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 /// Currently a mock impl for process
@ -113,7 +124,7 @@ async fn validate_config(config: config::Config) -> eyre::Result<config::Config>
async fn begin() -> eyre::Result<i32> async fn begin() -> eyre::Result<i32>
{ {
use futures::prelude::*; use futures::prelude::*;
install()?; let (log_level,) = install()?;
{ {
let (progress, progress_join) = { let (progress, progress_join) = {
let bar = termprogress::progress::Bar::new(50); let bar = termprogress::progress::Bar::new(50);
@ -132,10 +143,7 @@ async fn begin() -> eyre::Result<i32>
let state = { let state = {
use progress::logging; use progress::logging;
let mut logger = logging::Logger::new(progress, 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 let state = Arc::new(state::State::new(validate_config(config::Config::default()).await
.wrap_err(eyre!("Failed to validate config"))?, .wrap_err(eyre!("Failed to validate config"))?,
@ -154,7 +162,6 @@ async fn begin() -> eyre::Result<i32>
{ {
args::usage(); 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. //Cleanup deferred in new `async` block to drop `state` before this block is ran.
async move { async move {

@ -1,5 +1,10 @@
//! Logging //! Logging
use super::*; use super::*;
use std::{
str,
fmt,
error,
};
/// The logging level /// The logging level
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[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<Self, Self::Err> {
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 impl Level
{ {

Loading…
Cancel
Save