From 51f635069f2b784cd8c7b116b62fdb7b95be1c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 13 Nov 2023 08:33:27 +0100 Subject: [PATCH] Fix defmt --- CHANGELOG.md | 1 + espflash/src/cli/monitor/mod.rs | 16 ++++++---------- espflash/src/cli/monitor/parser/esp_defmt.rs | 6 +++--- espflash/src/cli/monitor/parser/mod.rs | 2 +- espflash/src/cli/monitor/parser/serial.rs | 2 +- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b732de..ec868b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed printing panic backtraces when using `esp-println` and `defmt` (#496) +- Fixed defmt parsing when data is read in parts (#503) ### Changed diff --git a/espflash/src/cli/monitor/mod.rs b/espflash/src/cli/monitor/mod.rs index b99eb9dc..d574190e 100644 --- a/espflash/src/cli/monitor/mod.rs +++ b/espflash/src/cli/monitor/mod.rs @@ -99,6 +99,11 @@ pub fn monitor_with( let stdout = stdout(); let mut stdout = ResolvingPrinter::new(elf, stdout.lock()); + let mut parser: Box = match log_format { + LogFormat::Defmt => Box::new(parser::esp_defmt::EspDefmt::new(elf)), + LogFormat::Serial => Box::new(parser::serial::Serial), + }; + let mut buff = [0; 1024]; loop { let read_count = match serial.serial_port_mut().read(&mut buff) { @@ -108,16 +113,7 @@ pub fn monitor_with( err => err, }?; - match log_format { - LogFormat::Defmt => { - let mut parser = parser::esp_defmt::EspDefmt::new(elf); - parser.feed(&buff[0..read_count], &mut stdout); - } - LogFormat::Serial => { - let mut parser = parser::serial::Serial; - parser.feed(&buff[0..read_count], &mut stdout); - } - } + parser.feed(&buff[0..read_count], &mut stdout); // Don't forget to flush the writer! stdout.flush().ok(); diff --git a/espflash/src/cli/monitor/parser/esp_defmt.rs b/espflash/src/cli/monitor/parser/esp_defmt.rs index 5077b38c..acef261e 100644 --- a/espflash/src/cli/monitor/parser/esp_defmt.rs +++ b/espflash/src/cli/monitor/parser/esp_defmt.rs @@ -106,11 +106,11 @@ impl EspDefmt { } } - fn handle_raw(bytes: &[u8], out: &mut impl Write) { + fn handle_raw(bytes: &[u8], out: &mut dyn Write) { out.write_all(bytes).unwrap(); } - fn handle_defmt(frame: Frame<'_>, out: &mut impl Write) { + fn handle_defmt(frame: Frame<'_>, out: &mut dyn Write) { match frame.level() { Some(level) => { let color = match level { @@ -142,7 +142,7 @@ impl EspDefmt { } impl InputParser for EspDefmt { - fn feed(&mut self, bytes: &[u8], out: &mut impl Write) { + fn feed(&mut self, bytes: &[u8], out: &mut dyn Write) { let Some(table) = self.table.as_mut() else { Self::handle_raw(bytes, out); return; diff --git a/espflash/src/cli/monitor/parser/mod.rs b/espflash/src/cli/monitor/parser/mod.rs index 0a62c2a7..6c49ff36 100644 --- a/espflash/src/cli/monitor/parser/mod.rs +++ b/espflash/src/cli/monitor/parser/mod.rs @@ -13,7 +13,7 @@ use regex::Regex; use crate::cli::monitor::{line_endings::normalized, symbols::Symbols}; pub trait InputParser { - fn feed(&mut self, bytes: &[u8], out: &mut impl Write); + fn feed(&mut self, bytes: &[u8], out: &mut dyn Write); } // Pattern to much a function address in serial output. diff --git a/espflash/src/cli/monitor/parser/serial.rs b/espflash/src/cli/monitor/parser/serial.rs index 5a2adb05..03cad46b 100644 --- a/espflash/src/cli/monitor/parser/serial.rs +++ b/espflash/src/cli/monitor/parser/serial.rs @@ -5,7 +5,7 @@ use crate::cli::monitor::parser::InputParser; pub struct Serial; impl InputParser for Serial { - fn feed(&mut self, bytes: &[u8], out: &mut impl Write) { + fn feed(&mut self, bytes: &[u8], out: &mut dyn Write) { out.write_all(bytes).unwrap(); } }