33 lines
992 B
Rust
33 lines
992 B
Rust
use linux_battery::Battery;
|
|
|
|
use std::fs::OpenOptions;
|
|
use std::io::Write;
|
|
use std::time::{Duration, SystemTime};
|
|
use std::thread::sleep;
|
|
|
|
fn main() {
|
|
let mut battery = Battery::new("BAT0");
|
|
let mut log_file = OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open("batman.log")
|
|
.expect("can't open log file");
|
|
|
|
loop {
|
|
let sys_time = SystemTime::now();
|
|
match sys_time.duration_since(SystemTime::UNIX_EPOCH) {
|
|
Ok(n) => {
|
|
let mut log_entry = format!("{}\t{}", n.as_secs(), battery.state());
|
|
match battery.capacity() {
|
|
Some(cap) => log_entry = format!("{}\t{} %", log_entry, cap),
|
|
None => {},
|
|
}
|
|
log_entry = format!("{}\r\n", log_entry);
|
|
log_file.write_all(log_entry.as_bytes()).expect("write to file failed");
|
|
},
|
|
Err(_) => {},
|
|
}
|
|
sleep(Duration::new(2, 0));
|
|
}
|
|
}
|