Crate report

Source
Expand description

§Report

MIT licensed Latest version Documentation

Report is a simple logging and error-reporting library. It is designed to be:

  • Simple: There is almost no boilerplate required. It is not required to define error enums or implement traits.
  • Compatible: This library will work with other libraries that don’t use the custom Result or Error types from this crate.
  • Efficient: Strings are only formatted when it is clear that they will actually be printed.

§Example

use report::{Result, log, report, info, bail};
use std::fs::File;
use std::io::{BufRead, BufReader};

#[report]
#[log("Running experiments")]
fn main() {
    let path = "data.txt";
    
    #[report("Running task one on {path}")]
    task_one(path).ok();

    let path = "Cargo.toml";
    #[report("Running task two on {path}")]
    task_two(path).ok();
}

fn task_one(file: &str) -> Result {
    let _file = File::open(file)?; 
    bail!("File exists, even though it should not")
}

#[report]
fn task_two(file: &str) -> Result {
    let file = File::open(file)?;
    let metadata = file.metadata()?;

    info!("File size: {}", metadata.len());
    
    for line in BufReader::new(file).lines() {
        #[report("Reading line")]
        let line = line?;

        if line.starts_with("[") {
            info!("Found section: {line}");
        }
    }

    Ok(())
}

§Output

╭───────────────────────────────────────────────────────────────────────────────────────╮
│ Running experinments                                                                  │
├─┬─────────────────────────────────────────────────────────────────────────────────────┤
│ ├── Running task one on data.txt                                                      │
│ │   ╰── error: No such file or directory (os error 2)                                 │
│ ╰── Running task two on Cargo.toml                                                    │
│     ├── info: File size: 552                                                          │
│     ├── info: Found section: [package]                                                │
│     ├── info: Found section: [workspace]                                              │
│     ├── info: Found section: [dependencies]                                           │
│     ╰── info: Found section: [features]                                               │
╰───────────────────────────────────────────────────────────────────────────────────────╯

§Feature Flags

FlagDescription
unicodeUse unicode box drawing characters.
colorUse colors for the log level.
frameDraw a frame around every report

Macros§

bail
Log error message and return from function
error
Logs a message with the error prefix
info
Logs a message with the info prefix
warn
Logs a message with the warning prefix

Structs§

Error
Custom error type without context information
Report
Group of logging events

Type Aliases§

Result
Custom result type without error information

Attribute Macros§

log
Print all nested logging events to the console.
report
Annotate a new logging group with a custom message.