added logging to builder trait and implemented for compiler and

assembler crates.
This commit is contained in:
2026-02-23 09:04:30 +00:00
parent a1d7b54479
commit 7ab1ac8842
11 changed files with 137 additions and 60 deletions
+2
View File
@@ -49,4 +49,6 @@ pub trait Builder {
std::fs::write(path.as_ref(), output)
.map_err(|e| BuildError::IoError(e.to_string()))
}
fn logs(&self) -> Vec<String>;
}
+63 -2
View File
@@ -1,4 +1,65 @@
// TODO: Use an actual logging or tracing library for pretty (scoped) output.
pub fn log(message: &str) {
use std::sync::{Arc, mpsc};
pub fn info(message: &str) {
println!("\x1b[32mINFO:\x1b[0m {message}");
}
#[derive(Debug)]
pub struct LogReceiver {
logs_rx: mpsc::Receiver<String>,
sender: Logger,
use_stdio: bool,
}
#[derive(Debug, Clone)]
pub struct Logger {
use_stdio: bool,
logs_tx: Arc<mpsc::Sender<String>>,
}
impl Logger {
#[must_use]
pub fn new(logs_tx: mpsc::Sender<String>, use_stdio: bool) -> Self {
Self {
use_stdio: true,
logs_tx: Arc::new(logs_tx),
}
}
pub fn info(&self, message: &str) {
let res = format!("\x1b[32mINFO:\x1b[0m {message}");
if self.use_stdio {
println!("{res}");
}
self.logs_tx.send(res).expect("Failed to send log message");
}
}
impl LogReceiver {
#[must_use]
pub fn new(use_stdio: bool) -> Self {
let (logs_tx, logs_rx) = mpsc::channel();
Self {
use_stdio,
logs_rx,
sender: Logger::new(logs_tx, use_stdio),
}
}
#[must_use]
pub fn logs(&self) -> Vec<String> {
self.logs_rx.try_iter().collect()
}
#[must_use]
pub fn logger(&self) -> Logger {
self.sender.clone()
}
}
impl Default for LogReceiver {
fn default() -> Self {
Self::new(true)
}
}