diff --git a/src/daemon.rs b/src/daemon.rs index 854b5d6..757968e 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -640,8 +640,16 @@ pub fn daemon_init(settings: Settings, config: Config) -> Arc> { } // Make a cpu struct for each cpu listed - for cpu in list_cpus() { - daemon.cpus.push(cpu); + match list_cpus() { + Ok(cpus) => { + for cpu in cpus { + daemon.cpus.push(cpu); + } + } + Err(e) => daemon.logger.log( + &format!("Failed to read from CPUs: {:?}", e), + logger::Severity::Error, + ), } let daemon_mutex = Arc::new(Mutex::new(daemon)); diff --git a/src/interface.rs b/src/interface.rs index 496a0fe..4ac4839 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -141,8 +141,15 @@ pub trait Getter { impl Getter for Get { fn freq(&self, raw: bool) { - let f = check_cpu_freq(&list_cpus()); - print_freq(f, raw); + match list_cpus() { + Ok(cpus) => { + let f = check_cpu_freq(&cpus); + print_freq(f, raw); + } + Err(e) => { + eprint!("Failed to get cpu information, an error occured: {:?}", e); + } + } } fn power(&self, raw: bool) { @@ -230,26 +237,63 @@ impl Getter for Get { } fn cpus(&self, raw: bool) { - let cpus = list_cpus(); - match check_cpu_name() { - Ok(name) => print_cpus(cpus, name, raw), - Err(_) => println!("Failed get list of cpus"), - }; + let cpus_result = list_cpus(); + match cpus_result { + Ok(cpus) => { + match check_cpu_name() { + Ok(name) => print_cpus(cpus, name, raw), + Err(_) => println!("Failed get list of cpus"), + }; + } + Err(e) => { + eprintln!("Failed to get cpu information, an error occured: {:?}", e); + } + } } fn speeds(&self, raw: bool) { - let speeds = list_cpu_speeds(); - print_cpu_speeds(speeds, raw); + let speeds_result = list_cpu_speeds(); + match speeds_result { + Ok(speeds) => { + print_cpu_speeds(speeds, raw); + } + Err(e) => { + eprintln!( + "Failed to get cpu speed information, an error occured: {:?}", + e + ); + } + } } fn temp(&self, raw: bool) { - let cpu_temp = list_cpu_temp(); - print_cpu_temp(cpu_temp, raw); + let cpu_temp_result = list_cpu_temp(); + match cpu_temp_result { + Ok(cpu_temp) => { + print_cpu_temp(cpu_temp, raw); + } + Err(e) => { + eprintln!( + "Failed to get cpu temperature information, an error occured: {:?}", + e + ); + } + } } fn govs(&self, raw: bool) { - let govs = list_cpu_governors(); - print_cpu_governors(govs, raw); + let govs_result = list_cpu_governors(); + match govs_result { + Ok(govs) => { + print_cpu_governors(govs, raw); + } + Err(e) => { + eprintln!( + "Failed to get cpu governor information, an error occured: {:?}", + e + ); + } + } } fn bat_cond(&self, raw: bool) { diff --git a/src/system.rs b/src/system.rs index e4d9b62..e7138db 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,4 +1,3 @@ -use cached::proc_macro::once; use std::fs::{self, read_dir}; use std::path::Path; use std::string::String; @@ -182,8 +181,7 @@ pub fn check_available_governors() -> Result, Error> { } /// Get all the cpus (cores), returns cpus from 0 to the (amount of cores -1) the machine has -#[once] -pub fn list_cpus() -> Vec { +pub fn list_cpus() -> Result, Error> { let mut cpus: Vec = Vec::::new(); // Get each item in the cpu directory @@ -228,30 +226,30 @@ pub fn list_cpus() -> Vec { gov: "Unknown".to_string(), }; - new.init_cpu().unwrap(); + new.init_cpu()?; - new.update().unwrap(); + new.update()?; to_return.push(new) } to_return.sort_by(|a, b| a.number.cmp(&b.number)); - to_return + Ok(to_return) } /// Get a vector of speeds reported from each cpu from list_cpus -pub fn list_cpu_speeds() -> Vec { - list_cpus().into_iter().map(|x| x.cur_freq).collect() +pub fn list_cpu_speeds() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.cur_freq).collect()) } /// Get a vector of temperatures reported from each cpu from list_cpus -pub fn list_cpu_temp() -> Vec { - list_cpus().into_iter().map(|x| x.cur_temp).collect() +pub fn list_cpu_temp() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.cur_temp).collect()) } /// Get a vector of the governors that the cpus from list_cpus -pub fn list_cpu_governors() -> Vec { - list_cpus().into_iter().map(|x| x.gov).collect() +pub fn list_cpu_governors() -> Result, Error> { + Ok(list_cpus()?.into_iter().map(|x| x.gov).collect()) } pub fn read_int(path: &str) -> Result { @@ -282,7 +280,7 @@ mod tests { #[test] fn check_cpu_freq_acs_test() { - assert!(check_cpu_freq(&list_cpus()) > 0.0); + assert!(check_cpu_freq(&list_cpus().unwrap()) > 0.0); } #[test] @@ -521,9 +519,9 @@ microcode : 0xea #[test] fn list_cpus_acs_test() { - assert_eq!(type_of(list_cpus()), type_of(Vec::::new())); + assert_eq!(type_of(list_cpus().unwrap()), type_of(Vec::::new())); - for x in list_cpus() { + for x in list_cpus().unwrap() { assert!(!x.name.is_empty()); assert!(x.max_freq > 0); assert!(x.min_freq > 0); @@ -538,9 +536,12 @@ microcode : 0xea #[test] fn list_cpu_speeds_acs_test() -> Result<(), Error> { // Type check - assert_eq!(type_of(list_cpu_speeds()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_speeds().unwrap()), + type_of(Vec::::new()) + ); - for x in list_cpu_speeds() { + for x in list_cpu_speeds().unwrap() { assert!(x > 0); } Ok(()) @@ -549,9 +550,12 @@ microcode : 0xea #[test] fn list_cpu_temp_acs_test() { // Type check - assert_eq!(type_of(list_cpu_temp()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_temp().unwrap()), + type_of(Vec::::new()) + ); - for x in list_cpu_temp() { + for x in list_cpu_temp().unwrap() { assert!(x > -100); } } @@ -559,9 +563,12 @@ microcode : 0xea #[test] fn list_cpu_governors_acs_test() { // Type check - assert_eq!(type_of(list_cpu_governors()), type_of(Vec::::new())); + assert_eq!( + type_of(list_cpu_governors().unwrap()), + type_of(Vec::::new()) + ); - for x in list_cpu_governors() { + for x in list_cpu_governors().unwrap() { assert!(x == "powersave" || x == "performance" || x == "schedutil"); } }