Skip to content

Commit

Permalink
WIP: use argument based logging configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Gerace <nickagerace@gmail.com>
  • Loading branch information
nickgerace committed Jan 10, 2025
1 parent ed2b330 commit 119d76d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ strip = true
[dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
clap = { version = "4.5", features = ["derive"] }
clap-verbosity-flag = "3.0.2"
env_logger = { version = "0.11", features = [ "humantime" ], default-features = false }
git2 = { version = "0.20", default-features = false }
log = "0.4"
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod tests {
use git2::Oid;
use git2::Repository;
use git2::Signature;
use log::LevelFilter;
use pretty_assertions::assert_eq;
use std::fs::File;
use std::path::{Path, PathBuf};
Expand All @@ -27,11 +26,6 @@ mod tests {
/// of nesting.
#[test]
fn scenario() -> anyhow::Result<()> {
env_logger::builder()
.is_test(true)
.filter_level(LevelFilter::Info)
.try_init()?;

// Temporary directory structure:
// └── root
// ├── one (repo)
Expand Down
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use crate::config::{ColorMode, Config, DisplayMode};
use crate::display::DisplayHarness;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use gfold::RepositoryCollector;

use clap::Parser;
use env_logger::Builder;
use log::{debug, LevelFilter};
use std::{env, path::PathBuf};

Expand Down Expand Up @@ -46,20 +46,22 @@ struct Cli {
/// ignore config file settings
#[arg(short, long)]
pub ignore_config_file: bool,

#[command(flatten)]
verbose: Verbosity<InfoLevel>,
}

/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable, then
/// parses CLI arguments and generates a [`Config`](config::Config) by merging configurations as needed,
/// and finally collects results and displays them.
fn main() -> anyhow::Result<()> {
if env::var("RUST_LOG").is_err() {
Builder::new().filter_level(LevelFilter::Off).init();
} else {
env_logger::init();
}
let cli = Cli::parse();

env_logger::Builder::new()
.filter_level(cli.verbose.log_level_filter())
.init();
debug!("initialized logger");

let cli = Cli::parse();
let mut config = if cli.ignore_config_file {
Config::try_config_default()?
} else {
Expand Down

0 comments on commit 119d76d

Please sign in to comment.