6 releases
0.1.5 | Nov 20, 2023 |
---|---|
0.1.4 | Nov 12, 2023 |
0.1.3 | Oct 30, 2023 |
#484 in Debugging
21KB
285 lines
breadcrumbs
Breadcrumbs is a beautiful, tiny traceback and logging library for Rust that offers seamless integration with #![no_std]
, multi-threading and concurrency.
Features
- Beautifully-formatted traceback of logs (supporting
Display
andDebug
) - Dynamic log levels
- Dynamic log channels
- Seamless integration with
#![no_std]
- Multi-threading and concurrent logging supported with no special syntax
- Easy-to-use macros
- Support for listeners to be notified of new logs
Usage
Add the following to your Cargo.toml
:
[dependencies]
breadcrumbs = "0.1.5"
Then, initalize breadcrumbs
once in your main.rs
or lib.rs
:
use breadcrumbs::init;
init!();
You can set a custom log listener with ease by implementing the LogListener
trait:
use breadcrumbs::{init, LogListener, Log, LogLevel};
struct MyLogListener;
impl LogListener for MyLogListener {
fn on_log(&mut self, log: Log) {
if log.level.is_at_least(LogLevel::Warn) {
println!("{}", log);
} else {
// 💡 New in 0.1.5 - Remove unnecessary logs to save memory
// Useful in embedded usecases
log.remove();
}
}
}
init!(MyLogListener);
Then, simply use the log!
macro from or its variants from anywhere to log messages:
use breadcrumbs::{log, log_level, log_channel, LogLevel};
// A basic log message
log!("Hello, world!");
// A log message with a custom level
log_level!(LogLevel::Info, "Test log message");
// A log message with a custom channel
log_channel!("test_channel", "Test log message");
// A log message with a custom channel and level
log!(LogLevel::Info, "test_channel", "Test log message");
Access a traceback of log messages from anywhere with the traceback!
macro or its variants:
use breadcrumbs::{traceback, traceback_channel, traceback_level, LogLevel};
// A basic traceback, fetching all logged messages
let t = traceback!();
// A traceback with a custom channel, fetching messages in this channel
let t = traceback_channel!("my-channel");
// A traceback with a custom level, fetching messages of this level or higher
let traceback = traceback_level!(LogLevel::Warn);
// A traceback with a custom channel and level, fetching messages in this channel of this level or higher
let traceback = traceback!(LogLevel::Warn, "test_channel");
Traceback
and Log
objects beautifully implement Display
and Debug
:
use breadcrumbs::traceback;
let t = traceback!();
println!("{}", t);
println!("{:?}", t);
Example
use breadcrumbs::{init, log, log_level, log_channel, traceback, LogLevel, LogListener, Log};
struct MyLogListner;
impl LogListener for MyLogListner {
fn on_log(&mut self, log: Log) {
if log.level.is_at_least(LogLevel::Warn) {
println!("{}", log);
}
}
}
fn main() {
init!(MyLogListner);
log!("Hello, world!");
log_level!(LogLevel::Info, "Test log message");
log_channel!("test_channel", "Test log message");
log!(LogLevel::Warn, "test_channel", "Test log message");
let t = traceback!();
println!("Fatal Error! Traceback:\n{}", t);
}
Dependencies
~155KB