Skip to content

Commit

Permalink
Set errors during streaming to debug log level
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Sep 27, 2024
1 parent b40c317 commit 144fb23
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/legacy/cpu_brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, &'static str> {
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");
}
Expand Down Expand Up @@ -60,7 +60,7 @@ fn get_processor_info_linux(lib_path: &str) -> Result<String, &'static str> {
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_windows(lib_path: &str) -> Result<String, &'static str> {
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"),
Expand Down
10 changes: 5 additions & 5 deletions src/legacy/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand All @@ -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<HashMap<String, String>> {
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(_) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ fn linux_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
///
/// A `Vec` of `HashMap` containing the disk information.
fn darwin_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
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(_) => {
Expand All @@ -128,7 +128,7 @@ fn darwin_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
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(_) => {
Expand Down Expand Up @@ -183,7 +183,7 @@ fn reformat_windows(data: &mut HashMap<String, Value>) -> HashMap<String, String
/// A `Vec` of `HashMap` containing the disk information.
fn windows_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
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(_) => {
Expand Down
11 changes: 7 additions & 4 deletions src/resources/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = [].to_vec();
Expand All @@ -31,12 +31,13 @@ pub fn get_disk_stats() -> Value {
fn get_docker_stats() -> Result<Vec<serde_json::Value>, Box<dyn std::error::Error>> {
// 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,
Expand All @@ -50,7 +51,7 @@ fn get_docker_stats() -> Result<Vec<serde_json::Value>, Box<dyn std::error::Erro
return Ok(vec![]);
}
Err(err) => {
log::error!("Error checking containers: {}", err);
log::debug!("Error checking containers: {}", err);
return Ok(vec![]);
}
};
Expand Down Expand Up @@ -88,6 +89,8 @@ fn get_system_metrics() -> HashMap<String, serde_json::Value> {
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![
(
Expand Down
4 changes: 2 additions & 2 deletions src/resources/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand All @@ -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(_) => {
Expand Down
14 changes: 8 additions & 6 deletions src/squire/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
pub fn run_command(command: &str, args: &[&str], log: bool) -> Result<String, String> {
match Command::new(command)
.args(args)
.output()
Expand All @@ -108,8 +108,10 @@ pub fn run_command(command: &str, args: &[&str]) -> Result<String, String> {
} 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)
}
}
Expand Down

0 comments on commit 144fb23

Please sign in to comment.