Hello folks, In this blog, I will be introducing a feature to track source location of caller of a function.
In one of the Rust projects, I came across a scenario, where I had to trace source location of caller of a function. Rust also has this feature, but it is not available for stable release. So I published a crate trace_caller and introduced trace
, a procedural attribute macro to track the source location. This crate will work in stable release of Rust.
This library uses backtrace
library in backend for acquiring backtraces at runtime.
How to use trace_caller:-
1) Add dependencies in Cargo.toml
[dependencies]
trace_caller = "0.2.0"
2) Now use in your code
use trace_caller::trace;
#[trace]
fn add(x: u32, y: u32) -> u32 {
x + y
}
fn main() {
let result = add(3, 4);
println!("Result: {}", result);
}
3) Run cargo.run
Called from "src/main.rs" at line 9
Result: 7
You can find source code here https://github.com/ayushmishra2005/trace_caller. Please feel free to raise pull request.
Thank you for reading the blog!!
Thank you so much for this amazing post! Got main logic from this, I think will help in python also.