diff --git a/src/main.rs b/src/main.rs index 900de360..6e65d8fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -419,35 +419,45 @@ fn notmain() -> Result { 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(¤t_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(¤t_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])?;