Skip to content

Commit

Permalink
Merge #610
Browse files Browse the repository at this point in the history
610: `defmt-print`: Log if malformed frame gets skipped r=justahero a=Urhengulas

Also add `--verbose`-flag to also show non-defmt logs.

Fixes #609

## Example

```
❯ bat defmt-frames | cargo run -- -e hello -v
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `/home/urhengulas/Documents/github.com/knurling-rs/defmt/target/debug/defmt-print -e hello -v`
0 INFO  Lorem Ipsum is simply dummy text
└─ hello::__cortex_m_rt_main @ /home/urhengulas/Documents/github.com/knurling-rs/defmt-app/src/bin/hello.rs:8
1 INFO  of the printing and typesetting 
└─ hello::__cortex_m_rt_main @ /home/urhengulas/Documents/github.com/knurling-rs/defmt-app/src/bin/hello.rs:9
2 INFO  industry. Lorem Ipsum has been the 
└─ hello::__cortex_m_rt_main @ /home/urhengulas/Documents/github.com/knurling-rs/defmt-app/src/bin/hello.rs:10
(HOST) WARN  malformed frame skipped
└─ defmt_print @ print/src/main.rs:79
4 INFO  since the 1500s, when an unknown printer 
└─ hello::__cortex_m_rt_main @ /home/urhengulas/Documents/github.com/knurling-rs/defmt-app/src/bin/hello.rs:12
5 INFO  took a galley of type and scrambled it to 
└─ hello::__cortex_m_rt_main @ /home/urhengulas/Documents/github.com/knurling-rs/defmt-app/src/bin/hello.rs:13
6 INFO  make a type specimen book.
```

Co-authored-by: Johann Hemmann <johann.hemmann@code.berlin>
  • Loading branch information
bors[bot] and Urhengulas authored Oct 21, 2021
2 parents da1682f + 4e1a25b commit 033fbcb
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions print/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,36 @@ struct Opts {
#[structopt(short, parse(from_os_str), required_unless_one(&["version"]))]
elf: Option<PathBuf>,

#[structopt(long)]
show_skipped_frames: bool,

#[structopt(short, long)]
verbose: bool,

#[structopt(short = "V", long)]
version: bool,
// may want to add this later
// #[structopt(short, long)]
// verbose: bool,
// TODO add file path argument; always use stdin for now
}

const READ_BUFFER_SIZE: usize = 1024;

fn main() -> anyhow::Result<()> {
let opts: Opts = Opts::from_args();

if opts.version {
let Opts {
elf,
show_skipped_frames,
verbose,
version,
} = Opts::from_args();

if version {
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)
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)?;
Expand Down Expand Up @@ -71,7 +77,13 @@ 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 => {
if show_skipped_frames || verbose {
println!("(HOST) malformed frame skipped");
println!("└─ {} @ {}:{}", env!("CARGO_PKG_NAME"), file!(), line!());
}
continue;
}
},
}
}
Expand All @@ -92,13 +104,10 @@ fn location_info(locs: &Option<Locations>, 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(&current_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(&current_dir).unwrap_or(&loc.file);

file = Some(path.display().to_string());
line = Some(loc.line as u32);
mod_path = Some(loc.module.clone());
}
Expand Down

0 comments on commit 033fbcb

Please sign in to comment.