Skip to content

Commit f814c58

Browse files
authored
Merge pull request #86 from dingxiangfei2009/fix-multi-line-indent
Properly indent multiline messages
2 parents 12ac87f + b8923de commit f814c58

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

examples/basic.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use tracing::{debug, error, info, instrument, span, warn, Level};
22
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
33
use tracing_tree::HierarchicalLayer;
44

5+
#[derive(Debug)]
6+
struct PrettyPrintMe {
7+
value_a: u32,
8+
value_b: String,
9+
}
10+
511
fn main() {
612
let layer = HierarchicalLayer::default()
713
.with_writer(std::io::stdout)
@@ -50,6 +56,18 @@ fn main() {
5056
error!("hello");
5157
});
5258
drop(peer3);
59+
let val = format!(
60+
"{:#?}",
61+
PrettyPrintMe {
62+
value_a: 42,
63+
value_b: "hello".into(),
64+
}
65+
);
66+
let peer3 = span!(Level::TRACE, "", val = %val);
67+
peer3.in_scope(|| {
68+
error!("hello");
69+
});
70+
drop(peer3);
5371
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
5472
peer1.in_scope(|| {
5573
warn!(algo = "xor", "weak encryption requested");

examples/basic.stdout

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
1:main ┌┘basic::foomp 42 <- format string, normal_var=43
2121
1:main ┌┘basic::server host="localhost", port=8080
2222
1:main └┐basic::server host="localhost", port=8080
23+
1:main └┐basic:: val=PrettyPrintMe {
24+
1:main │ value_a: 42,
25+
1:main │ value_b: "hello",
26+
1:main │ }
27+
1:main ├─ ERROR basic hello
28+
1:main ┌┘basic:: val=PrettyPrintMe {
29+
1:main │ value_a: 42,
30+
1:main │ value_b: "hello",
31+
1:main │ }
32+
1:main ┌┘basic::server host="localhost", port=8080
33+
1:main └┐basic::server host="localhost", port=8080
2334
1:main └┐basic::conn peer_addr="82.9.9.9", port=42381
2435
1:main ├─ WARN basic weak encryption requested, algo="xor"
2536
1:main ├─ DEBUG basic response sent, length=8

examples/no-indent.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use tracing::{debug, error, info, instrument, span, warn, Level};
22
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
33
use tracing_tree::HierarchicalLayer;
44

5+
#[derive(Debug)]
6+
struct PrettyPrintMe {
7+
value_a: u32,
8+
value_b: String,
9+
}
10+
511
fn main() {
612
let layer = HierarchicalLayer::default()
713
.with_writer(std::io::stdout)
@@ -47,6 +53,18 @@ fn main() {
4753
error!("hello");
4854
});
4955
drop(peer3);
56+
let val = format!(
57+
"{:#?}",
58+
PrettyPrintMe {
59+
value_a: 42,
60+
value_b: "hello".into(),
61+
}
62+
);
63+
let peer3 = span!(Level::TRACE, "", val = %val);
64+
peer3.in_scope(|| {
65+
error!("hello");
66+
});
67+
drop(peer3);
5068
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
5169
peer1.in_scope(|| {
5270
warn!(algo = "xor", "weak encryption requested");

examples/no-indent.stdout

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
1:main DEBUG no_indent connected
1010
1:main no_indent::foomp 42 <- format string, normal_var=43
1111
1:main ERROR no_indent hello
12+
1:main no_indent:: val=PrettyPrintMe {
13+
1:main value_a: 42,
14+
1:main value_b: "hello",
15+
1:main }
16+
1:main ERROR no_indent hello
1217
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
1318
1:main WARN no_indent weak encryption requested, algo="xor"
1419
1:main DEBUG no_indent response sent, length=8

src/format.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,14 @@ fn indent_block_with_lines(
448448

449449
// add the rest of the indentation, since we don't want to draw horizontal lines
450450
// for subsequent lines
451-
for i in 0..indent_amount {
452-
if i % indent_amount == 0 {
453-
s.push_str(LINE_VERT);
454-
} else {
455-
s.push(' ');
456-
}
451+
match style {
452+
SpanMode::Open { .. } | SpanMode::Retrace { .. } => s.push_str(" "),
453+
SpanMode::Close { .. } => s.push(' '),
454+
_ => {}
455+
}
456+
s.push_str(LINE_VERT);
457+
for _ in 1..=indent_amount {
458+
s.push(' ');
457459
}
458460

459461
// add all of the actual content, with each line preceded by the indent string
@@ -489,11 +491,16 @@ fn indent_block(
489491
if indent_lines {
490492
indent_block_with_lines(&lines, buf, indent, indent_amount, prefix, style);
491493
} else {
492-
let indent_str = String::from(" ").repeat(indent_spaces);
494+
let mut indent_str = " ".repeat(indent_spaces);
495+
let mut first_line = true;
493496
for line in lines {
494497
buf.push_str(prefix);
495498
buf.push(' ');
496499
buf.push_str(&indent_str);
500+
if first_line {
501+
first_line = false;
502+
indent_str.push_str(" ");
503+
}
497504
buf.push_str(line);
498505
buf.push('\n');
499506
}

0 commit comments

Comments
 (0)