Skip to content

Commit 36bf36c

Browse files
authored
Allow rendering the event/span target (#11)
1 parent 99c5f06 commit 36bf36c

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ authors = ["David Barsky <me@davidbarsky.com>", "Nathan Whitaker"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
77
description = "a tracing layer that represents prints a heirarchal tree of spans and events."
8+
repository = "https://github.com/davidbarsky/tracing-tree"
9+
readme = "README.md"
810

911
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1012

examples/basic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use tracing_tree::HierarchicalLayer;
55
fn main() {
66
let layer = HierarchicalLayer::default()
77
.with_indent_lines(true)
8-
.with_indent_amount(2);
8+
.with_indent_amount(2)
9+
.with_targets(true);
910

1011
let subscriber = Registry::default().with(layer);
1112
tracing::subscriber::set_global_default(subscriber).unwrap();

src/format.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ const LINE_BRANCH: &str = "├";
1414

1515
#[derive(Debug)]
1616
pub struct Config {
17+
/// Whether to use colors.
1718
pub ansi: bool,
19+
/// Whether an ascii art tree is used or (if false) whether to just use whitespace indent
1820
pub indent_lines: bool,
21+
/// The amount of chars to indent.
1922
pub indent_amount: usize,
23+
/// Whether to show the module paths.
24+
pub targets: bool,
2025
}
2126

2227
impl Config {
@@ -30,6 +35,10 @@ impl Config {
3035
..self
3136
}
3237
}
38+
39+
pub fn with_targets(self, targets: bool) -> Self {
40+
Self { targets, ..self }
41+
}
3342
}
3443

3544
impl Default for Config {
@@ -38,6 +47,7 @@ impl Default for Config {
3847
ansi: true,
3948
indent_lines: false,
4049
indent_amount: 2,
50+
targets: false,
4151
}
4252
}
4353
}

src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl Default for HierarchicalLayer {
5858
let config = Config {
5959
ansi,
6060
indent_amount,
61+
targets: false,
6162
..Default::default()
6263
};
6364
Self {
@@ -88,6 +89,7 @@ impl<W> HierarchicalLayer<W>
8889
where
8990
W: MakeWriter + 'static,
9091
{
92+
/// Enables terminal colors, boldness and italics.
9193
pub fn with_ansi(self, ansi: bool) -> Self {
9294
Self {
9395
config: self.config.with_ansi(ansi),
@@ -114,13 +116,23 @@ where
114116
Self { config, ..self }
115117
}
116118

119+
/// Renders an ascii art tree instead of just using whitespace indentation.
117120
pub fn with_indent_lines(self, indent_lines: bool) -> Self {
118121
Self {
119122
config: self.config.with_indent_lines(indent_lines),
120123
..self
121124
}
122125
}
123126

127+
/// Whether to render the event and span targets. Usually targets are the module path to the
128+
/// event/span macro invocation.
129+
pub fn with_targets(self, targets: bool) -> Self {
130+
Self {
131+
config: self.config.with_targets(targets),
132+
..self
133+
}
134+
}
135+
124136
fn styled(&self, style: Style, text: impl AsRef<str>) -> String {
125137
if self.config.ansi {
126138
style.paint(text.as_ref()).to_string()
@@ -173,6 +185,16 @@ where
173185

174186
let indent = ctx.scope().count().saturating_sub(1);
175187

188+
if self.config.targets {
189+
let target = span.metadata().target();
190+
write!(
191+
&mut current_buf,
192+
"{}::",
193+
self.styled(Style::new().dimmed(), target,),
194+
)
195+
.expect("Unable to write to buffer");
196+
}
197+
176198
write!(
177199
current_buf,
178200
"{name}",
@@ -249,6 +271,17 @@ where
249271
level.to_string()
250272
};
251273
write!(&mut event_buf, "{level}", level = level).expect("Unable to write to buffer");
274+
275+
if self.config.targets {
276+
let target = event.metadata().target();
277+
write!(
278+
&mut event_buf,
279+
" {}",
280+
self.styled(Style::new().dimmed(), target,),
281+
)
282+
.expect("Unable to write to buffer");
283+
}
284+
252285
let mut visitor = FmtEvent {
253286
comma: false,
254287
bufs: &mut bufs,

0 commit comments

Comments
 (0)