Skip to content

Properly indent multiline messages #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
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
18 changes: 18 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
use tracing_tree::HierarchicalLayer;

#[derive(Debug)]
struct PrettyPrintMe {
value_a: u32,

Check warning on line 7 in examples/basic.rs

View workflow job for this annotation

GitHub Actions / Test Suite

fields `value_a` and `value_b` are never read
value_b: String,
}

fn main() {
let layer = HierarchicalLayer::default()
.with_writer(std::io::stdout)
Expand Down Expand Up @@ -50,6 +56,18 @@
error!("hello");
});
drop(peer3);
let val = format!(
"{:#?}",
PrettyPrintMe {
value_a: 42,
value_b: "hello".into(),
}
);
let peer3 = span!(Level::TRACE, "", val = %val);
peer3.in_scope(|| {
error!("hello");
});
drop(peer3);
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.in_scope(|| {
warn!(algo = "xor", "weak encryption requested");
Expand All @@ -72,12 +90,12 @@
}

#[instrument]
fn call_a(name: &str) {

Check warning on line 93 in examples/basic.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `call_a` is never used
info!(name, "got a name");
call_b(name)
}

#[instrument]
fn call_b(name: &str) {

Check warning on line 99 in examples/basic.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `call_b` is never used
info!(name, "got a name");
}
11 changes: 11 additions & 0 deletions examples/basic.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
1:main ┌┘basic::foomp 42 <- format string, normal_var=43
1:main ┌┘basic::server host="localhost", port=8080
1:main └┐basic::server host="localhost", port=8080
1:main └┐basic:: val=PrettyPrintMe {
1:main │ value_a: 42,
1:main │ value_b: "hello",
1:main │ }
1:main ├─ ERROR basic hello
1:main ┌┘basic:: val=PrettyPrintMe {
1:main │ value_a: 42,
1:main │ value_b: "hello",
1:main │ }
1:main ┌┘basic::server host="localhost", port=8080
1:main └┐basic::server host="localhost", port=8080
1:main └┐basic::conn peer_addr="82.9.9.9", port=42381
1:main ├─ WARN basic weak encryption requested, algo="xor"
1:main ├─ DEBUG basic response sent, length=8
Expand Down
18 changes: 18 additions & 0 deletions examples/no-indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use tracing::{debug, error, info, instrument, span, warn, Level};
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
use tracing_tree::HierarchicalLayer;

#[derive(Debug)]
struct PrettyPrintMe {
value_a: u32,
value_b: String,
}

fn main() {
let layer = HierarchicalLayer::default()
.with_writer(std::io::stdout)
Expand Down Expand Up @@ -47,6 +53,18 @@ fn main() {
error!("hello");
});
drop(peer3);
let val = format!(
"{:#?}",
PrettyPrintMe {
value_a: 42,
value_b: "hello".into(),
}
);
let peer3 = span!(Level::TRACE, "", val = %val);
peer3.in_scope(|| {
error!("hello");
});
drop(peer3);
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.in_scope(|| {
warn!(algo = "xor", "weak encryption requested");
Expand Down
5 changes: 5 additions & 0 deletions examples/no-indent.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
1:main DEBUG no_indent connected
1:main no_indent::foomp 42 <- format string, normal_var=43
1:main ERROR no_indent hello
1:main no_indent:: val=PrettyPrintMe {
1:main value_a: 42,
1:main value_b: "hello",
1:main }
1:main ERROR no_indent hello
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
1:main WARN no_indent weak encryption requested, algo="xor"
1:main DEBUG no_indent response sent, length=8
Expand Down
21 changes: 14 additions & 7 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
// Render something when wraparound occurs so the user is aware of it
if config.indent_lines {
match style {
SpanMode::PreOpen { .. } | SpanMode::Open { .. } => {

Check failure on line 256 in src/format.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct pattern is not needed for a unit variant
if indent > 0 && (indent + 1) % config.wraparound == 0 {
self.current_buf.push_str(&prefix);
for _ in 0..(indent % config.wraparound * config.indent_amount) {
Expand Down Expand Up @@ -347,7 +347,7 @@
SpanMode::Open { .. } => buf.push_str(LINE_OPEN),
SpanMode::Retrace { .. } => buf.push_str(LINE_OPEN),
SpanMode::Close { .. } => buf.push_str(LINE_CLOSE),
SpanMode::PreOpen { .. } | SpanMode::PostClose => {}

Check failure on line 350 in src/format.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct pattern is not needed for a unit variant
SpanMode::Event => {}
}
}
Expand Down Expand Up @@ -448,12 +448,14 @@

// add the rest of the indentation, since we don't want to draw horizontal lines
// for subsequent lines
for i in 0..indent_amount {
if i % indent_amount == 0 {
s.push_str(LINE_VERT);
} else {
s.push(' ');
}
match style {
SpanMode::Open { .. } | SpanMode::Retrace { .. } => s.push_str(" "),
SpanMode::Close { .. } => s.push(' '),
_ => {}
}
s.push_str(LINE_VERT);
for _ in 1..=indent_amount {
s.push(' ');
}

// add all of the actual content, with each line preceded by the indent string
Expand Down Expand Up @@ -489,11 +491,16 @@
if indent_lines {
indent_block_with_lines(&lines, buf, indent, indent_amount, prefix, style);
} else {
let indent_str = String::from(" ").repeat(indent_spaces);
let mut indent_str = " ".repeat(indent_spaces);
let mut first_line = true;
for line in lines {
buf.push_str(prefix);
buf.push(' ');
buf.push_str(&indent_str);
if first_line {
first_line = false;
indent_str.push_str(" ");
}
buf.push_str(line);
buf.push('\n');
}
Expand Down
Loading