libobs_wrapper\logger/
file.rs

1use std::{fs::File, path::Path};
2
3use chrono::Local;
4
5use super::ObsLogger;
6
7/// A logger that writes logs to a file
8#[derive(Debug)]
9pub struct FileLogger {
10    file: File,
11}
12
13impl FileLogger {
14    pub fn from_dir(dir: &Path) -> anyhow::Result<Self> {
15        let current_local = Local::now();
16        let custom_format = current_local.format("%Y-%m-%d-%H-%M-%S");
17
18        Ok(Self {
19            file: File::create(dir.join(format!("obs-{}.log", custom_format)))?,
20        })
21    }
22
23    pub fn from_file(file: &Path) -> anyhow::Result<Self> {
24        Ok(Self {
25            file: File::create(file)?,
26        })
27    }
28}
29
30impl ObsLogger for FileLogger {
31    fn log(&mut self, level: crate::enums::ObsLogLevel, msg: String) {
32        use std::io::Write;
33        writeln!(self.file, "[{:?}] {}", level, msg).unwrap();
34    }
35}