Skip to content

Commit

Permalink
feat: remove log header on default terminal output (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMoesl committed Nov 26, 2020
1 parent d30491b commit 92cce66
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod cli;

use anyhow::Result;
use anyhow::{Context, Result};
use bytesize::ByteSize;
use cli::expect_arg;
use env_logger::{Env, TimestampPrecision};
Expand All @@ -10,21 +10,17 @@ use monster::{
engine,
exploration_strategy::ShortestPathStrategy,
};
use std::path::Path;
use std::{env, io::Write, path::Path};

fn main() -> Result<()> {
let matches = cli::args().get_matches();

// process global flags
let log_level = expect_arg(&matches, "verbose");

let env = Env::new()
.filter_or("MONSTER_LOG", log_level)
.write_style_or("MONSTER_LOG_STYLE", "always");

env_logger::Builder::from_env(env)
.format_timestamp(Some(TimestampPrecision::Millis))
.init();
init_logger(log_level)?;

// process subcommands
match matches.subcommand() {
Some(("disassemble", args)) => {
let input = Path::new(expect_arg(&args, "input-file"));
Expand Down Expand Up @@ -72,3 +68,21 @@ fn main() -> Result<()> {
_ => unreachable!(),
}
}

fn init_logger(cli_log_level: &str) -> Result<()> {
let env = Env::new()
.filter_or("MONSTER_LOG", cli_log_level)
.write_style_or("MONSTER_LOG_STYLE", "always");

let mut builder = env_logger::Builder::from_env(env);

builder.format_timestamp(Some(TimestampPrecision::Millis));

let level = env::var("MONSTER_LOG").unwrap_or_else(|_| cli_log_level.to_owned());

if level == "info" {
builder.format(|buf, record| writeln!(buf, "{}", record.args()));
}

builder.try_init().context("Failed to initialize logger")
}

0 comments on commit 92cce66

Please sign in to comment.