Trace caller’s source location in the stable Rust

Table of contents
Reading Time: < 1 minute

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

trace_caller = "0.2.0"

2) Now use in your code

use trace_caller::trace;

fn add(x: u32, y: u32) -> u32 {
    x + y

fn main() {
    let result = add(3, 4);
    println!("Result: {}", result);

3) Run

Called from "src/" at line 9
Result: 7

You can find source code here Please feel free to raise pull request.

Thank you for reading the blog!!

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

1 thought on “Trace caller’s source location in the stable Rust1 min read

Comments are closed.