diff --git a/examples/basic.rs b/examples/basic.rs index 91e905b..9a5d91a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,4 +1,4 @@ -use tracing::{debug, info, instrument, span, warn, Level}; +use tracing::{debug, error, info, instrument, span, warn, Level}; use tracing_subscriber::{layer::SubscriberExt, registry::Registry}; use tracing_tree::HierarchicalLayer; @@ -34,6 +34,16 @@ fn main() { std::thread::sleep(std::time::Duration::from_millis(300)); debug!("connected"); }); + let peer3 = span!( + Level::TRACE, + "foomp", + normal_var = 43, + "{} <- format string", + 42 + ); + peer3.in_scope(|| { + error!("hello"); + }); peer1.in_scope(|| { warn!(algo = "xor", "weak encryption requested"); std::thread::sleep(std::time::Duration::from_millis(300)); diff --git a/examples/basic.stdout b/examples/basic.stdout index b43c28e..aafc4b9 100644 --- a/examples/basic.stdout +++ b/examples/basic.stdout @@ -1,35 +1,40 @@ -1:mainbasic::hierarchical-example{version=0.1} -1:main├┐basic::hierarchical-example{version=0.1} -1:main│└┐basic::server{host="localhost", port=8080} +1:mainbasic::hierarchical-example version=0.1 +1:main├┐basic::hierarchical-example version=0.1 +1:main│└┐basic::server host="localhost", port=8080 1:main│ ├─ms INFO basic starting 1:main│ ├─ms INFO basic listening -1:main│ ├┐basic::server{host="localhost", port=8080} -1:main│ │└┐basic::conn{peer_addr="82.9.9.9", port=42381} +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::conn peer_addr="82.9.9.9", port=42381 1:main│ │ ├─ms DEBUG basic connected 1:main│ │ ├─ms DEBUG basic message received, 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} -1:main│ │└┐basic::conn{peer_addr="8.8.8.8", port=18230} +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│ │ ├─ms DEBUG basic connected -1:main│ │┌┘basic::conn{peer_addr="8.8.8.8", port=18230} -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│ │┌┘basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ ├┘basic::server host="localhost", port=8080 +1:main│ ├┐basic::server host="localhost", port=8080 +1:main│ │└┐basic::foomp 42 <- format string, normal_var=43 +1:main│ │ ├─ms ERROR basic hello +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::conn peer_addr="82.9.9.9", port=42381 1:main│ │ ├─ms WARN basic weak encryption requested, algo="xor" 1:main│ │ ├─ms DEBUG basic response sent, length=8 1:main│ │ ├─ms 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│ │┌┘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│ │ ├─ms DEBUG basic message received, length=5 1:main│ │ ├─ms DEBUG basic response sent, length=8 1:main│ │ ├─ms DEBUG basic disconnected -1:main│ │┌┘basic::conn{peer_addr="8.8.8.8", port=18230} -1:main│ ├┘basic::server{host="localhost", port=8080} +1:main│ │┌┘basic::conn peer_addr="8.8.8.8", port=18230 +1:main│ ├┘basic::server host="localhost", port=8080 1:main│ ├─ms WARN basic internal error 1:main│ ├─ms INFO basic exit -1:main│┌┘basic::server{host="localhost", port=8080} -1:main├┘basic::hierarchical-example{version=0.1} -1:mainbasic::hierarchical-example{version=0.1} +1:main│┌┘basic::server host="localhost", port=8080 +1:main├┘basic::hierarchical-example version=0.1 +1:mainbasic::hierarchical-example version=0.1 diff --git a/examples/stderr.rs b/examples/stderr.rs index 071048c..ab9868c 100644 --- a/examples/stderr.rs +++ b/examples/stderr.rs @@ -29,6 +29,7 @@ fn main() { let layer = HierarchicalLayer::default() .with_indent_lines(true) .with_indent_amount(2) + .with_bracketed_fields(true) .with_writer(std::io::stderr); let subscriber = Registry::default().with(layer); diff --git a/examples/wraparound.stdout b/examples/wraparound.stdout index 6268580..018cb96 100644 --- a/examples/wraparound.stdout +++ b/examples/wraparound.stdout @@ -1,67 +1,67 @@ -1:mainwraparound::recurse{i=0} +1:mainwraparound::recurse i=0 1:main├─ms WARN wraparound boop -1:main├┐wraparound::recurse{i=0} -1:main│└┐wraparound::recurse{i=1} +1:main├┐wraparound::recurse i=0 +1:main│└┐wraparound::recurse i=1 1:main│ ├─ms WARN wraparound boop -1:main│ ├┐wraparound::recurse{i=1} -1:main│ │└┐wraparound::recurse{i=2} +1:main│ ├┐wraparound::recurse i=1 +1:main│ │└┐wraparound::recurse i=2 1:main│ │ ├─ms WARN wraparound boop -1:main│ │ ├┐wraparound::recurse{i=2} -1:main│ │ │└┐wraparound::recurse{i=3} +1:main│ │ ├┐wraparound::recurse i=2 +1:main│ │ │└┐wraparound::recurse i=3 1:main│ │ │ ├─ms WARN wraparound boop -1:mainwraparound::recurse{i=3} -1:mainwraparound::recurse{i=4} +1:mainwraparound::recurse i=3 +1:mainwraparound::recurse i=4 1:mainms WARN wraparound boop -1:mainwraparound::recurse{i=4} -1:mainwraparound::recurse{i=5} +1:mainwraparound::recurse i=4 +1:mainwraparound::recurse i=5 1:main├─ms WARN wraparound boop -1:main├┐wraparound::recurse{i=5} -1:main│└┐wraparound::recurse{i=6} +1:main├┐wraparound::recurse i=5 +1:main│└┐wraparound::recurse i=6 1:main│ ├─ms WARN wraparound boop -1:main│ ├┐wraparound::recurse{i=6} -1:main│ │└┐wraparound::recurse{i=7} +1:main│ ├┐wraparound::recurse i=6 +1:main│ │└┐wraparound::recurse i=7 1:main│ │ ├─ms WARN wraparound boop -1:main│ │ ├┐wraparound::recurse{i=7} -1:main│ │ │└┐wraparound::recurse{i=8} +1:main│ │ ├┐wraparound::recurse i=7 +1:main│ │ │└┐wraparound::recurse i=8 1:main│ │ │ ├─ms WARN wraparound boop -1:mainwraparound::recurse{i=8} -1:mainwraparound::recurse{i=9} +1:mainwraparound::recurse i=8 +1:mainwraparound::recurse i=9 1:mainms WARN wraparound boop -1:mainwraparound::recurse{i=9} -1:mainwraparound::recurse{i=10} +1:mainwraparound::recurse i=9 +1:mainwraparound::recurse i=10 1:main├─ms WARN wraparound boop -1:main├┐wraparound::recurse{i=10} -1:main│└┐wraparound::recurse{i=11} +1:main├┐wraparound::recurse i=10 +1:main│└┐wraparound::recurse i=11 1:main│ ├─ms WARN wraparound boop -1:main│ ├┐wraparound::recurse{i=11} -1:main│ │└┐wraparound::recurse{i=12} +1:main│ ├┐wraparound::recurse i=11 +1:main│ │└┐wraparound::recurse i=12 1:main│ │ ├─ms WARN wraparound boop -1:main│ │ ├┐wraparound::recurse{i=12} -1:main│ │ │└┐wraparound::recurse{i=13} +1:main│ │ ├┐wraparound::recurse i=12 +1:main│ │ │└┐wraparound::recurse i=13 1:main│ │ │ ├─ms WARN wraparound boop -1:mainwraparound::recurse{i=13} -1:mainwraparound::recurse{i=14} +1:mainwraparound::recurse i=13 +1:mainwraparound::recurse i=14 1:mainms WARN wraparound boop -1:mainwraparound::recurse{i=14} -1:mainwraparound::recurse{i=15} +1:mainwraparound::recurse i=14 +1:mainwraparound::recurse i=15 1:main├─ms WARN wraparound boop -1:main├┐wraparound::recurse{i=15} -1:main│└┐wraparound::recurse{i=16} +1:main├┐wraparound::recurse i=15 +1:main│└┐wraparound::recurse i=16 1:main│ ├─ms WARN wraparound boop -1:main│ ├┐wraparound::recurse{i=16} -1:main│ │└┐wraparound::recurse{i=17} +1:main│ ├┐wraparound::recurse i=16 +1:main│ │└┐wraparound::recurse i=17 1:main│ │ ├─ms WARN wraparound boop -1:main│ │ ├┐wraparound::recurse{i=17} -1:main│ │ │└┐wraparound::recurse{i=18} +1:main│ │ ├┐wraparound::recurse i=17 +1:main│ │ │└┐wraparound::recurse i=18 1:main│ │ │ ├─ms WARN wraparound boop -1:mainwraparound::recurse{i=18} -1:mainwraparound::recurse{i=19} +1:mainwraparound::recurse i=18 +1:mainwraparound::recurse i=19 1:mainms WARN wraparound boop -1:mainwraparound::recurse{i=19} -1:mainwraparound::recurse{i=20} +1:mainwraparound::recurse i=19 +1:mainwraparound::recurse i=20 1:main├─ms WARN wraparound boop -1:main├┐wraparound::recurse{i=20} -1:main│└┐wraparound::recurse{i=21} +1:main├┐wraparound::recurse i=20 +1:main│└┐wraparound::recurse i=21 1:main│ ├─ms WARN wraparound boop 1:main│ ├─ms WARN wraparound bop 1:main├─ms WARN wraparound bop diff --git a/src/format.rs b/src/format.rs index 2cfd7b9..74ff1f4 100644 --- a/src/format.rs +++ b/src/format.rs @@ -42,6 +42,8 @@ pub struct Config { pub verbose_entry: bool, /// Whether to print the current span before exiting it. pub verbose_exit: bool, + /// Whether to print squiggly brackets (`{}`) around the list of fields in a span. + pub bracketed_fields: bool, } impl Config { @@ -92,6 +94,13 @@ impl Config { } } + pub fn with_bracketed_fields(self, bracketed_fields: bool) -> Self { + Self { + bracketed_fields, + ..self + } + } + pub(crate) fn prefix(&self) -> String { let mut buf = String::new(); if self.render_thread_ids { @@ -127,6 +136,7 @@ impl Default for Config { wraparound: usize::max_value(), verbose_entry: false, verbose_exit: false, + bracketed_fields: false, } } } diff --git a/src/lib.rs b/src/lib.rs index 6fb97ce..9ffdb56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,6 +169,15 @@ where } } + /// Whether to print `{}` around the fields when printing a span. + /// This can help visually distinguish fields from the rest of the message. + pub fn with_bracketed_fields(self, bracketed_fields: bool) -> Self { + Self { + config: self.config.with_bracketed_fields(bracketed_fields), + ..self + } + } + fn styled(&self, style: Style, text: impl AsRef) -> String { if self.config.ansi { style.paint(text.as_ref()).to_string() @@ -177,23 +186,21 @@ where } } - fn print_kvs<'a, I, K, V>( - &self, - buf: &mut impl fmt::Write, - kvs: I, - leading: &str, - ) -> fmt::Result + fn print_kvs<'a, I, V>(&self, buf: &mut impl fmt::Write, kvs: I) -> fmt::Result where - I: IntoIterator, - K: AsRef + 'a, + I: IntoIterator, V: fmt::Display + 'a, { let mut kvs = kvs.into_iter(); if let Some((k, v)) = kvs.next() { - write!(buf, "{}{}={}", leading, k.as_ref(), v)?; + if k == "message" { + write!(buf, "{}", v)?; + } else { + write!(buf, "{}={}", k, v)?; + } } for (k, v) in kvs { - write!(buf, ", {}={}", k.as_ref(), v)?; + write!(buf, ", {}={}", k, v)?; } Ok(()) } @@ -238,20 +245,26 @@ where name = self.styled(Style::new().fg(Color::Green).bold(), span.metadata().name()) ) .unwrap(); - write!( - current_buf, - "{}", - self.styled(Style::new().fg(Color::Green).bold(), "{") // Style::new().fg(Color::Green).dimmed().paint("{") - ) - .unwrap(); - self.print_kvs(&mut current_buf, data.kvs.iter().map(|(k, v)| (k, v)), "") + if self.config.bracketed_fields { + write!( + current_buf, + "{}", + self.styled(Style::new().fg(Color::Green).bold(), "{") // Style::new().fg(Color::Green).dimmed().paint("{") + ) .unwrap(); - write!( - current_buf, - "{}", - self.styled(Style::new().fg(Color::Green).bold(), "}") // Style::new().dimmed().paint("}") - ) - .unwrap(); + } else { + write!(current_buf, " ").unwrap(); + } + self.print_kvs(&mut current_buf, data.kvs.iter().map(|(k, v)| (*k, v))) + .unwrap(); + if self.config.bracketed_fields { + write!( + current_buf, + "{}", + self.styled(Style::new().fg(Color::Green).bold(), "}") // Style::new().dimmed().paint("}") + ) + .unwrap(); + } bufs.indent_current(indent, &self.config, style); let writer = self.make_writer.make_writer();