updated logging to be configurable from the CLI.

This commit is contained in:
Djairo Hougee 2023-05-12 12:58:45 +02:00
parent b53dacbe79
commit 69bfa65d13
2 changed files with 36 additions and 3 deletions

View File

@ -1,5 +1,6 @@
//! This file contains all driver code for the program. //! This file contains all driver code for the program.
use core::time; use core::time;
use std::ffi::OsString;
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::thread; use std::thread;
@ -54,15 +55,17 @@ fn default_loop(pf: &PlayerFinder, cfg: &Config, data: &mut Data, r: &Vec<String
/// Main function. Mostly concerned with initialisation. /// Main function. Mostly concerned with initialisation.
fn main() { fn main() {
// Parse cli flags
let cli = Cli::parse();
// logging initialisation // logging initialisation
std::env::set_var("RUST_LOG", "error"); std::env::set_var::<&str, OsString>("RUST_LOG", cli.log_level.into());
if let Err(e) = env_logger::init() { if let Err(e) = env_logger::init() {
error!("{e}"); error!("{e}");
return return
} }
// Cli flags, Config, Data, and PlayerFinder initialisation // Config, Data, and PlayerFinder initialisation
let cli = Cli::parse();
match confy::load::<Config>("polybar-now-playing", cli.config_file.as_str()) { match confy::load::<Config>("polybar-now-playing", cli.config_file.as_str()) {
Ok(cfg) => { Ok(cfg) => {
let mut data: Data = Data::default(); let mut data: Data = Data::default();

View File

@ -1,6 +1,31 @@
//! This file contains structs and functionality that are relevant to the Command Line Interface part of the program. //! 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; 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<OsString> for LogLevel, so it can actually be used by env_logger.
impl Into<OsString> 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. /// 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. /// 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. /// 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")] #[arg(short = 'l', long = "list")]
pub list: bool, 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
} }