Skip to content

Commit

Permalink
Centralize log setup into own crate, add color options
Browse files Browse the repository at this point in the history
Waiting for Drakulix/simplelog.rs#72 to land
for the latter; using a fork meantime
  • Loading branch information
mrkline committed Mar 26, 2021
1 parent 64d734b commit 37f996c
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 220 deletions.
138 changes: 22 additions & 116 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
members = ["flt2vhs", "vhscat", "flt-mover"]
members = ["flt2vhs", "vhscat", "flt-mover", "logsetup"]
resolver = "2" # Try new dependency resolver from Rust 1.51

[profile.dev]
Expand Down
2 changes: 1 addition & 1 deletion flt-mover/Cargo.toml
Expand Up @@ -11,5 +11,5 @@ anyhow = "1.0"
chrono = "0.4"
notify = "4.0"
log = "0.4"
simplelog = "0.7"
logsetup = { version = "0.1", path = "../logsetup" }
structopt = "0.3.8"
34 changes: 10 additions & 24 deletions flt-mover/src/main.rs
Expand Up @@ -10,7 +10,6 @@ use anyhow::*;
use chrono::prelude::*;
use log::*;
use notify::{raw_watcher, RecursiveMode, Watcher};
use simplelog::*;
use structopt::StructOpt;

/// Monitors a directory and moves FLT files out from under BMS,
Expand All @@ -22,6 +21,15 @@ struct Args {
#[structopt(short, long, parse(from_occurrences))]
verbose: u8,

#[structopt(short, long, case_insensitive = true, default_value = "auto")]
#[structopt(name = "always/auto/never")]
color: logsetup::Color,

/// Prepend ISO-8601 timestamps to all messages
/// (from --verbose). Useful for benchmarking.
#[structopt(short, long, verbatim_doc_comment)]
timestamps: bool,

/// Switch to this working directory (e.g., BMS/User/Acmi)
/// before doing anything else.
#[structopt(short = "C", long, name = "path")]
Expand All @@ -46,7 +54,7 @@ struct Args {

fn main() -> Result<()> {
let args = Args::from_args();
init_logger(args.verbose)?;
logsetup::init_logger(args.verbose, args.timestamps, args.color)?;

if let Some(change_to) = &args.directory {
env::set_current_dir(change_to).with_context(|| {
Expand All @@ -72,28 +80,6 @@ fn main() -> Result<()> {
}
}

/// Set up simplelog to spit messages to stderr.
fn init_logger(verbosity: u8) -> Result<()> {
let mut builder = ConfigBuilder::new();
// Shut a bunch of stuff off - we're just spitting to stderr.
builder.set_location_level(LevelFilter::Trace);
builder.set_target_level(LevelFilter::Off);
builder.set_thread_level(LevelFilter::Off);
builder.set_time_level(LevelFilter::Off);

let level = match verbosity {
0 | 1 => LevelFilter::Info,
2 => LevelFilter::Debug,
_ => LevelFilter::Trace,
};

let config = builder.build();

TermLogger::init(level, config.clone(), TerminalMode::Stderr)
.or_else(|_| SimpleLogger::init(level, config))
.context("Couldn't init logger")
}

fn find_first_flt() -> Result<Option<PathBuf>> {
for f in fs::read_dir(env::current_dir()?)? {
let entry = f?;
Expand Down
5 changes: 1 addition & 4 deletions flt2vhs/Cargo.toml
Expand Up @@ -10,12 +10,9 @@ edition = "2018"
anyhow = "1.0"
humansize = "1.0"
log = "0.4"
logsetup = { version = "0.1", path = "../logsetup" }
memmap = "0.7"
rayon = "1.4"
serde = "1.0"
serde_derive = "1.0"
structopt = "0.3.8"

[dependencies.simplelog]
version = "0.7"
features = ["test"]

0 comments on commit 37f996c

Please sign in to comment.