Split mgba log if too big (#646)

- [x] Changelog updated
This commit is contained in:
Corwin 2024-04-20 00:21:11 +01:00 committed by GitHub
commit 4419dfb0b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 15 deletions

View file

@ -28,6 +28,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixnums are now implemented with `num_traits` trait definitions. - Fixnums are now implemented with `num_traits` trait definitions.
- Rather than having our own sync with Statics, use the standard portable - Rather than having our own sync with Statics, use the standard portable
atomics crate. These are reexported for convenience. atomics crate. These are reexported for convenience.
- `Mgba` no longer implements `Write`. You're unlikely to notice as
`agb::println!` is unchanged.
- Writes of long messages to mgba are split over multiple log messages if they
overflow mgba's buffer. On a panic, only the final message will be Fatal with
the preceding ones (if needed) being Info.
## [0.19.1] - 2024/03/06 ## [0.19.1] - 2024/03/06

View file

@ -32,15 +32,13 @@ pub(crate) fn test_runner_measure_cycles() {
NUMBER_OF_CYCLES.set(0); NUMBER_OF_CYCLES.set(0);
} }
pub struct Mgba { pub struct Mgba {}
bytes_written: usize,
}
impl Mgba { impl Mgba {
#[must_use] #[must_use]
pub fn new() -> Option<Self> { pub fn new() -> Option<Self> {
if is_running_in_mgba() { if is_running_in_mgba() {
Some(Mgba { bytes_written: 0 }) Some(Mgba {})
} else { } else {
None None
} }
@ -51,30 +49,32 @@ impl Mgba {
output: core::fmt::Arguments, output: core::fmt::Arguments,
level: DebugLevel, level: DebugLevel,
) -> Result<(), core::fmt::Error> { ) -> Result<(), core::fmt::Error> {
write!(self, "{output}")?; let mut writer = MgbaWriter { bytes_written: 0 };
write!(&mut writer, "{output}")?;
self.set_level(level); self.set_level(level);
Ok(()) Ok(())
} }
} }
struct MgbaWriter {
bytes_written: usize,
}
impl Mgba { impl Mgba {
pub fn set_level(&mut self, level: DebugLevel) { pub fn set_level(&mut self, level: DebugLevel) {
DEBUG_LEVEL.set(DEBUG_FLAG_CODE | level as u16); DEBUG_LEVEL.set(DEBUG_FLAG_CODE | level as u16);
self.bytes_written = 0;
} }
} }
impl core::fmt::Write for Mgba { impl core::fmt::Write for MgbaWriter {
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> { fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
let mut str_iter = s.bytes(); for b in s.bytes() {
while self.bytes_written < 255 { if self.bytes_written > 255 {
match str_iter.next() { DEBUG_LEVEL.set(DEBUG_FLAG_CODE | DebugLevel::Info as u16);
Some(byte) => { self.bytes_written = 0;
OUTPUT_STRING.set(self.bytes_written, byte);
self.bytes_written += 1;
}
None => return Ok(()),
} }
OUTPUT_STRING.set(self.bytes_written, b);
self.bytes_written += 1;
} }
Ok(()) Ok(())
} }