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

Commit

Permalink
apply path compression to defmt logs
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed May 18, 2021
1 parent f4d863e commit 7bd5a8b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
60 changes: 4 additions & 56 deletions src/backtrace/pp.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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.heygears.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<Self> {
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.heygears.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
}
}
}
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod backtrace;
mod cortexm;
mod registers;
mod stacked;
mod utils;

use std::{
collections::HashSet,
Expand Down Expand Up @@ -455,14 +456,16 @@ fn notmain() -> anyhow::Result<i32> {

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(&current_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());
}
Expand Down
54 changes: 54 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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.heygears.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<Self> {
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.heygears.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
}
}
}

0 comments on commit 7bd5a8b

Please # to comment.