From 144fb2379094fc28536cb8ae17a480eb0a12ab45 Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Thu, 26 Sep 2024 23:47:57 -0500 Subject: [PATCH] Set errors during streaming to debug log level --- src/legacy/cpu_brand.rs | 4 ++-- src/legacy/disks.rs | 10 +++++----- src/resources/stream.rs | 11 +++++++---- src/resources/system.rs | 4 ++-- src/squire/util.rs | 14 ++++++++------ 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/legacy/cpu_brand.rs b/src/legacy/cpu_brand.rs index 2d1c3ec..4390806 100644 --- a/src/legacy/cpu_brand.rs +++ b/src/legacy/cpu_brand.rs @@ -13,7 +13,7 @@ use crate::squire; /// /// A `Option` containing the processor information if successful, otherwise `None`. fn get_processor_info_darwin(lib_path: &str) -> Result { - let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"]); + let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"], true); if result.is_err() { return Err("Failed to get processor info"); } @@ -60,7 +60,7 @@ fn get_processor_info_linux(lib_path: &str) -> Result { /// /// A `Option` containing the processor information if successful, otherwise `None`. fn get_processor_info_windows(lib_path: &str) -> Result { - let result = squire::util::run_command(lib_path, &["cpu", "get", "name"]); + let result = squire::util::run_command(lib_path, &["cpu", "get", "name"], true); let output = match result { Ok(output) => output, Err(_) => return Err("Failed to get processor info"), diff --git a/src/legacy/disks.rs b/src/legacy/disks.rs index 70eed3c..ff02159 100644 --- a/src/legacy/disks.rs +++ b/src/legacy/disks.rs @@ -47,7 +47,7 @@ fn parse_size(size_str: &str) -> String { /// /// A `bool` indicating if the disk is physical. fn is_physical_disk(lib_path: &str, device_id: &str) -> bool { - let result = squire::util::run_command(lib_path, &["info", device_id]); + let result = squire::util::run_command(lib_path, &["info", device_id], true); let output = match result { Ok(output) => output, Err(_) => { @@ -73,7 +73,7 @@ fn is_physical_disk(lib_path: &str, device_id: &str) -> bool { /// /// A `Vec` of `HashMap` containing the disk information. fn linux_disks(lib_path: &str) -> Vec> { - let result = squire::util::run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"]); + let result = squire::util::run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"], true); let output = match result { Ok(output) => output, Err(_) => { @@ -112,7 +112,7 @@ fn linux_disks(lib_path: &str) -> Vec> { /// /// A `Vec` of `HashMap` containing the disk information. fn darwin_disks(lib_path: &str) -> Vec> { - let result = squire::util::run_command(lib_path, &["list"]); + let result = squire::util::run_command(lib_path, &["list"], true); let output = match result { Ok(output) => output, Err(_) => { @@ -128,7 +128,7 @@ fn darwin_disks(lib_path: &str) -> Vec> { if !is_physical_disk(lib_path, device_id) { continue; } - let result = squire::util::run_command(lib_path, &["info", device_id]); + let result = squire::util::run_command(lib_path, &["info", device_id], true); let disk_info_output = match result { Ok(output) => output, Err(_) => { @@ -183,7 +183,7 @@ fn reformat_windows(data: &mut HashMap) -> HashMap Vec> { let ps_command = "Get-CimInstance Win32_DiskDrive | Select-Object Caption, DeviceID, Model, Partitions, Size | ConvertTo-Json"; - let result = squire::util::run_command(lib_path, &["-Command", ps_command]); + let result = squire::util::run_command(lib_path, &["-Command", ps_command], true); let output = match result { Ok(output) => output, Err(_) => { diff --git a/src/resources/stream.rs b/src/resources/stream.rs index 03d2a91..398be45 100644 --- a/src/resources/stream.rs +++ b/src/resources/stream.rs @@ -2,14 +2,14 @@ use std::collections::HashMap; use sysinfo::{CpuRefreshKind, Disks, RefreshKind, System}; use crate::{resources, squire}; -use serde_json::{self, Value}; +use serde_json; /// Function to get disk statistics. /// /// # Returns /// /// A `Value` object with total and used disk space. -pub fn get_disk_stats() -> Value { +pub fn get_disk_stats() -> serde_json::Value { let disks = Disks::new_with_refreshed_list(); let disks_total = resources::info::get_disk_usage(&disks); let mut disk_available: Vec = [].to_vec(); @@ -31,12 +31,13 @@ pub fn get_disk_stats() -> Value { fn get_docker_stats() -> Result, Box> { // Check if there are any docker containers running // `docker -a` will show all containers including stopped, which will block `docker stats` - let ps_result = squire::util::run_command("docker", &["ps", "-q"]); + let ps_result = squire::util::run_command("docker", &["ps", "-q"], false); let stats_result = match ps_result { Ok(output) if !output.is_empty() => { let stats_result = squire::util::run_command( "docker", &["stats", "--no-stream", "--format", "{{json .}}"], + false, ); match stats_result { Ok(stats) => stats, @@ -50,7 +51,7 @@ fn get_docker_stats() -> Result, Box { - log::error!("Error checking containers: {}", err); + log::debug!("Error checking containers: {}", err); return Ok(vec![]); } }; @@ -88,6 +89,8 @@ fn get_system_metrics() -> HashMap { let mut system = System::new_all(); system.refresh_all(); + // https://docs.rs/sysinfo/0.31.4/sysinfo/struct.System.html#method.load_average + // Currently this doesn't work on Windows let load_avg = System::load_average(); let mut hash_vec = vec![ ( diff --git a/src/resources/system.rs b/src/resources/system.rs index 89a0a5b..9a644a8 100644 --- a/src/resources/system.rs +++ b/src/resources/system.rs @@ -15,7 +15,7 @@ pub struct OperatingSystem { /// A string with the OS architecture. fn unamem() -> String { // Get architecture using `uname -m` with fallback - let result = squire::util::run_command("uname", &["-m"]); + let result = squire::util::run_command("uname", &["-m"], true); match result { Ok(output) => output.to_lowercase(), Err(_) => { @@ -32,7 +32,7 @@ fn unamem() -> String { /// A string with the OS name. fn unameu() -> String { // Get OS using `uname` - let result = squire::util::run_command("uname", &[]); + let result = squire::util::run_command("uname", &[], true); match result { Ok(output) => output.to_uppercase(), Err(_) => { diff --git a/src/squire/util.rs b/src/squire/util.rs index a14760a..f94a0a0 100644 --- a/src/squire/util.rs +++ b/src/squire/util.rs @@ -87,17 +87,17 @@ pub fn size_converter(byte_size: u64) -> String { format!("{:.2} {}", size, size_name[index]) } -/// Function to parse size string. +/// Function to run a terminal command. /// /// # Arguments /// -/// * `size_str` - The size string to parse -/// * `unit` - The unit to convert the size to +/// * `command` - Command to run +/// * `log` - Boolean flag to log errors /// /// # Returns /// /// A `String` containing the parsed size string. -pub fn run_command(command: &str, args: &[&str]) -> Result { +pub fn run_command(command: &str, args: &[&str], log: bool) -> Result { match Command::new(command) .args(args) .output() @@ -108,8 +108,10 @@ pub fn run_command(command: &str, args: &[&str]) -> Result { } else { let stderr = String::from_utf8_lossy(&output.stderr).to_string(); let exit_code = output.status.code().unwrap_or(-1); - log::error!("Command [{}] failed with exit code: {}", command, exit_code); - log::error!("Stderr: {}", stderr); + if log { + log::error!("Command [{}] failed with exit code: {}", command, exit_code); + log::error!("Stderr: {}", stderr); + } Err(stderr) } }