Skip to main content

rusty_bubbletea/
logging.rs

1//! Cleanroom Rust port of upstream Go source file: `logging.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Logging Utilities
6//!
7//! Logging helpers (`log_to_file`, `FileLogger`) for logging to file without corrupting the TUI.
8//! </public-docs>
9
10use std::fs::{File, OpenOptions};
11use std::io::Write;
12
13/// FileLogger utility struct.
14pub struct FileLogger {
15    file: File,
16    prefix: String,
17}
18
19impl FileLogger {
20    /// Creates a new FileLogger.
21    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    /// Logs a string line.
31    pub fn log(&mut self, s: &str) {
32        let _ = writeln!(self.file, "{}{}", self.prefix, s);
33    }
34}
35
36/// Helper function to log to a file.
37pub fn log_to_file(path: &str, prefix: &str) -> Result<FileLogger, std::io::Error> {
38    FileLogger::new(path, prefix)
39}