Skip to content

Commit

Permalink
add an option to change the log level
Browse files Browse the repository at this point in the history
  • Loading branch information
jannikac committed Nov 28, 2024
1 parent a099e22 commit 1e35be2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -38,8 +38,8 @@ impl usb::UsbCallback for App {
}

impl App {
pub fn new() -> Result<Self> {
logging::init_logging().context("failed to initialize logging")?;
pub fn new(args: Args) -> Result<Self> {
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"),
Expand Down
17 changes: 9 additions & 8 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?)?,
),
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(())
}

0 comments on commit 1e35be2

Please # to comment.