From 69bfa65d13332a2367302d8588bcd239cc456137 Mon Sep 17 00:00:00 2001 From: djairoh Date: Fri, 12 May 2023 12:58:45 +0200 Subject: [PATCH] updated logging to be configurable from the CLI. --- src/main.rs | 9 ++++++--- src/structs/cli.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 897d0a7..cdc3761 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ //! This file contains all driver code for the program. use core::time; +use std::ffi::OsString; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; @@ -54,15 +55,17 @@ fn default_loop(pf: &PlayerFinder, cfg: &Config, data: &mut Data, r: &Vec("RUST_LOG", cli.log_level.into()); if let Err(e) = env_logger::init() { error!("{e}"); return } - // Cli flags, Config, Data, and PlayerFinder initialisation - let cli = Cli::parse(); + // Config, Data, and PlayerFinder initialisation match confy::load::("polybar-now-playing", cli.config_file.as_str()) { Ok(cfg) => { let mut data: Data = Data::default(); diff --git a/src/structs/cli.rs b/src/structs/cli.rs index 41d4443..7d3b810 100644 --- a/src/structs/cli.rs +++ b/src/structs/cli.rs @@ -1,6 +1,31 @@ //! This file contains structs and functionality that are relevant to the Command Line Interface part of the program. +use std::ffi::OsString; + use clap::Parser; +/// Custom enum to define the desired loglevel during run-time. +#[derive(clap::ValueEnum, Clone)] +pub enum LogLevel { + TRACE, + DEBUG, + INFO, + WARN, + ERROR, +} + +/// Implement Into for LogLevel, so it can actually be used by env_logger. +impl Into for LogLevel { + fn into(self) -> OsString { + match self { + LogLevel::TRACE => "trace".into(), + LogLevel::DEBUG => "debug".into(), + LogLevel::INFO => "info".into(), + LogLevel::WARN => "warn".into(), + LogLevel::ERROR => "error".into(), + } + } +} + /// Program which finds the active mpris player and displays metadata about the playing piece of media. /// /// This program is intended to be used with polybar. @@ -15,4 +40,9 @@ pub struct Cli { /// This mode prints all active players to stdout, to allow one to find the appropriate player names to use in the config files. #[arg(short = 'l', long = "list")] pub list: bool, + /// Set log level. + /// + /// Sets the log level to print to stdout. + #[arg(long = "log", value_enum, default_value = "error")] + pub log_level: LogLevel } \ No newline at end of file