From 1e35be21cfa8dbe8b0abb75b30f1f7aa12d00e4e Mon Sep 17 00:00:00 2001 From: jannikac Date: Thu, 28 Nov 2024 20:35:48 +0100 Subject: [PATCH] add an option to change the log level --- src/app.rs | 6 +++--- src/logging.rs | 17 +++++++++-------- src/main.rs | 5 ++++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6dd7c07..d19c803 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,10 +6,10 @@ use anyhow::{Context, Result}; use crate::configuration::{Configuration, SwitchDirection}; -use crate::display_control; use crate::logging; use crate::platform::{wake_displays, PnPDetect}; use crate::usb; +use crate::{display_control, Args}; pub struct App { config: Configuration, @@ -38,8 +38,8 @@ impl usb::UsbCallback for App { } impl App { - pub fn new() -> Result { - logging::init_logging().context("failed to initialize logging")?; + pub fn new(args: Args) -> Result { + logging::init_logging(args.debug).context("failed to initialize logging")?; info!( "display-switch v{version} built on {timestamp} from git {git}", version = env!("CARGO_PKG_VERSION"), diff --git a/src/logging.rs b/src/logging.rs index 9b0df0a..3b2aeb8 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -10,16 +10,17 @@ use simplelog::*; use crate::configuration::Configuration; -pub fn init_logging() -> Result<()> { +pub fn init_logging(log_debug: bool) -> Result<()> { + let log_level = if log_debug { + LevelFilter::Debug + } else { + LevelFilter::Info + }; + Ok(CombinedLogger::init(vec![ - TermLogger::new( - LevelFilter::Debug, - Config::default(), - TerminalMode::Mixed, - ColorChoice::Auto, - ), + TermLogger::new(log_level, Config::default(), TerminalMode::Mixed, ColorChoice::Auto), WriteLogger::new( - LevelFilter::Debug, + log_level, Config::default(), File::create(Configuration::log_file_name()?)?, ), diff --git a/src/main.rs b/src/main.rs index 1eb3bdf..1c72010 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,9 @@ mod usb; #[derive(Parser, Debug)] #[command(version)] struct Args { + /// Print debug information + #[arg(short, long, default_value_t = false)] + debug: bool, } /// On Windows, re-attach the console, if parent process has the console. This allows @@ -39,7 +42,7 @@ fn main() -> Result<()> { attach_console(); let args = Args::parse(); - let app = app::App::new()?; + let app = app::App::new(args)?; app.run()?; Ok(()) }