Files
nes-emu/src/debug.rs

99 lines
2.3 KiB
Rust

// use std::num::NonZeroUsize;
#[derive(Debug, Clone)]
pub struct DebugLog {
enabled: bool,
current: String,
history: Vec<String>,
// max_history: Option<NonZeroUsize>,
// pos: usize,
}
impl DebugLog {
pub fn new() -> Self {
Self {
enabled: false,
current: String::new(),
history: vec![],
// max_history: None,
// pos: 0,
}
}
pub fn rotate(&mut self) {
// if self.current.len() > 500 {
let mut rot = std::mem::take(&mut self.current);
self.current = rot.split_off(rot.rfind('\n').unwrap_or(rot.len()));
// if let Some(max) = self.max_history {
// if self.history.len() < max.into() {
// self.history.extend(rot.lines().map(|s| s.to_owned()));
// } else {
// self.history[self.pos] = rot;
// self.pos = (self.pos + 1) % max.get();
// }
// } else {
// self.history.push(rot);
self.history.extend(rot.lines().map(|s| s.to_owned()));
// }
// }
}
// pub fn current(&self) -> &str {
// &self.current
// }
pub fn history(&self) -> &[String] {
&self.history[self.history.len().saturating_sub(100)..]
}
pub fn pop(&mut self) -> Option<String> {
self.history.pop()
}
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn enabled(&self) -> bool {
self.enabled
}
}
impl std::fmt::Write for DebugLog {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
if self.enabled {
let tmp = self.current.write_str(s);
self.rotate();
tmp
} else {
Ok(())
}
}
fn write_char(&mut self, c: char) -> std::fmt::Result {
if self.enabled {
let tmp = self.current.write_char(c);
self.rotate();
tmp
} else {
Ok(())
}
}
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::fmt::Result {
if self.enabled {
let tmp = self.current.write_fmt(args);
self.rotate();
tmp
} else {
Ok(())
}
}
}
impl Default for DebugLog {
fn default() -> Self {
Self::new()
}
}