Skip to content

Commit

Permalink
feat: using log crate
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Jan 28, 2024
1 parent e9d3366 commit f3c263c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 5 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Config {
}
}

pub fn parse(config_path: String) -> Result<Self, Box<dyn error::Error>> {
pub fn parse(config_path: String) -> Result<Self, Box<dyn std::error::Error>> {
let content = fs::read_to_string(config_path)?;
let config: Config = toml::from_str(&content)?;

Expand All @@ -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
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::Utc;
use clap::Parser;
use log::{debug, error, info, warn, LevelFilter};
use notify_rust::NotificationHandle;
use std::{
thread,
Expand All @@ -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 {
Expand All @@ -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
);

Expand All @@ -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
Expand All @@ -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
);

Expand All @@ -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
);
}
Expand Down
9 changes: 4 additions & 5 deletions src/notify.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -69,18 +70,16 @@ 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);
while sl.voice_count() > 0 {
thread::sleep(time::Duration::from_millis(500));
}
}
Err(error) => println!(
Err(error) => error!(
"[ERROR] soloud instance couldn't be correctly initialized: {}",
error.to_string()
),
Expand Down

0 comments on commit f3c263c

Please sign in to comment.