[Feature] macro(?) for retrieving the file path relative to crate root

When building file paths for logging, the output is full of absolute paths, making the output illegible and also leaking information about the system where the final binary was compiled on (project root, cargo registry root, username of whoever compiled, etc).

I'd like to request a method (probably two different macros) to fetch the crate name and the file location relative to the crate root directory that could be used by the log crate to build much better log messages. Just like file!, this needs to point to the location of the invocation of the first macro, if nested.

When looking through logs, and I'm outputting file paths and line numbers these are actually what I'm looking for: which crate it is, and which file it is within that crate.

The plan afaik is to instead have file! output these sorts of sanitized paths, rather than real paths

https://rust-lang.github.io/rfcs/3127-trim-paths.html

You can do this today with manually setting up --remap-path-prefix in your ~/.cargo/config.toml:

[build]
rustflags = [
  "--remap-path-prefix=/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823=\u001b[36;1mcrates.io\u001b[0m",
]
3 Likes

Awesome, exactly what I was looking for

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.