From 6f26a98b37105c1ca61d0e70f6cc71d55255561f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 May 2025 03:46:22 +0300 Subject: [PATCH] cli: improve debug cmd output --- src/cli/debug.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++-- src/conflict.rs | 8 ++++- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/cli/debug.rs b/src/cli/debug.rs index 3e5ac54..c34565d 100644 --- a/src/cli/debug.rs +++ b/src/cli/debug.rs @@ -4,6 +4,8 @@ use crate::cpu; use crate::monitor; use std::error::Error; use std::fs; +use std::process::{Command, Stdio}; +use std::time::{Duration, SystemTime}; /// Prints comprehensive debug information about the system pub fn run_debug(config: &AppConfig) -> Result<(), Box> { @@ -11,8 +13,27 @@ pub fn run_debug(config: &AppConfig) -> Result<(), Box> { println!("Version: {}", env!("CARGO_PKG_VERSION")); // Current date and time - let now = std::time::SystemTime::now(); - println!("Timestamp: {now:?}"); + let now = SystemTime::now(); + let formatted_time = chrono::Local::now().format("%Y-%m-%d %H:%M:%S"); + println!("Timestamp: {formatted_time}"); + + // Kernel information + if let Ok(kernel_info) = get_kernel_info() { + println!("Kernel Version: {kernel_info}"); + } else { + println!("Kernel Version: Unable to determine"); + } + + // System uptime + if let Ok(uptime) = get_system_uptime() { + println!( + "System Uptime: {} hours, {} minutes", + uptime.as_secs() / 3600, + (uptime.as_secs() % 3600) / 60 + ); + } else { + println!("System Uptime: Unable to determine"); + } // Get system information and conflicts match monitor::collect_system_report(config) { @@ -28,6 +49,26 @@ pub fn run_debug(config: &AppConfig) -> Result<(), Box> { println!("\n--- CONFIGURATION ---"); println!("Current Configuration: {config:#?}"); + // Print important sysfs paths and whether they exist + println!("\n--- SYSFS PATHS ---"); + check_and_print_sysfs_path( + "/sys/devices/system/cpu/intel_pstate/no_turbo", + "Intel P-State Turbo Control", + ); + check_and_print_sysfs_path( + "/sys/devices/system/cpu/cpufreq/boost", + "Generic CPU Boost Control", + ); + check_and_print_sysfs_path( + "/sys/devices/system/cpu/amd_pstate/cpufreq/boost", + "AMD P-State Boost Control", + ); + check_and_print_sysfs_path( + "/sys/firmware/acpi/platform_profile", + "ACPI Platform Profile Control", + ); + check_and_print_sysfs_path("/sys/class/power_supply", "Power Supply Information"); + println!("\n--- CPU INFORMATION ---"); println!("Current Governor: {:?}", report.cpu_global.current_governor); println!( @@ -159,8 +200,57 @@ pub fn run_debug(config: &AppConfig) -> Result<(), Box> { let daemon_status = fs::metadata("/var/run/superfreq.pid").is_ok(); println!("Daemon Running: {daemon_status}"); + // Check for systemd service status + if let Ok(systemd_status) = is_systemd_service_active("superfreq") { + println!("Systemd Service Active: {systemd_status}"); + } + Ok(()) } Err(e) => Err(Box::new(e) as Box), } } + +/// Get kernel version information +fn get_kernel_info() -> Result> { + let output = Command::new("uname").arg("-r").output()?; + + let kernel_version = String::from_utf8(output.stdout)?; + Ok(kernel_version.trim().to_string()) +} + +/// Get system uptime +fn get_system_uptime() -> Result> { + let uptime_str = fs::read_to_string("/proc/uptime")?; + let uptime_secs = uptime_str + .split_whitespace() + .next() + .ok_or("Invalid uptime format")? + .parse::()?; + + Ok(Duration::from_secs_f64(uptime_secs)) +} + +/// Check if a sysfs path exists and print its status +fn check_and_print_sysfs_path(path: &str, description: &str) { + let exists = std::path::Path::new(path).exists(); + println!( + "{}: {} ({})", + description, + path, + if exists { "Exists" } else { "Not Found" } + ); +} + +/// Check if a systemd service is active +fn is_systemd_service_active(service_name: &str) -> Result> { + let output = Command::new("systemctl") + .arg("is-active") + .arg(format!("{}.service", service_name)) + .stdout(Stdio::piped()) // capture stdout instead of letting it print + .stderr(Stdio::null()) // redirect stderr to null + .output()?; + + let status = String::from_utf8(output.stdout)?; + Ok(status.trim() == "active") +} diff --git a/src/conflict.rs b/src/conflict.rs index fe08e70..15bf773 100644 --- a/src/conflict.rs +++ b/src/conflict.rs @@ -1,5 +1,5 @@ use std::path::Path; -use std::process::Command; +use std::process::{Command, Stdio}; /// Represents detected conflicts with other power management services #[derive(Debug)] @@ -62,6 +62,8 @@ fn systemctl_exists() -> bool { Command::new("sh") .arg("-c") .arg("command -v systemctl") + .stdout(Stdio::null()) + .stderr(Stdio::null()) .status() .is_ok_and(|status| status.success()) } @@ -78,6 +80,8 @@ fn is_service_active(service: &str) -> bool { .arg("--quiet") .arg("is-active") .arg(service) + .stdout(Stdio::null()) + .stderr(Stdio::null()) .status() .is_ok_and(|status| status.success()) } @@ -118,6 +122,8 @@ pub fn detect_conflicts() -> ConflictDetection { && Command::new("sh") .arg("-c") .arg("tlp-stat -s 2>/dev/null | grep -q 'TLP power save = enabled'") + .stdout(Stdio::null()) + .stderr(Stdio::null()) .status() .is_ok_and(|status| status.success()) {