Skip to content

Commit

Permalink
Remove brackets and message field name from rendered message (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk authored Sep 28, 2020
1 parent 5977c7f commit b7a839f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 89 deletions.
12 changes: 11 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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));
Expand Down
49 changes: 27 additions & 22 deletions examples/basic.stdout
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions examples/stderr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
86 changes: 43 additions & 43 deletions examples/wraparound.stdout
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -127,6 +136,7 @@ impl Default for Config {
wraparound: usize::max_value(),
verbose_entry: false,
verbose_exit: false,
bracketed_fields: false,
}
}
}
Expand Down
59 changes: 36 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>) -> String {
if self.config.ansi {
style.paint(text.as_ref()).to_string()
Expand All @@ -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<Item = (K, V)>,
K: AsRef<str> + 'a,
I: IntoIterator<Item = (&'a str, V)>,
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(())
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b7a839f

Please sign in to comment.