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",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.153"
@ -841,6 +847,15 @@ dependencies = [
"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]]
name = "signal-hook-registry"
version = "1.4.1"
@ -881,6 +896,8 @@ version = "24.3.0"
dependencies = [
"anyhow",
"tokio",
"tracing",
"tracing-subscriber",
"zbus",
]
@ -919,6 +936,16 @@ dependencies = [
"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]]
name = "tokio"
version = "1.33.0"
@ -997,6 +1024,17 @@ dependencies = [
"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]]
name = "typenum"
version = "1.16.0"

View file

@ -9,4 +9,6 @@ strip="symbols"
[dependencies]
anyhow = "1"
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"] }

View file

@ -24,6 +24,7 @@
*/
use anyhow::Result;
use tracing_subscriber;
use zbus::ConnectionBuilder;
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
// level things. It implements com.steampowered.SteamOSManager1 interface
tracing_subscriber::fmt::init();
let manager = manager::SMManager::new()?;
let _system_connection = ConnectionBuilder::system()?

View file

@ -26,6 +26,7 @@
use anyhow::{Error, Result};
use std::{ffi::OsStr, fmt, fs};
use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use tracing::{error, warn};
use zbus::{interface, zvariant::Fd};
#[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
script_exit_code(executable, args)
.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> {
@ -144,12 +145,12 @@ async fn restart_iwd() -> Result<bool> {
run_script("restart iwd", "systemctl", &["restart", "iwd"]).await
} else {
// reload failed
println!("restart_iwd: reload systemd failed somehow");
error!("restart_iwd: reload systemd failed with non-zero exit code");
Ok(false)
}
}
Err(message) => {
println!("restart_iwd: reload systemd got an error {message}");
error!("restart_iwd: reload systemd got an error: {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")
.await
.inspect_err(|message| println!("Error opening sysfs file for writing {message}"))?;
.inspect_err(|message| error!("Error opening sysfs file for writing: {message}"))?;
myfile
.write_all(levels[level as usize].as_bytes())
.await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?;
.inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
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")
.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");
myfile
.write(data.as_bytes())
.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");
myfile
.write(data.as_bytes())
.await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?;
.inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
myfile
.write("c\n".as_bytes())
.await
.inspect_err(|message| println!("Error writing to sysfs file {message}"))?;
.inspect_err(|message| error!("Error writing to sysfs file: {message}"))?;
Ok(())
}
@ -246,13 +247,13 @@ async fn set_tdp_limit(limit: i32) -> Result<()> {
let mut power1file = File::create("/sys/class/hwmon/hwmon5/power1_cap")
.await
.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")
.await
.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
@ -260,11 +261,11 @@ async fn set_tdp_limit(limit: i32) -> Result<()> {
power1file
.write(data.as_bytes())
.await
.inspect_err(|message| println!("Error writing to power1_cap file: {message}"))?;
.inspect_err(|message| error!("Error writing to power1_cap file: {message}"))?;
power2file
.write(data.as_bytes())
.await
.inspect_err(|message| println!("Error writing to power2_cap file: {message}"))?;
.inspect_err(|message| error!("Error writing to power2_cap file: {message}"))?;
Ok(())
}
@ -333,7 +334,7 @@ impl SMManager {
match result {
Ok(as_string) => as_string.trim().parse().unwrap_or(-1.0),
Err(message) => {
println!("Unable to run als calibration script: {}", message);
error!("Unable to run als calibration script: {}", message);
-1.0
}
}
@ -406,7 +407,7 @@ impl SMManager {
match result {
Ok(f) => Ok(Fd::Owned(std::os::fd::OwnedFd::from(f))),
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()))
}
}
@ -430,24 +431,24 @@ impl SMManager {
let result = match stop_tracing(self.should_trace).await {
Ok(result) => result,
Err(message) => {
println!("stop_tracing command had an error {message}");
error!("stop_tracing command got an error: {message}");
return false;
}
};
if !result {
println!("stop_tracing command failed somehow, bailing");
error!("stop_tracing command returned non-zero");
return false;
}
// Stop_tracing was successful
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;
}
// setup_iwd_config false worked
let value = match restart_iwd().await {
Ok(value) => value,
Err(message) => {
println!("restart_iwd got an error {message}");
error!("restart_iwd got an error: {message}");
return false;
}
};
@ -456,7 +457,7 @@ impl SMManager {
self.wifi_debug_mode = WifiDebugMode::Off;
} else {
// restart_iwd failed
println!("restart_iwd failed somehow, check log above");
error!("restart_iwd failed, check log above");
return false;
}
}
@ -467,26 +468,26 @@ impl SMManager {
}
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;
}
// setup_iwd_config worked
let value = match restart_iwd().await {
Ok(value) => value,
Err(message) => {
println!("restart_iwd got an error {message}");
error!("restart_iwd got an error: {message}");
return false;
}
};
if !value {
println!("restart_iwd failed somehow");
error!("restart_iwd failed");
return false;
}
// restart_iwd worked
let value = match start_tracing(buffer_size, self.should_trace).await {
Ok(value) => value,
Err(message) => {
println!("start_tracing got an error {message}");
error!("start_tracing got an error: {message}");
return false;
}
};
@ -495,13 +496,13 @@ impl SMManager {
self.wifi_debug_mode = WifiDebugMode::On;
} else {
// start_tracing failed
println!("start_tracing failed somehow");
error!("start_tracing failed");
return false;
}
}
Err(_) => {
// 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;
}
}