Skip to content

Commit

Permalink
feat: add error reporting contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Feb 13, 2025
1 parent 92ef370 commit aeb47ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ edition = "2021"
[profile.release]
lto = true
codegen-units = 1
strip = true

[dependencies]
anyhow = { version = "1.0.95", default-features = false, features = ["std", "backtrace"] }
Expand Down
67 changes: 48 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ fn main() -> anyhow::Result<()> {
let fan = Fan::new(&cl::PwmSettings {
filepath: pwm_path.to_owned(),
thresholds: fan::Thresholds::default(),
})?;
let rpm_path = fan.resolve_rpm_path()?;
let mut fan = fan.with_rpm_file(&rpm_path)?;
})
.context("Failed to setup fan")?;
let rpm_path = fan
.resolve_rpm_path()
.context("Failed to resolve fan rpm filepath")?;
let mut fan = fan
.with_rpm_file(&rpm_path)
.context("Failed to setup fan with rpm filepath")?;
log::info!("Testing fan {fan}, this may take a long time");
match fan.test() {
Ok(t) => {
Expand Down Expand Up @@ -95,25 +100,34 @@ fn main() -> anyhow::Result<()> {
let drives: Vec<Drive> = drive_paths
.iter()
.map(|path| Drive::new(path))
.collect::<anyhow::Result<_>>()?;
.collect::<anyhow::Result<_>>()
.context("Failed to setup drives")?;
let mut drive_probers: Vec<(Box<dyn DeviceTempProber>, bool)> = drives
.iter()
.zip(drive_paths.iter())
.map(|(drive, path)| {
probe::prober(drive, hddtemp_daemon_port)?.ok_or_else(|| {
anyhow::anyhow!("No probing method found for drive {path:?}")
})
probe::prober(drive, hddtemp_daemon_port)
.with_context(|| format!("Failed to setup prober for drive {drive}"))?
.ok_or_else(|| {
anyhow::anyhow!("No probing method found for drive {path:?}")
})
})
.collect::<anyhow::Result<_>>()?;
.collect::<anyhow::Result<_>>()
.context("Failed to setup drive probers")?;

let mut hwmon_and_range: Vec<(Hwmon, Range<Temp>)> = hwmons
.iter()
.map(|h| {
let hwm = Hwmon::new(&h.filepath)?;
let hwm = Hwmon::new(&h.filepath)
.with_context(|| format!("Failed to setup hwmon {:?}", h.filepath))?;
let range = h.temp.as_ref().map_or_else(
|| -> anyhow::Result<_> {
// Default range
let range = hwm.default_range()?;
let range = hwm.default_range().with_context(|| {
format!(
"Failed to compute default temperature range for hwmon {hwm}"
)
})?;
log::info!(
"Device temperature range set to {}-{}°C",
range.start,
Expand All @@ -127,13 +141,19 @@ fn main() -> anyhow::Result<()> {
})
.collect::<anyhow::Result<_>>()?;

let min_fan_speed = Speed::try_from(f64::from(min_fan_speed_prct) / 100.0)?;
let mut fans: Vec<_> = pwm.iter().map(Fan::new).collect::<anyhow::Result<_>>()?;
let min_fan_speed = Speed::try_from(f64::from(min_fan_speed_prct) / 100.0)
.with_context(|| format!("Invalid speed {min_fan_speed_prct}%"))?;
let mut fans: Vec<_> = pwm
.iter()
.map(Fan::new)
.collect::<anyhow::Result<_>>()
.context("Failed to setup fans")?;

let _exit_hook = ExitHook::new(
pwm.iter()
.map(|p| pwm::Pwm::new(&p.filepath))
.collect::<anyhow::Result<_>>()?,
.collect::<anyhow::Result<_>>()
.context("Failed to setup PWMs for exit hook")?,
restore_fan_settings,
)?;

Expand All @@ -145,7 +165,8 @@ fn main() -> anyhow::Result<()> {
ctrlc::set_handler(move || {
exit_requested.store(true, Ordering::SeqCst);
let _ = exit_tx.send(());
})?;
})
.context("Failed to setup SIGINT handler")?;
}

while !exit_requested.load(Ordering::SeqCst) {
Expand All @@ -155,27 +176,34 @@ fn main() -> anyhow::Result<()> {
.iter_mut()
.zip(drives.iter())
.map(|((prober, supports_probing_sleeping), drive)| {
let state = drive.state()?;
let state = drive
.state()
.with_context(|| format!("Failed to get drive {drive} state"))?;
log::debug!("Drive {drive} state: {state}");
let temp = if state.is_spun_down() && !*supports_probing_sleeping {
log::debug!("Drive {drive} is sleeping");
None
} else {
let temp = prober.probe_temp()?;
let temp = prober
.probe_temp()
.with_context(|| format!("Failed to get drive {drive} temp"))?;
log::debug!("Drive {drive}: {temp}°C");
Some(temp)
};
Ok(temp)
})
.collect::<anyhow::Result<Vec<_>>>()?
.collect::<anyhow::Result<Vec<_>>>()
.context("Failed to get maximum drive temperature")?
.into_iter()
.flatten()
.reduce(f64::max);

let hwmon_temps: Vec<Temp> = hwmon_and_range
.iter_mut()
.map(|(hwm, _range)| {
let temp = hwm.probe_temp()?;
let temp = hwm
.probe_temp()
.with_context(|| format!("Failed to get hwmon {hwm} temp"))?;
log::info!("Hwmon {hwm} temperature: {temp}°C");
Ok(temp)
})
Expand All @@ -194,7 +222,8 @@ fn main() -> anyhow::Result<()> {
speed = fan::target_speed(hwmon_temp, hwmon_range, speed);
}
for fan in &mut fans {
fan.set_speed(speed)?;
fan.set_speed(speed)
.with_context(|| format!("Failed to set fan {fan} speed"))?;
}

let elapsed = Instant::now().duration_since(start);
Expand Down

0 comments on commit aeb47ed

Please sign in to comment.