Skip to content

Instantly share code, notes, and snippets.

@sohnryang
Created December 21, 2021 22:50
Show Gist options
  • Save sohnryang/63fd8844dc176e3c63d8766986f8309b to your computer and use it in GitHub Desktop.
Save sohnryang/63fd8844dc176e3c63d8766986f8309b to your computer and use it in GitHub Desktop.
Power logger written in Rust
use std::{
env::args,
fs::{read_to_string, File},
io::Write,
thread::sleep,
time::Duration,
};
fn main() -> std::io::Result<()> {
let args: Vec<String> = args().collect();
let mut outfile = File::create(args[1].clone())?;
outfile.write_all(b"voltage,current\n")?;
loop {
let voltage = read_to_string("/sys/class/power_supply/BAT0/voltage_now")
.unwrap_or_default()
.trim()
.parse::<i32>()
.unwrap_or(-1);
let current = read_to_string("/sys/class/power_supply/BAT0/current_now")
.unwrap_or_default()
.trim()
.parse::<i32>()
.unwrap_or(-1);
outfile.write_fmt(format_args!("{},{}\n", voltage, current))?;
sleep(Duration::from_secs(1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment