Created
December 21, 2021 22:50
-
-
Save sohnryang/63fd8844dc176e3c63d8766986f8309b to your computer and use it in GitHub Desktop.
Power logger written in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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