summaryrefslogtreecommitdiffstats
path: root/vendor/tracing-tree/examples/stderr.rs
blob: 75880a3769b57c435e649879070252a0eda6b64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use tracing::{debug, info, instrument};
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
use tracing_tree::HierarchicalLayer;

#[instrument]
fn nth_fibonacci(n: u64) -> u64 {
    if n == 0 || n == 1 {
        debug!("Base case");
        1
    } else {
        debug!("Recursing");
        nth_fibonacci(n - 1) + nth_fibonacci(n - 2)
    }
}

#[instrument]
fn fibonacci_seq(to: u64) -> Vec<u64> {
    let mut sequence = vec![];

    for n in 0..=to {
        debug!("Pushing {n} fibonacci", n = n);
        sequence.push(nth_fibonacci(n));
    }

    sequence
}

fn main() {
    let layer = HierarchicalLayer::default()
        .with_indent_lines(true)
        .with_indent_amount(2)
        .with_bracketed_fields(true);

    let subscriber = Registry::default().with(layer);
    tracing::subscriber::set_global_default(subscriber).unwrap();

    let n = 5;
    let sequence = fibonacci_seq(n);
    info!("The first {} fibonacci numbers are {:?}", n, sequence);
}