Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to override log level #47

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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