Skip to content

Commit

Permalink
Merge pull request #47 from thomasschafer/tschafer-allow-users-to-ove…
Browse files Browse the repository at this point in the history
…rride-log-level

Allow users to override log level
  • Loading branch information
thomasschafer authored Nov 30, 2024
2 parents 68014d3 + f67b999 commit 1be6016
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ impl App {
.build();
let paths: Vec<_> = walker
.flatten()
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
.filter(|entry| entry.file_type().is_some_and(|ft| ft.is_file()))
.map(|entry| entry.path().to_path_buf())
.filter(|path| {
if self.ignore_file(path) {
Expand Down
7 changes: 4 additions & 3 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use log::info;
use log::{info, LevelFilter};
use std::path::{Path, PathBuf};

use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};

const APP_NAME: &str = "scooter";
pub const DEFAULT_LOG_LEVEL: &str = "error";

pub fn cache_dir() -> PathBuf {
let strategy = choose_base_strategy().expect("Error when finding cache directory");
Expand All @@ -24,11 +25,11 @@ fn make_parent_dir(path: &Path) {
}
}

pub fn setup_logging() -> anyhow::Result<()> {
pub fn setup_logging(level: LevelFilter) -> anyhow::Result<()> {
let log_path = default_log_file();
make_parent_dir(&log_path);

let _ = simple_log::file(log_path.to_str().unwrap(), "warn", 100, 10);
let _ = simple_log::file(log_path.to_str().unwrap(), level.as_str(), 100, 10);

info!("Logging initialized at {:?}", log_path);
Ok(())
Expand Down
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::Parser;
use logging::setup_logging;
use log::LevelFilter;
use logging::{setup_logging, DEFAULT_LOG_LEVEL};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::{io, str::FromStr};
use tui::Tui;
use utils::validate_directory;

Expand Down Expand Up @@ -29,11 +30,26 @@ struct Args {
/// Include hidden files and directories, such as those whose name starts with a dot (.)
#[arg(short = '.', long, default_value = "false")]
hidden: bool,

/// Log level (trace, debug, info, warn, error)
#[arg(
long,
value_parser = parse_log_level,
default_value = DEFAULT_LOG_LEVEL
)]
log_level: LevelFilter,
}

fn parse_log_level(s: &str) -> Result<LevelFilter, String> {
LevelFilter::from_str(s).map_err(|_| format!("Invalid log level: {}", s))
}

// In main(), update the logging setup:
#[tokio::main]
async fn main() -> anyhow::Result<()> {
setup_logging()?;
let args = Args::parse();

setup_logging(args.log_level)?;

let args = Args::parse();

Expand Down

0 comments on commit 1be6016

Please sign in to comment.