Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Report defmt decode errors to the user. #112

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,35 +419,45 @@ fn notmain() -> Result<i32, anyhow::Error> {
if let Some(table) = table.as_ref() {
frames.extend_from_slice(&read_buf[..num_bytes_read]);

while let Ok((frame, consumed)) = defmt_decoder::decode(&frames, table) {
// NOTE(`[]` indexing) all indices in `table` have already been
// verified to exist in the `locs` map
let loc = locs.as_ref().map(|locs| &locs[&frame.index()]);

let (mut file, mut line, mut mod_path) = (None, None, None);
if let Some(loc) = loc {
let relpath = if let Ok(relpath) = loc.file.strip_prefix(&current_dir) {
relpath
} else {
// not relative; use full path
&loc.file
};
file = Some(relpath.display().to_string());
line = Some(loc.line as u32);
mod_path = Some(loc.module.clone());
loop {
match defmt_decoder::decode(&frames, table) {
Ok((frame, consumed)) => {
// NOTE(`[]` indexing) all indices in `table` have already been
// verified to exist in the `locs` map
let loc = locs.as_ref().map(|locs| &locs[&frame.index()]);

let (mut file, mut line, mut mod_path) = (None, None, None);
if let Some(loc) = loc {
let relpath =
if let Ok(relpath) = loc.file.strip_prefix(&current_dir) {
relpath
} else {
// not relative; use full path
&loc.file
};
file = Some(relpath.display().to_string());
line = Some(loc.line as u32);
mod_path = Some(loc.module.clone());
}

// Forward the defmt frame to our logger.
logger::log_defmt(
&frame,
file.as_deref(),
line,
mod_path.as_ref().map(|s| &**s),
);

let num_frames = frames.len();
frames.rotate_left(consumed);
frames.truncate(num_frames - consumed);
}
Err(defmt_decoder::DecodeError::UnexpectedEof) => break,
Err(defmt_decoder::DecodeError::Malformed) => {
log::error!("failed to decode defmt data: {:x?}", frames);
Err(defmt_decoder::DecodeError::Malformed)?;
}
}

// Forward the defmt frame to our logger.
logger::log_defmt(
&frame,
file.as_deref(),
line,
mod_path.as_ref().map(|s| &**s),
);

let num_frames = frames.len();
frames.rotate_left(consumed);
frames.truncate(num_frames - consumed);
}
} else {
stdout.write_all(&read_buf[..num_bytes_read])?;
Expand Down