From 0ed62a248bd52d0802464e0bc9b9b172f6bf275b Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Mon, 11 Oct 2021 16:02:04 +0200 Subject: [PATCH 1/6] `defmt-print`--Add-`--verbose`-flag --- print/src/main.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/print/src/main.rs b/print/src/main.rs index 650f4f84..61cc44a0 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -17,9 +17,11 @@ struct Opts { #[structopt(short = "V", long)] version: bool, - // may want to add this later - // #[structopt(short, long)] - // verbose: bool, + + /// Enable more verbose logging. + #[structopt(short, long)] + verbose: bool, + // // TODO add file path argument; always use stdin for now } @@ -32,10 +34,12 @@ fn main() -> anyhow::Result<()> { return print_version(); } - let verbose = false; - defmt_decoder::log::init_logger(verbose, |metadata| { - // We display *all* defmt frames, but nothing else. - defmt_decoder::log::is_defmt_frame(metadata) + let verbose = opts.verbose; + defmt_decoder::log::init_logger(verbose, move |metadata| { + match verbose { + false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else. + true => true, // We display *all* frames + } }); let bytes = fs::read(&opts.elf.unwrap())?; From 2f509f890f5dce061ba589dd8967bc7a3a39437e Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Mon, 11 Oct 2021 16:27:08 +0200 Subject: [PATCH 2/6] `defmt-print`: Log if malformed frame gets skipped --- print/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/print/src/main.rs b/print/src/main.rs index 61cc44a0..ba6b21e9 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -75,7 +75,10 @@ fn main() -> anyhow::Result<()> { // if recovery is impossible, abort false => return Err(DecodeError::Malformed.into()), // if recovery is possible, skip the current frame and continue with new data - true => continue, + true => { + log::warn!("malformed frame skipped"); + continue; + } }, } } From 93b2aad2a1f1650a6eb0ca5b08cc8dfa46b1f9ea Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 19 Oct 2021 16:08:49 +0200 Subject: [PATCH 3/6] `defmt-print`: Replace if-let with unwrap_or --- print/src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/print/src/main.rs b/print/src/main.rs index ba6b21e9..fbaa8c3c 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -99,13 +99,10 @@ fn location_info(locs: &Option, frame: &Frame, current_dir: &Path) -> let loc = locs.as_ref().map(|locs| &locs[&frame.index()]); 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()); + // try to get the relative path, else the full one + let path = loc.file.strip_prefix(¤t_dir).unwrap_or(&loc.file); + + file = Some(path.display().to_string()); line = Some(loc.line as u32); mod_path = Some(loc.module.clone()); } From 361ee9ffd8ed686346351833f76ea33c227ab1ef Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 19 Oct 2021 16:10:44 +0200 Subject: [PATCH 4/6] `defmt-print`: Alphabetically order fields of `struct Opts` --- print/src/main.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/print/src/main.rs b/print/src/main.rs index fbaa8c3c..5f6b845f 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -15,14 +15,12 @@ struct Opts { #[structopt(short, parse(from_os_str), required_unless_one(&["version"]))] elf: Option, - #[structopt(short = "V", long)] - version: bool, - /// Enable more verbose logging. #[structopt(short, long)] verbose: bool, - // - // TODO add file path argument; always use stdin for now + + #[structopt(short = "V", long)] + version: bool, } const READ_BUFFER_SIZE: usize = 1024; From 6ebf576e9bedd0592b7352e9d34df263d80bdc2d Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 19 Oct 2021 16:24:08 +0200 Subject: [PATCH 5/6] `defmt-print`: Destructure `Opts` --- print/src/main.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/print/src/main.rs b/print/src/main.rs index 5f6b845f..d1fcf47e 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -15,7 +15,6 @@ struct Opts { #[structopt(short, parse(from_os_str), required_unless_one(&["version"]))] elf: Option, - /// Enable more verbose logging. #[structopt(short, long)] verbose: bool, @@ -26,21 +25,22 @@ struct Opts { const READ_BUFFER_SIZE: usize = 1024; fn main() -> anyhow::Result<()> { - let opts: Opts = Opts::from_args(); + let Opts { + elf, + verbose, + version, + } = Opts::from_args(); - if opts.version { + if version { return print_version(); } - let verbose = opts.verbose; - defmt_decoder::log::init_logger(verbose, move |metadata| { - match verbose { - false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else. - true => true, // We display *all* frames - } + defmt_decoder::log::init_logger(verbose, move |metadata| match verbose { + false => defmt_decoder::log::is_defmt_frame(metadata), // We display *all* defmt frames, but nothing else. + true => true, // We display *all* frames. }); - let bytes = fs::read(&opts.elf.unwrap())?; + let bytes = fs::read(&elf.unwrap())?; let table = Table::parse(&bytes)?.ok_or_else(|| anyhow!(".defmt data not found"))?; let locs = table.get_locations(&bytes)?; From 4e1a25bfe24a3c7cb25657a247fc1688dd45713a Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 19 Oct 2021 17:00:15 +0200 Subject: [PATCH 6/6] `defmt-print`: Add `--show-skipped-frames` --- print/src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/print/src/main.rs b/print/src/main.rs index d1fcf47e..767c0de7 100644 --- a/print/src/main.rs +++ b/print/src/main.rs @@ -15,6 +15,9 @@ struct Opts { #[structopt(short, parse(from_os_str), required_unless_one(&["version"]))] elf: Option, + #[structopt(long)] + show_skipped_frames: bool, + #[structopt(short, long)] verbose: bool, @@ -27,6 +30,7 @@ const READ_BUFFER_SIZE: usize = 1024; fn main() -> anyhow::Result<()> { let Opts { elf, + show_skipped_frames, verbose, version, } = Opts::from_args(); @@ -74,7 +78,10 @@ fn main() -> anyhow::Result<()> { false => return Err(DecodeError::Malformed.into()), // if recovery is possible, skip the current frame and continue with new data true => { - log::warn!("malformed frame skipped"); + if show_skipped_frames || verbose { + println!("(HOST) malformed frame skipped"); + println!("└─ {} @ {}:{}", env!("CARGO_PKG_NAME"), file!(), line!()); + } continue; } },