Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/wraparound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use tracing::{instrument, warn};
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
use tracing_tree::HierarchicalLayer;

fn main() {
let layer = HierarchicalLayer::default()
.with_indent_lines(true)
.with_indent_amount(2)
.with_thread_names(true)
.with_thread_ids(true)
.with_targets(true)
.with_wraparound(5);

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

recurse(0);
}

#[instrument]
fn recurse(i: usize) {
warn!("boop");
if i > 20 {
warn!("bop");
return;
} else {
recurse(i + 1);
}
warn!("bop");
}
9 changes: 8 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Config {
pub render_thread_ids: bool,
/// Whether to show thread names.
pub render_thread_names: bool,
/// Specifies after how many indentation levels we will wrap back around to zero
pub wraparound: usize,
}

impl Config {
Expand Down Expand Up @@ -58,6 +60,10 @@ impl Config {
}
}

pub fn with_wraparound(self, wraparound: usize) -> Self {
Self { wraparound, ..self }
}

pub(crate) fn prefix(&self) -> String {
let mut buf = String::new();
if self.render_thread_ids {
Expand Down Expand Up @@ -90,6 +96,7 @@ impl Default for Config {
targets: false,
render_thread_ids: false,
render_thread_names: false,
wraparound: usize::max_value(),
}
}
}
Expand Down Expand Up @@ -123,7 +130,7 @@ impl Buffers {
indent_block(
&mut self.current_buf,
&mut self.indent_buf,
indent,
indent % config.wraparound,
config.indent_amount,
config.indent_lines,
&config.prefix(),
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ where
}
}

/// Resets the indentation to zero after `wraparound` indentation levels.
/// This is helpful if you expect very deeply nested spans as otherwise the indentation
/// just runs out of your screen.
pub fn with_wraparound(self, wraparound: usize) -> Self {
Self {
config: self.config.with_wraparound(wraparound),
..self
}
}

fn styled(&self, style: Style, text: impl AsRef<str>) -> String {
if self.config.ansi {
style.paint(text.as_ref()).to_string()
Expand Down