| Crates.io | localtrace |
| lib.rs | localtrace |
| version | 0.1.9 |
| created_at | 2025-07-13 14:50:47.066568+00 |
| updated_at | 2025-07-15 16:16:15.223904+00 |
| description | A local tracing library for Rust |
| homepage | |
| repository | https://github.com/ysuzuki19/localtrace |
| max_upload_size | |
| id | 1750564 |
| size | 28,592 |
A lightweight Rust library for enhanced error handling with local backtrace information.
localtrace provides a simple error handling mechanism that captures and displays backtrace information filtered to show only your local project files. This makes debugging much easier by focusing on your code rather than system libraries.
Result<T, E>trace! macroRun the following command to add localtrace to your project:
cargo add localtrace
use localtrace::{Result, trace, catch_with_trace};
fn might_fail() -> Result<String> {
let content = std::fs::read_to_string("config.txt")?;
if content.is_empty() {
return trace!("Configuration file is empty");
}
Ok(content)
}
fn main() {
catch_with_trace(|| {
let config = might_fail()?;
println!("Config: {}", config);
Ok(())
});
}
$ cargo run
message: No such file or directory (os error 2)
- /path/to/project/localtrace/example/src/readme_quickstart.rs:4
- /path/to/project/localtrace/example/src/readme_quickstart.rs:15
- /path/to/project/localtrace/example/src/readme_quickstart.rs:14
The error is triggered at the line where read_to_string is called. The backtrace shows the sequence of function calls, specifically pointing to the lines in might_fail() and catch_with_trace() where the error was propagated.
Result<T>A type alias for std::result::Result<T, Error> that provides enhanced error information.
ErrorThe main error type that captures:
catch_with_trace<F>(f: F)Executes a function and prints any errors to stderr.
localtrace::catch_with_trace(|| {
// Your code here
Ok(())
});
trace!(message)Creates an error with the given message.
use localtrace::{Result, trace};
// trace! can receive a simple message
fn simple_message() -> Result<()> {
trace!("Something went wrong")
}
// trace! can receive a formatted message
fn formatted_message(id: u32, reason: &str) -> Result<()> {
trace!("Failed to process item {}: {}", id, reason)
}
In debug builds, localtrace captures a full backtrace when an error occurs, then filters it to show only stack frames from your project directory (determined by the CARGO_MANIFEST_DIR environment variable). This gives you precise error location information without the out of crate calling.
In release builds, backtrace collection is completely disabled for zero runtime overhead.
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
Example code is licensed under the MIT License.