From 3559edec63968a24d327fe4b59af027b28b0b02b Mon Sep 17 00:00:00 2001 From: Xiangfei Ding Date: Fri, 6 Jun 2025 11:49:31 +0000 Subject: [PATCH 1/2] original indent behaviour --- examples/basic.rs | 18 ++++++++++++++++++ examples/basic.stdout | 11 +++++++++++ examples/no-indent.rs | 18 ++++++++++++++++++ examples/no-indent.stdout | 5 +++++ 4 files changed, 52 insertions(+) diff --git a/examples/basic.rs b/examples/basic.rs index a736c65..b62e9e5 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -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) @@ -50,6 +56,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"); diff --git a/examples/basic.stdout b/examples/basic.stdout index 24f7fdf..e25941c 100644 --- a/examples/basic.stdout +++ b/examples/basic.stdout @@ -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 diff --git a/examples/no-indent.rs b/examples/no-indent.rs index 288e560..4ccaffc 100644 --- a/examples/no-indent.rs +++ b/examples/no-indent.rs @@ -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) @@ -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"); diff --git a/examples/no-indent.stdout b/examples/no-indent.stdout index 21bd888..16386d7 100644 --- a/examples/no-indent.stdout +++ b/examples/no-indent.stdout @@ -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 From b8923de9661b12d1da5102e8fe33f9aad100b41e Mon Sep 17 00:00:00 2001 From: Xiangfei Ding Date: Fri, 6 Jun 2025 12:32:37 +0000 Subject: [PATCH 2/2] align the pipes to the open/close header --- examples/basic.stdout | 12 ++++++------ examples/no-indent.stdout | 6 +++--- src/format.rs | 21 ++++++++++++++------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/examples/basic.stdout b/examples/basic.stdout index e25941c..b491939 100644 --- a/examples/basic.stdout +++ b/examples/basic.stdout @@ -21,14 +21,14 @@ 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 │ 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 │ 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 diff --git a/examples/no-indent.stdout b/examples/no-indent.stdout index 16386d7..024f4ea 100644 --- a/examples/no-indent.stdout +++ b/examples/no-indent.stdout @@ -10,9 +10,9 @@ 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 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" diff --git a/src/format.rs b/src/format.rs index f2c39f4..8ce291c 100644 --- a/src/format.rs +++ b/src/format.rs @@ -448,12 +448,14 @@ fn indent_block_with_lines( // 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 @@ -489,11 +491,16 @@ fn indent_block( 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'); }