Skip to content

Commit

Permalink
Put event fields on their own lines in line-rendering mode
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 23, 2023
1 parent 483cc0a commit 73e521d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
15 changes: 10 additions & 5 deletions examples/basic.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
1:main│ ├┐basic::server host="localhost", port=8080
1:main│ │└┐basic::conn peer_addr="82.9.9.9", port=42381
1:main│ │ ├─ Xms DEBUG basic connected
1:main│ │ ├─ Xms DEBUG basic message received, length=2
1:main│ │ ├─┬─ Xms DEBUG basic message received
1:main│ │ │ └─ length=2
1:main│ │┌┘basic::conn peer_addr="82.9.9.9", port=42381
1:main│ ├┘basic::server host="localhost", port=8080
1:main│ ├┐basic::server host="localhost", port=8080
Expand All @@ -21,15 +22,19 @@
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│ │ ├─ Xms WARN basic weak encryption requested, algo="xor"
1:main│ │ ├─ Xms DEBUG basic response sent, length=8
1:main│ │ ├─┬─ Xms WARN basic weak encryption requested
1:main│ │ │ └─ algo="xor"
1:main│ │ ├─┬─ Xms DEBUG basic response sent
1:main│ │ │ └─ length=8
1:main│ │ ├─ Xms DEBUG basic disconnected
1:main│ │┌┘basic::conn peer_addr="82.9.9.9", port=42381
1:main│ ├┘basic::server host="localhost", port=8080
1:main│ ├┐basic::server host="localhost", port=8080
1:main│ │└┐basic::conn peer_addr="8.8.8.8", port=18230
1:main│ │ ├─ Xms DEBUG basic message received, length=5
1:main│ │ ├─ Xms DEBUG basic response sent, length=8
1:main│ │ ├─┬─ Xms DEBUG basic message received
1:main│ │ │ └─ length=5
1:main│ │ ├─┬─ Xms DEBUG basic response sent
1:main│ │ │ └─ length=8
1:main│ │ ├─ Xms DEBUG basic disconnected
1:main│ │┌┘basic::conn peer_addr="8.8.8.8", port=18230
1:main│ ├┘basic::server host="localhost", port=8080
Expand Down
15 changes: 10 additions & 5 deletions examples/quiet.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
1:main│ ├─ Xms INFO quiet listening
1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381
1:main│ │ ├─ Xms DEBUG quiet connected
1:main│ │ ├─ Xms DEBUG quiet message received, length=2
1:main│ │ ├─┬─ Xms DEBUG quiet message received
1:main│ │ │ └─ length=2
1:main│ ├─┘
1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230
1:main│ │ ├─ Xms DEBUG quiet connected
Expand All @@ -13,13 +14,17 @@
1:main│ │ ├─ Xms ERROR quiet hello
1:main│ ├─┘
1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381
1:main│ │ ├─ Xms WARN quiet weak encryption requested, algo="xor"
1:main│ │ ├─ Xms DEBUG quiet response sent, length=8
1:main│ │ ├─┬─ Xms WARN quiet weak encryption requested
1:main│ │ │ └─ algo="xor"
1:main│ │ ├─┬─ Xms DEBUG quiet response sent
1:main│ │ │ └─ length=8
1:main│ │ ├─ Xms DEBUG quiet disconnected
1:main│ ├─┘
1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230
1:main│ │ ├─ Xms DEBUG quiet message received, length=5
1:main│ │ ├─ Xms DEBUG quiet response sent, length=8
1:main│ │ ├─┬─ Xms DEBUG quiet message received
1:main│ │ │ └─ length=5
1:main│ │ ├─┬─ Xms DEBUG quiet response sent
1:main│ │ │ └─ length=8
1:main│ │ ├─ Xms DEBUG quiet disconnected
1:main│ ├─┘
1:main│ ├─ Xs WARN quiet internal error
Expand Down
19 changes: 17 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const LINE_HORIZ: &str = "─";
pub(crate) const LINE_BRANCH: &str = "├";
pub(crate) const LINE_CLOSE: &str = "┘";
pub(crate) const LINE_OPEN: &str = "┐";
pub(crate) const ARGS_BRANCH: &str = "┬";

#[derive(Copy, Clone)]
pub(crate) enum SpanMode {
Expand Down Expand Up @@ -227,7 +228,7 @@ pub struct FmtEvent<'a> {
impl<'a> Visit for FmtEvent<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
let buf = &mut self.bufs.current_buf;
let comma = if self.comma { "," } else { "" };
let comma = if self.comma { "\n" } else { "" };
match field.name() {
"message" => {
write!(buf, "{} {:?}", comma, value).unwrap();
Expand Down Expand Up @@ -372,6 +373,12 @@ fn indent_block_with_lines(
}
SpanMode::Event => {
buf.push_str(LINE_BRANCH);
if lines.len() > 1 {
for _ in 0..(indent_amount - 1) {
buf.push_str(LINE_HORIZ);
}
buf.push_str(ARGS_BRANCH);
}

// add `indent_amount - 1` horizontal lines before the span/event
for _ in 0..(indent_amount - 1) {
Expand All @@ -393,8 +400,16 @@ fn indent_block_with_lines(
}

// add all of the actual content, with each line preceded by the indent string
for line in &lines[1..] {
for (i, line) in lines[1..].iter().enumerate() {
buf.push_str(&s);
// Magic number `2` means "last entry" because we iterate from `1`
// and then restart indexing at `0`.
if i == lines.len() - 2 {
buf.push_str("└");
} else {
buf.push_str(LINE_BRANCH);
}
buf.push_str(LINE_HORIZ);
buf.push_str(line);
buf.push('\n');
}
Expand Down

0 comments on commit 73e521d

Please sign in to comment.