From f3c263cdfcdcb899a1e4d165ba80f33ab4668806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Qui=C3=B1ones?= Date: Sun, 28 Jan 2024 13:04:03 -0500 Subject: [PATCH] feat: using log crate --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/config.rs | 8 +++++--- src/main.rs | 33 +++++++++++++++++++++------------ src/notify.rs | 9 ++++----- 5 files changed, 63 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 94866cb..93f824d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,7 +273,9 @@ dependencies = [ "anstyle", "chrono", "clap", + "env_logger", "linuxver", + "log", "notify-rust", "os_info", "serde", @@ -545,6 +547,29 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex 1.10.3", +] + +[[package]] +name = "env_logger" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e7cf40684ae96ade6232ed84582f40ce0a66efcd43a5117aef610534f8e0b8" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -727,6 +752,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "iana-time-zone" version = "0.1.59" diff --git a/Cargo.toml b/Cargo.toml index 939a275..f35f4ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,5 @@ soloud = { git = "https://github.com/MoAlyousef/soloud-rs" } toml = "0.8.8" notify-rust = "4.10.0" anstyle = "1.0.4" +log = "0.4.20" +env_logger = "0.11.1" diff --git a/src/config.rs b/src/config.rs index d5c495c..ed3116f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ +use log::{error, info, warn}; use serde::Deserialize; -use std::{env, error, fs, path::Path}; +use std::{env, fs, path::Path}; #[derive(Debug, Clone, Deserialize)] pub struct Bound { @@ -75,7 +76,7 @@ impl Config { } } - pub fn parse(config_path: String) -> Result> { + pub fn parse(config_path: String) -> Result> { let content = fs::read_to_string(config_path)?; let config: Config = toml::from_str(&content)?; @@ -86,7 +87,8 @@ impl Config { match Config::parse(config_path) { Ok(config) => config, Err(error) => { - eprintln!("[ERROR] error parsing config: {}", error); + error!("unable to parse user config: {}", error); + info!("default config will be used"); Config::default() // Provide a default value or handle it as needed } } diff --git a/src/main.rs b/src/main.rs index caf7505..1347008 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use chrono::Utc; use clap::Parser; +use log::{debug, error, info, warn, LevelFilter}; use notify_rust::NotificationHandle; use std::{ thread, @@ -18,13 +19,18 @@ mod battery; use battery::*; fn main() { + env_logger::builder() + .filter_level(LevelFilter::Debug) + .init(); + let args = cli::Args::parse(); + debug!("{:#?}", args); let cp = get_config_file(args.config_file); - println!("Config file path: {}", cp); + debug!("config file path is {}", cp); let config = Config::parse_or_default(cp); - println!("{:#?}", config); + debug!("{:#?}", config); // Calculates the notification level based on the provided battery capacity. let get_notification_level = |capacity: u8| -> BatteryNotificationLevel { @@ -51,12 +57,12 @@ fn main() { let capacity = psc.get_capacity(); let status = psc.get_status(); - println!("[DEBUG] Current capacity: {} Status: {}", capacity, status); + info!("current capacity: {} Status: {}", capacity, status); if status == "Charging" && last_notification_level != BatteryNotificationLevel::Charging { - println!("[DEBUG] Now the battery is charging..."); - println!( - "[DEBUG] The last notified capacity will be restarted to 0 (it was {})", + info!("now the battery is charging..."); + info!( + "the last notified capacity will be restarted to 0 (it was {})", last_notification_level ); @@ -66,7 +72,7 @@ fn main() { last_notification_handler.take().map(|h| h.close()); send_sound_notification(CHARGING_BATTERY_SOUND); } else { - println!("[WARNING] the app started with the computer plugged in, nothing to do"); + warn!("the app started with the computer plugged in, nothing to do"); } last_notification_level = BatteryNotificationLevel::Charging @@ -81,8 +87,8 @@ fn main() { _ => panic!("unexpected battery notification level"), }; - println!( - "[DEBUG] Last notification level: {}, Current notification level: {}", + debug!( + "last notification level: {}, current notification level: {}", last_notification_level, current_notification_level ); @@ -99,14 +105,17 @@ fn main() { if result.is_ok() { last_notification_handler = Some(result.unwrap()); } else { - println!("[ERROR] Battery notification: {:#?}", result.unwrap()) + error!( + "error sending desktop notification: {}", + result.unwrap_err() + ) } send_sound_notification(urgency.get_sound()); }; - println!( - "[DEBUG] Last notification level: {}, Current notification level: {}", + info!( + "last notification level: {}, current notification level: {}", last_notification_level, current_notification_level ); } diff --git a/src/notify.rs b/src/notify.rs index 5baa1ee..29cf953 100644 --- a/src/notify.rs +++ b/src/notify.rs @@ -1,3 +1,4 @@ +use log::{debug, error}; use notify_rust::{error, Hint, Notification, NotificationHandle}; use soloud::{audio::Wav, AudioExt, LoadExt, Soloud}; use std::{env, fmt, path::Path, thread, time}; @@ -69,10 +70,8 @@ pub fn send_sound_notification(sound: &[u8]) { let mut wav = Wav::default(); match wav.load_mem(sound) { - Ok(r) => println!("[DEBUG] Sound file has been loaded: {:#?}", r), - Err(error) => { - println!("[WARN] Couldn't load sound file: {}", error.to_string()) - } + Ok(r) => debug!("sound file has been loaded: {:#?}", r), + Err(error) => error!("couldn't load sound file: {}", error.to_string()), }; sl.play(&wav); @@ -80,7 +79,7 @@ pub fn send_sound_notification(sound: &[u8]) { thread::sleep(time::Duration::from_millis(500)); } } - Err(error) => println!( + Err(error) => error!( "[ERROR] soloud instance couldn't be correctly initialized: {}", error.to_string() ),