rusty_bubbletea/
logging.rs1use std::fs::{File, OpenOptions};
11use std::io::Write;
12
13pub struct FileLogger {
15 file: File,
16 prefix: String,
17}
18
19impl FileLogger {
20 pub fn new(path: &str, prefix: &str) -> Result<Self, std::io::Error> {
22 let file = OpenOptions::new().create(true).append(true).open(path)?;
23 let mut pref = prefix.to_string();
24 if !pref.is_empty() && !pref.ends_with(' ') {
25 pref.push(' ');
26 }
27 Ok(Self { file, prefix: pref })
28 }
29
30 pub fn log(&mut self, s: &str) {
32 let _ = writeln!(self.file, "{}{}", self.prefix, s);
33 }
34}
35
36pub fn log_to_file(path: &str, prefix: &str) -> Result<FileLogger, std::io::Error> {
38 FileLogger::new(path, prefix)
39}