Skip to content

Commit

Permalink
Fix defmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 13, 2023
1 parent d59bd37 commit 51f6350
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 6 additions & 10 deletions espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ pub fn monitor_with(
let stdout = stdout();
let mut stdout = ResolvingPrinter::new(elf, stdout.lock());

let mut parser: Box<dyn InputParser> = 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) {
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions espflash/src/cli/monitor/parser/esp_defmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/cli/monitor/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/cli/monitor/parser/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 51f6350

Please sign in to comment.