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

Add clap as a CLI argument parser and add debug flag to change log level #154

Merged
merged 2 commits into from
Dec 1, 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
116 changes: 115 additions & 1 deletion 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 @@ ddc = "0.2"
ddc-hi = "0.4"
rusb = "^0.9"
shell-words = "1.0"
clap = { version = "4.5.21", features = ["derive"] }

[build-dependencies]
vergen-git2 = { version = "1.0.0", features = ["build", "cargo"] }
Expand Down
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
19 changes: 11 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extern crate log;

use anyhow::Result;
use std::env;
use clap::Parser;

#[cfg(target_os = "windows")]
use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS};
Expand All @@ -21,6 +21,14 @@ mod logging;
mod platform;
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
/// to see the log output when run from the command line.
fn attach_console() {
Expand All @@ -32,14 +40,9 @@ fn attach_console() {

fn main() -> Result<()> {
attach_console();
let args = Args::parse();

let args: Vec<String> = env::args().collect();
if args.len() == 2 && args[1] == "--version" {
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(());
}

let app = app::App::new()?;
let app = app::App::new(args)?;
app.run()?;
Ok(())
}