From d3a8f492ba172d3bec1588a09f9af39bd5fe34a6 Mon Sep 17 00:00:00 2001 From: "Morley, Jonathan" Date: Mon, 11 Apr 2022 10:05:50 -0400 Subject: [PATCH 1/3] remove newlines from non-line-indented output --- examples/quiet.rs | 1 - examples/quiet.stdout | 49 +++++++++++++++++++------------------------ src/format.rs | 3 ++- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/examples/quiet.rs b/examples/quiet.rs index 81b0c9c..288e560 100644 --- a/examples/quiet.rs +++ b/examples/quiet.rs @@ -5,7 +5,6 @@ use tracing_tree::HierarchicalLayer; fn main() { let layer = HierarchicalLayer::default() .with_writer(std::io::stdout) - .with_indent_lines(true) .with_indent_amount(2) .with_thread_names(true) .with_thread_ids(true) diff --git a/examples/quiet.stdout b/examples/quiet.stdout index 52160cb..f801c32 100644 --- a/examples/quiet.stdout +++ b/examples/quiet.stdout @@ -1,28 +1,21 @@ -1:main┐quiet::hierarchical-example version=0.1 -1:main├─┐quiet::server host="localhost", port=8080 -1:main│ ├─ Xms INFO quiet starting -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│ ├─┘ -1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230 -1:main│ │ ├─ Xms DEBUG quiet connected -1:main│ ├─┘ -1:main│ ├─┐quiet::foomp 42 <- format string, normal_var=43 -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 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 disconnected -1:main│ ├─┘ -1:main│ ├─ Xs WARN quiet internal error -1:main│ ├─ Xs INFO quiet exit -1:main├─┘ -1:main┘ +1:mainquiet::hierarchical-example version=0.1 +1:main quiet::server host="localhost", port=8080 +1:main Xms INFO quiet starting +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 quiet::conn peer_addr="8.8.8.8", port=18230 +1:main Xms DEBUG quiet connected +1:main quiet::foomp 42 <- format string, normal_var=43 +1:main Xms ERROR quiet hello +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 DEBUG quiet disconnected +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 disconnected +1:main Xs WARN quiet internal error +1:main Xs INFO quiet exit diff --git a/src/format.rs b/src/format.rs index 895c04b..47f8b1a 100644 --- a/src/format.rs +++ b/src/format.rs @@ -167,11 +167,12 @@ impl Buffers { } pub(crate) fn indent_current(&mut self, indent: usize, config: &Config, style: SpanMode) { - self.current_buf.push('\n'); let prefix = config.prefix(); // Render something when wraparound occurs so the user is aware of it if config.indent_lines { + self.current_buf.push('\n'); + match style { SpanMode::Close { .. } | SpanMode::PostClose => { if indent > 0 && (indent + 1) % config.wraparound == 0 { From 5b22ee508ac3f3edacffaf2af334e66bb8f6fb9f Mon Sep 17 00:00:00 2001 From: Jonathan Morley Date: Thu, 18 May 2023 12:19:08 -0400 Subject: [PATCH 2/3] add new example --- examples/no-indent.rs | 79 +++++++++++++++++++++++++++++++++++++++ examples/no-indent.stdout | 21 +++++++++++ examples/quiet.rs | 1 + examples/quiet.stdout | 49 +++++++++++++----------- 4 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 examples/no-indent.rs create mode 100644 examples/no-indent.stdout diff --git a/examples/no-indent.rs b/examples/no-indent.rs new file mode 100644 index 0000000..288e560 --- /dev/null +++ b/examples/no-indent.rs @@ -0,0 +1,79 @@ +use tracing::{debug, error, info, instrument, span, warn, Level}; +use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; +use tracing_tree::HierarchicalLayer; + +fn main() { + let layer = HierarchicalLayer::default() + .with_writer(std::io::stdout) + .with_indent_amount(2) + .with_thread_names(true) + .with_thread_ids(true) + .with_verbose_exit(false) + .with_verbose_entry(false) + .with_targets(true); + + let subscriber = Registry::default().with(layer); + tracing::subscriber::set_global_default(subscriber).unwrap(); + + let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1); + let _e = app_span.enter(); + + let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080); + let _e2 = server_span.enter(); + info!("starting"); + std::thread::sleep(std::time::Duration::from_millis(300)); + info!("listening"); + let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381); + peer1.in_scope(|| { + debug!("connected"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 2, "message received"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!("connected"); + }); + drop(peer2); + let peer3 = span!( + Level::TRACE, + "foomp", + normal_var = 43, + "{} <- format string", + 42 + ); + 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"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer1); + let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230); + peer2.in_scope(|| { + debug!(length = 5, "message received"); + std::thread::sleep(std::time::Duration::from_millis(300)); + debug!(length = 8, "response sent"); + debug!("disconnected"); + }); + drop(peer2); + warn!("internal error"); + info!("exit"); +} + +#[instrument] +fn call_a(name: &str) { + info!(name, "got a name"); + call_b(name) +} + +#[instrument] +fn call_b(name: &str) { + info!(name, "got a name"); +} diff --git a/examples/no-indent.stdout b/examples/no-indent.stdout new file mode 100644 index 0000000..c883bee --- /dev/null +++ b/examples/no-indent.stdout @@ -0,0 +1,21 @@ +1:mainno_indent::hierarchical-example version=0.1 +1:main no_indent::server host="localhost", port=8080 +1:main Xms INFO no_indent starting +1:main Xms INFO no_indent listening +1:main no_indent::conn peer_addr="82.9.9.9", port=42381 +1:main Xms DEBUG no_indent connected +1:main Xms DEBUG no_indent message received, length=2 +1:main no_indent::conn peer_addr="8.8.8.8", port=18230 +1:main Xms DEBUG no_indent connected +1:main no_indent::foomp 42 <- format string, normal_var=43 +1:main Xms ERROR no_indent hello +1:main no_indent::conn peer_addr="82.9.9.9", port=42381 +1:main Xms WARN no_indent weak encryption requested, algo="xor" +1:main Xms DEBUG no_indent response sent, length=8 +1:main Xms DEBUG no_indent disconnected +1:main no_indent::conn peer_addr="8.8.8.8", port=18230 +1:main Xms DEBUG no_indent message received, length=5 +1:main Xms DEBUG no_indent response sent, length=8 +1:main Xms DEBUG no_indent disconnected +1:main Xs WARN no_indent internal error +1:main Xs INFO no_indent exit diff --git a/examples/quiet.rs b/examples/quiet.rs index 288e560..81b0c9c 100644 --- a/examples/quiet.rs +++ b/examples/quiet.rs @@ -5,6 +5,7 @@ use tracing_tree::HierarchicalLayer; fn main() { let layer = HierarchicalLayer::default() .with_writer(std::io::stdout) + .with_indent_lines(true) .with_indent_amount(2) .with_thread_names(true) .with_thread_ids(true) diff --git a/examples/quiet.stdout b/examples/quiet.stdout index f801c32..52160cb 100644 --- a/examples/quiet.stdout +++ b/examples/quiet.stdout @@ -1,21 +1,28 @@ -1:mainquiet::hierarchical-example version=0.1 -1:main quiet::server host="localhost", port=8080 -1:main Xms INFO quiet starting -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 quiet::conn peer_addr="8.8.8.8", port=18230 -1:main Xms DEBUG quiet connected -1:main quiet::foomp 42 <- format string, normal_var=43 -1:main Xms ERROR quiet hello -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 DEBUG quiet disconnected -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 disconnected -1:main Xs WARN quiet internal error -1:main Xs INFO quiet exit +1:main┐quiet::hierarchical-example version=0.1 +1:main├─┐quiet::server host="localhost", port=8080 +1:main│ ├─ Xms INFO quiet starting +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│ ├─┘ +1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230 +1:main│ │ ├─ Xms DEBUG quiet connected +1:main│ ├─┘ +1:main│ ├─┐quiet::foomp 42 <- format string, normal_var=43 +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 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 disconnected +1:main│ ├─┘ +1:main│ ├─ Xs WARN quiet internal error +1:main│ ├─ Xs INFO quiet exit +1:main├─┘ +1:main┘ From 396a36bc92241edcfdd7c3abd1a5bed2347c4be4 Mon Sep 17 00:00:00 2001 From: Jonathan Morley Date: Thu, 18 May 2023 12:26:24 -0400 Subject: [PATCH 3/3] add an extra space for padding on the first line --- examples/no-indent.stdout | 42 +++++++++++++++++++-------------------- src/format.rs | 1 + 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/no-indent.stdout b/examples/no-indent.stdout index c883bee..1339211 100644 --- a/examples/no-indent.stdout +++ b/examples/no-indent.stdout @@ -1,21 +1,21 @@ -1:mainno_indent::hierarchical-example version=0.1 -1:main no_indent::server host="localhost", port=8080 -1:main Xms INFO no_indent starting -1:main Xms INFO no_indent listening -1:main no_indent::conn peer_addr="82.9.9.9", port=42381 -1:main Xms DEBUG no_indent connected -1:main Xms DEBUG no_indent message received, length=2 -1:main no_indent::conn peer_addr="8.8.8.8", port=18230 -1:main Xms DEBUG no_indent connected -1:main no_indent::foomp 42 <- format string, normal_var=43 -1:main Xms ERROR no_indent hello -1:main no_indent::conn peer_addr="82.9.9.9", port=42381 -1:main Xms WARN no_indent weak encryption requested, algo="xor" -1:main Xms DEBUG no_indent response sent, length=8 -1:main Xms DEBUG no_indent disconnected -1:main no_indent::conn peer_addr="8.8.8.8", port=18230 -1:main Xms DEBUG no_indent message received, length=5 -1:main Xms DEBUG no_indent response sent, length=8 -1:main Xms DEBUG no_indent disconnected -1:main Xs WARN no_indent internal error -1:main Xs INFO no_indent exit +1:main no_indent::hierarchical-example version=0.1 +1:main no_indent::server host="localhost", port=8080 +1:main Xms INFO no_indent starting +1:main Xms INFO no_indent listening +1:main no_indent::conn peer_addr="82.9.9.9", port=42381 +1:main Xms DEBUG no_indent connected +1:main Xms DEBUG no_indent message received, length=2 +1:main no_indent::conn peer_addr="8.8.8.8", port=18230 +1:main Xms DEBUG no_indent connected +1:main no_indent::foomp 42 <- format string, normal_var=43 +1:main Xms ERROR no_indent hello +1:main no_indent::conn peer_addr="82.9.9.9", port=42381 +1:main Xms WARN no_indent weak encryption requested, algo="xor" +1:main Xms DEBUG no_indent response sent, length=8 +1:main Xms DEBUG no_indent disconnected +1:main no_indent::conn peer_addr="8.8.8.8", port=18230 +1:main Xms DEBUG no_indent message received, length=5 +1:main Xms DEBUG no_indent response sent, length=8 +1:main Xms DEBUG no_indent disconnected +1:main Xs WARN no_indent internal error +1:main Xs INFO no_indent exit diff --git a/src/format.rs b/src/format.rs index 47f8b1a..aa56178 100644 --- a/src/format.rs +++ b/src/format.rs @@ -418,6 +418,7 @@ fn indent_block( let indent_str = String::from(" ").repeat(indent_spaces); for line in lines { buf.push_str(prefix); + buf.push(' '); buf.push_str(&indent_str); buf.push_str(line); buf.push('\n');