Initial commit
This commit is contained in:
32
src/main.rs
Normal file
32
src/main.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user