Clean up logging

This commit is contained in:
Vicki Pfau 2024-03-19 17:53:36 -07:00
parent 4a4b021ea5
commit 96efd91228
4 changed files with 70 additions and 26 deletions

38
Cargo.lock generated
View file

@ -541,6 +541,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.153" version = "0.2.153"
@ -841,6 +847,15 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"
@ -881,6 +896,8 @@ version = "24.3.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"tokio", "tokio",
"tracing",
"tracing-subscriber",
"zbus", "zbus",
] ]
@ -919,6 +936,16 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.33.0" version = "1.33.0"
@ -997,6 +1024,17 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.16.0" version = "1.16.0"

View file

@ -9,4 +9,6 @@ strip="symbols"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
tokio = { version = "1", features = ["fs", "process", "io-std", "rt-multi-thread", "macros"] } tokio = { version = "1", features = ["fs", "process", "io-std", "rt-multi-thread", "macros"] }
tracing = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
zbus = { version = "4", features = ["tokio"] } zbus = { version = "4", features = ["tokio"] }

View file

@ -24,6 +24,7 @@
*/ */
use anyhow::Result; use anyhow::Result;
use tracing_subscriber;
use zbus::ConnectionBuilder; use zbus::ConnectionBuilder;
pub mod manager; pub mod manager;
@ -33,6 +34,8 @@ async fn main() -> Result<()> {
// This daemon is responsible for creating a dbus api that steam client can use to do various OS // This daemon is responsible for creating a dbus api that steam client can use to do various OS
// level things. It implements com.steampowered.SteamOSManager1 interface // level things. It implements com.steampowered.SteamOSManager1 interface
tracing_subscriber::fmt::init();
let manager = manager::SMManager::new()?; let manager = manager::SMManager::new()?;
let _system_connection = ConnectionBuilder::system()? let _system_connection = ConnectionBuilder::system()?

View file

@ -26,6 +26,7 @@
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use std::{ffi::OsStr, fmt, fs}; use std::{ffi::OsStr, fmt, fs};
use tokio::{fs::File, io::AsyncWriteExt, process::Command}; use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use tracing::{error, warn};
use zbus::{interface, zvariant::Fd}; use zbus::{interface, zvariant::Fd};
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
@ -105,7 +106,7 @@ async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) ->
// Return false on failure, but also print an error if needed // Return false on failure, but also print an error if needed
script_exit_code(executable, args) script_exit_code(executable, args)
.await .await
.inspect_err(|message| println!("Error running {name} {message}")) .inspect_err(|message| warn!("Error running {name} {message}"))
} }
async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> { async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
@ -144,12 +145,12 @@ async fn restart_iwd() -> Result<bool> {
run_script("restart iwd", "systemctl", &["restart", "iwd"]).await run_script("restart iwd", "systemctl", &["restart", "iwd"]).await
} else { } else {
// reload failed // reload failed
println!("restart_iwd: reload systemd failed somehow"); error!("restart_iwd: reload systemd failed with non-zero exit code");
Ok(false) Ok(false)
} }
} }
Err(message) => { Err(message) => {
println!("restart_iwd: reload systemd got an error {message}"); error!("restart_iwd: reload systemd got an error: {message}");
Err(message) Err(message)
} }
} }
@ -197,12 +198,12 @@ async fn set_gpu_performance_level(level: i32) -> Result<()> {
let mut myfile = File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level") let mut myfile = File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level")
.await .await
.inspect_err(|message| println!("Error opening sysfs file for writing {message}"))?; .inspect_err(|message| error!("Error opening sysfs file for writing: {message}"))?;
myfile myfile
.write_all(levels[level as usize].as_bytes()) .write_all(levels[level as usize].as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?; .inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
Ok(()) Ok(())
} }
@ -215,24 +216,24 @@ async fn set_gpu_clocks(clocks: i32) -> Result<()> {
let mut myfile = File::create("/sys/class/drm/card0/device/pp_od_clk_voltage") let mut myfile = File::create("/sys/class/drm/card0/device/pp_od_clk_voltage")
.await .await
.inspect_err(|message| println!("Error opening sysfs file for writing {message}"))?; .inspect_err(|message| error!("Error opening sysfs file for writing: {message}"))?;
let data = format!("s 0 {clocks}\n"); let data = format!("s 0 {clocks}\n");
myfile myfile
.write(data.as_bytes()) .write(data.as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?; .inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
let data = format!("s 1 {clocks}\n"); let data = format!("s 1 {clocks}\n");
myfile myfile
.write(data.as_bytes()) .write(data.as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?; .inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
myfile myfile
.write("c\n".as_bytes()) .write("c\n".as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?; .inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
Ok(()) Ok(())
} }
@ -246,13 +247,13 @@ async fn set_tdp_limit(limit: i32) -> Result<()> {
let mut power1file = File::create("/sys/class/hwmon/hwmon5/power1_cap") let mut power1file = File::create("/sys/class/hwmon/hwmon5/power1_cap")
.await .await
.inspect_err(|message| { .inspect_err(|message| {
println!("Error opening sysfs power1_cap file for writing TDP limits {message}") error!("Error opening sysfs power1_cap file for writing TDP limits {message}")
})?; })?;
let mut power2file = File::create("/sys/class/hwmon/hwmon5/power2_cap") let mut power2file = File::create("/sys/class/hwmon/hwmon5/power2_cap")
.await .await
.inspect_err(|message| { .inspect_err(|message| {
println!("Error opening sysfs power2_cap file for wtriting TDP limits {message}") error!("Error opening sysfs power2_cap file for wtriting TDP limits {message}")
})?; })?;
// Now write the value * 1,000,000 // Now write the value * 1,000,000
@ -260,11 +261,11 @@ async fn set_tdp_limit(limit: i32) -> Result<()> {
power1file power1file
.write(data.as_bytes()) .write(data.as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to power1_cap file: {message}"))?; .inspect_err(|message| error!("Error writing to power1_cap file: {message}"))?;
power2file power2file
.write(data.as_bytes()) .write(data.as_bytes())
.await .await
.inspect_err(|message| println!("Error writing to power2_cap file: {message}"))?; .inspect_err(|message| error!("Error writing to power2_cap file: {message}"))?;
Ok(()) Ok(())
} }
@ -333,7 +334,7 @@ impl SMManager {
match result { match result {
Ok(as_string) => as_string.trim().parse().unwrap_or(-1.0), Ok(as_string) => as_string.trim().parse().unwrap_or(-1.0),
Err(message) => { Err(message) => {
println!("Unable to run als calibration script: {}", message); error!("Unable to run als calibration script: {}", message);
-1.0 -1.0
} }
} }
@ -406,7 +407,7 @@ impl SMManager {
match result { match result {
Ok(f) => Ok(Fd::Owned(std::os::fd::OwnedFd::from(f))), Ok(f) => Ok(Fd::Owned(std::os::fd::OwnedFd::from(f))),
Err(message) => { Err(message) => {
println!("Error opening sysfs file for giving file descriptor {message}"); error!("Error opening sysfs file for giving file descriptor: {message}");
Err(zbus::fdo::Error::IOError(message.to_string())) Err(zbus::fdo::Error::IOError(message.to_string()))
} }
} }
@ -430,24 +431,24 @@ impl SMManager {
let result = match stop_tracing(self.should_trace).await { let result = match stop_tracing(self.should_trace).await {
Ok(result) => result, Ok(result) => result,
Err(message) => { Err(message) => {
println!("stop_tracing command had an error {message}"); error!("stop_tracing command got an error: {message}");
return false; return false;
} }
}; };
if !result { if !result {
println!("stop_tracing command failed somehow, bailing"); error!("stop_tracing command returned non-zero");
return false; return false;
} }
// Stop_tracing was successful // Stop_tracing was successful
if let Err(message) = setup_iwd_config(false).await { if let Err(message) = setup_iwd_config(false).await {
println!("setup_iwd_config false got an error somehow {message}"); error!("setup_iwd_config false got an error: {message}");
return false; return false;
} }
// setup_iwd_config false worked // setup_iwd_config false worked
let value = match restart_iwd().await { let value = match restart_iwd().await {
Ok(value) => value, Ok(value) => value,
Err(message) => { Err(message) => {
println!("restart_iwd got an error {message}"); error!("restart_iwd got an error: {message}");
return false; return false;
} }
}; };
@ -456,7 +457,7 @@ impl SMManager {
self.wifi_debug_mode = WifiDebugMode::Off; self.wifi_debug_mode = WifiDebugMode::Off;
} else { } else {
// restart_iwd failed // restart_iwd failed
println!("restart_iwd failed somehow, check log above"); error!("restart_iwd failed, check log above");
return false; return false;
} }
} }
@ -467,26 +468,26 @@ impl SMManager {
} }
if let Err(message) = setup_iwd_config(true).await { if let Err(message) = setup_iwd_config(true).await {
println!("setup_iwd_config true got an error somehow {message}"); error!("setup_iwd_config true got an error: {message}");
return false; return false;
} }
// setup_iwd_config worked // setup_iwd_config worked
let value = match restart_iwd().await { let value = match restart_iwd().await {
Ok(value) => value, Ok(value) => value,
Err(message) => { Err(message) => {
println!("restart_iwd got an error {message}"); error!("restart_iwd got an error: {message}");
return false; return false;
} }
}; };
if !value { if !value {
println!("restart_iwd failed somehow"); error!("restart_iwd failed");
return false; return false;
} }
// restart_iwd worked // restart_iwd worked
let value = match start_tracing(buffer_size, self.should_trace).await { let value = match start_tracing(buffer_size, self.should_trace).await {
Ok(value) => value, Ok(value) => value,
Err(message) => { Err(message) => {
println!("start_tracing got an error {message}"); error!("start_tracing got an error: {message}");
return false; return false;
} }
}; };
@ -495,13 +496,13 @@ impl SMManager {
self.wifi_debug_mode = WifiDebugMode::On; self.wifi_debug_mode = WifiDebugMode::On;
} else { } else {
// start_tracing failed // start_tracing failed
println!("start_tracing failed somehow"); error!("start_tracing failed");
return false; return false;
} }
} }
Err(_) => { Err(_) => {
// Invalid mode requested, more coming later, but add this catch-all for now // Invalid mode requested, more coming later, but add this catch-all for now
println!("Invalid wifi debug mode {mode} requested"); warn!("Invalid wifi debug mode {mode} requested");
return false; return false;
} }
} }