diff --git a/src/backtrace/pp.rs b/src/backtrace/pp.rs index 1cb75796..40e1930f 100644 --- a/src/backtrace/pp.rs +++ b/src/backtrace/pp.rs @@ -1,9 +1,11 @@ //! Pretty printing the backtrace -use std::{borrow::Cow, path::Path}; +use std::borrow::Cow; use colored::Colorize as _; +use crate::utils; + use super::{symbolicate::Frame, Settings}; /// Pretty prints processed backtrace frames up to `max_backtrace_len` @@ -39,7 +41,7 @@ pub(crate) fn backtrace(frames: &[Frame], settings: &Settings) { if let Some(location) = &subroutine.location { let path = if settings.compress_cratesio_dep_paths { - compress_cratesio_dep_path(&location.path) + utils::compress_cratesio_dep_path(&location.path) } else { location.path.display().to_string() }; @@ -73,57 +75,3 @@ pub(crate) fn backtrace(frames: &[Frame], settings: &Settings) { } } } - -// TODO use this for defmt logs -fn compress_cratesio_dep_path(path: &Path) -> String { - if let Some(dep) = Dependency::from_path(path) { - format!("[{}]/{}", dep.name_version, dep.path.display()) - } else { - path.display().to_string() - } -} - -struct Dependency<'p> { - name_version: &'p str, - path: &'p Path, -} - -impl<'p> Dependency<'p> { - // as of Rust 1.52.1 this path looks like this on Linux - // /home/some-user/.cargo/registry/src/github.com-0123456789abcdef/crate-name-0.1.2/src/lib.rs - // on Windows the `/home/some-user` part becomes something else - fn from_path(path: &'p Path) -> Option { - if !path.is_absolute() { - return None; - } - - let mut components = path.components(); - let _registry = components.find(|component| match component { - std::path::Component::Normal(component) => *component == "registry", - _ => false, - })?; - - if let std::path::Component::Normal(src) = components.next()? { - if src != "src" { - return None; - } - } - - if let std::path::Component::Normal(github) = components.next()? { - let github = github.to_str()?; - if !github.starts_with("github.com-") { - return None; - } - } - - if let std::path::Component::Normal(name_version) = components.next()? { - let name_version = name_version.to_str()?; - Some(Dependency { - name_version, - path: components.as_path(), - }) - } else { - None - } - } -} diff --git a/src/main.rs b/src/main.rs index 51384953..1c713bfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod backtrace; mod cortexm; mod registers; mod stacked; +mod utils; use std::{ collections::HashSet, @@ -455,14 +456,16 @@ fn notmain() -> anyhow::Result { let (mut file, mut line, mut mod_path) = (None, None, None); if let Some(loc) = loc { - let relpath = + let path = if let Ok(relpath) = loc.file.strip_prefix(¤t_dir) { - relpath + relpath.display().to_string() + } else if compress_cratesio_dep_paths { + utils::compress_cratesio_dep_path(&loc.file) } else { - // not relative; use full path - &loc.file + loc.file.display().to_string() }; - file = Some(relpath.display().to_string()); + + file = Some(path); line = Some(loc.line as u32); mod_path = Some(loc.module.clone()); } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 00000000..d8db3139 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,54 @@ +use std::path::Path; + +pub(crate) fn compress_cratesio_dep_path(path: &Path) -> String { + if let Some(dep) = Dependency::from_path(path) { + format!("[{}]/{}", dep.name_version, dep.path.display()) + } else { + path.display().to_string() + } +} + +struct Dependency<'p> { + name_version: &'p str, + path: &'p Path, +} + +impl<'p> Dependency<'p> { + // as of Rust 1.52.1 this path looks like this on Linux + // /home/some-user/.cargo/registry/src/github.com-0123456789abcdef/crate-name-0.1.2/src/lib.rs + // on Windows the `/home/some-user` part becomes something else + fn from_path(path: &'p Path) -> Option { + if !path.is_absolute() { + return None; + } + + let mut components = path.components(); + let _registry = components.find(|component| match component { + std::path::Component::Normal(component) => *component == "registry", + _ => false, + })?; + + if let std::path::Component::Normal(src) = components.next()? { + if src != "src" { + return None; + } + } + + if let std::path::Component::Normal(github) = components.next()? { + let github = github.to_str()?; + if !github.starts_with("github.com-") { + return None; + } + } + + if let std::path::Component::Normal(name_version) = components.next()? { + let name_version = name_version.to_str()?; + Some(Dependency { + name_version, + path: components.as_path(), + }) + } else { + None + } + } +}