Skip to content

Commit

Permalink
Add updated error handling for use of list_cpus()
Browse files Browse the repository at this point in the history
  • Loading branch information
Camerooooon committed Feb 22, 2024
1 parent 5979b6c commit c9ac547
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
15 changes: 13 additions & 2 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,19 @@ pub fn daemon_init(settings: Settings, config: Config) -> Arc<Mutex<Daemon>> {
}

// 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));
Expand Down
61 changes: 48 additions & 13 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -230,26 +237,54 @@ 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,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]
Expand Down

0 comments on commit c9ac547

Please sign in to comment.