35 lines
859 B
Rust
35 lines
859 B
Rust
use crate::logging::logger::{Logger, KeyState};
|
|
use std::fs::{File, OpenOptions};
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
use std::sync::Mutex;
|
|
|
|
pub struct ConsoleLogger;
|
|
impl Logger for ConsoleLogger {
|
|
fn log(&self, key: &KeyState) {
|
|
println!("{}", Logger::default_format(key))
|
|
}
|
|
}
|
|
|
|
pub struct FileLogger {
|
|
file: Mutex<File>,
|
|
}
|
|
|
|
impl FileLogger {
|
|
pub fn new(filename: &Path) -> Self {
|
|
FileLogger { file: Mutex::from(OpenOptions::new().create(true).write(true).append(true).open(&filename).unwrap()) }
|
|
}
|
|
|
|
pub fn new_default() -> Self {
|
|
let mut path = dirs::desktop_dir().unwrap();
|
|
path.push("log.txt");
|
|
Self::new(&path)
|
|
}
|
|
}
|
|
|
|
impl Logger for FileLogger {
|
|
fn log(&self, key: &KeyState) {
|
|
self.file.lock().unwrap().write_all(Logger::default_format(key).as_ref()).unwrap();
|
|
}
|
|
}
|