Skip to content

Commit 876f942

Browse files
authored
Make tracing-tree usable in contexts with span re-entry (#27)
This PR: - Connects the verbose entry/exit configuration to the formatters. - Skips emitting span entry/exit information
1 parent c613581 commit 876f942

File tree

7 files changed

+291
-179
lines changed

7 files changed

+291
-179
lines changed

examples/basic.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ fn main() {
3131
std::thread::sleep(std::time::Duration::from_millis(300));
3232
debug!(length = 2, "message received");
3333
});
34+
drop(peer1);
3435
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
3536
peer2.in_scope(|| {
3637
std::thread::sleep(std::time::Duration::from_millis(300));
3738
debug!("connected");
3839
});
40+
drop(peer2);
3941
let peer3 = span!(
4042
Level::TRACE,
4143
"foomp",
@@ -46,18 +48,23 @@ fn main() {
4648
peer3.in_scope(|| {
4749
error!("hello");
4850
});
51+
drop(peer3);
52+
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
4953
peer1.in_scope(|| {
5054
warn!(algo = "xor", "weak encryption requested");
5155
std::thread::sleep(std::time::Duration::from_millis(300));
5256
debug!(length = 8, "response sent");
5357
debug!("disconnected");
5458
});
59+
drop(peer1);
60+
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
5561
peer2.in_scope(|| {
5662
debug!(length = 5, "message received");
5763
std::thread::sleep(std::time::Duration::from_millis(300));
5864
debug!(length = 8, "response sent");
5965
debug!("disconnected");
6066
});
67+
drop(peer2);
6168
warn!("internal error");
6269
log::error!("this is a log message");
6370
info!("exit");

examples/quiet.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use tracing::{debug, error, info, instrument, span, warn, Level};
2+
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
3+
use tracing_tree::HierarchicalLayer;
4+
5+
fn main() {
6+
let layer = HierarchicalLayer::default()
7+
.with_indent_lines(true)
8+
.with_indent_amount(2)
9+
.with_thread_names(true)
10+
.with_thread_ids(true)
11+
.with_verbose_exit(false)
12+
.with_verbose_entry(false)
13+
.with_targets(true);
14+
15+
let subscriber = Registry::default().with(layer);
16+
tracing::subscriber::set_global_default(subscriber).unwrap();
17+
18+
let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1);
19+
let _e = app_span.enter();
20+
21+
let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080);
22+
let _e2 = server_span.enter();
23+
info!("starting");
24+
std::thread::sleep(std::time::Duration::from_millis(300));
25+
info!("listening");
26+
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
27+
peer1.in_scope(|| {
28+
debug!("connected");
29+
std::thread::sleep(std::time::Duration::from_millis(300));
30+
debug!(length = 2, "message received");
31+
});
32+
drop(peer1);
33+
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
34+
peer2.in_scope(|| {
35+
std::thread::sleep(std::time::Duration::from_millis(300));
36+
debug!("connected");
37+
});
38+
drop(peer2);
39+
let peer3 = span!(
40+
Level::TRACE,
41+
"foomp",
42+
normal_var = 43,
43+
"{} <- format string",
44+
42
45+
);
46+
peer3.in_scope(|| {
47+
error!("hello");
48+
});
49+
drop(peer3);
50+
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
51+
peer1.in_scope(|| {
52+
warn!(algo = "xor", "weak encryption requested");
53+
std::thread::sleep(std::time::Duration::from_millis(300));
54+
debug!(length = 8, "response sent");
55+
debug!("disconnected");
56+
});
57+
drop(peer1);
58+
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
59+
peer2.in_scope(|| {
60+
debug!(length = 5, "message received");
61+
std::thread::sleep(std::time::Duration::from_millis(300));
62+
debug!(length = 8, "response sent");
63+
debug!("disconnected");
64+
});
65+
drop(peer2);
66+
warn!("internal error");
67+
info!("exit");
68+
}
69+
70+
#[instrument]
71+
fn call_a(name: &str) {
72+
info!(name, "got a name");
73+
call_b(name)
74+
}
75+
76+
#[instrument]
77+
fn call_b(name: &str) {
78+
info!(name, "got a name");
79+
}

examples/quiet.stdout

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1:mainquiet::hierarchical-example version=0.1
2+
1:main├─┐quiet::server host="localhost", port=8080
3+
1:main│ ├─ms INFO quiet starting
4+
1:main│ ├─ms INFO quiet listening
5+
1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381
6+
1:main│ │ ├─ms DEBUG quiet connected
7+
1:main│ │ ├─ms DEBUG quiet message received, length=2
8+
1:main│ ├─┘
9+
1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230
10+
1:main│ │ ├─ms DEBUG quiet connected
11+
1:main│ ├─┘
12+
1:main│ ├─┐quiet::foomp 42 <- format string, normal_var=43
13+
1:main│ │ ├─ms ERROR quiet hello
14+
1:main│ ├─┘
15+
1:main│ ├─┐quiet::conn peer_addr="82.9.9.9", port=42381
16+
1:main│ │ ├─ms WARN quiet weak encryption requested, algo="xor"
17+
1:main│ │ ├─ms DEBUG quiet response sent, length=8
18+
1:main│ │ ├─ms DEBUG quiet disconnected
19+
1:main│ ├─┘
20+
1:main│ ├─┐quiet::conn peer_addr="8.8.8.8", port=18230
21+
1:main│ │ ├─ms DEBUG quiet message received, length=5
22+
1:main│ │ ├─ms DEBUG quiet response sent, length=8
23+
1:main│ │ ├─ms DEBUG quiet disconnected
24+
1:main│ ├─┘
25+
1:main│ ├─ms WARN quiet internal error
26+
1:main│ ├─ms INFO quiet exit
27+
1:main├─┘
28+
1:main

examples/stderr.stderr

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,111 @@
11
fibonacci_seq{to=5}
22
├─ms DEBUG Pushing 0 fibonacci
3-
├┐fibonacci_seq{to=5}
4-
│└┐nth_fibonacci{n=0}
3+
├─┐nth_fibonacci{n=0}
54
│ ├─ms DEBUG Base case
5+
├─┘
66
├─ms DEBUG Pushing 1 fibonacci
7-
├┐fibonacci_seq{to=5}
8-
│└┐nth_fibonacci{n=1}
7+
├─┐nth_fibonacci{n=1}
98
│ ├─ms DEBUG Base case
9+
├─┘
1010
├─ms DEBUG Pushing 2 fibonacci
11-
├┐fibonacci_seq{to=5}
12-
│└┐nth_fibonacci{n=2}
11+
├─┐nth_fibonacci{n=2}
1312
│ ├─ms DEBUG Recursing
14-
│ ├┐nth_fibonacci{n=2}
15-
│ │└┐nth_fibonacci{n=1}
13+
│ ├─┐nth_fibonacci{n=1}
1614
│ │ ├─ms DEBUG Base case
17-
│ ├┐nth_fibonacci{n=2}
18-
│└┐nth_fibonacci{n=0}
15+
│ ├─┘
16+
├─┐nth_fibonacci{n=0}
1917
│ │ ├─ms DEBUG Base case
18+
│ ├─┘
19+
├─┘
2020
├─ms DEBUG Pushing 3 fibonacci
21-
├┐fibonacci_seq{to=5}
22-
│└┐nth_fibonacci{n=3}
21+
├─┐nth_fibonacci{n=3}
2322
│ ├─ms DEBUG Recursing
24-
│ ├┐nth_fibonacci{n=3}
25-
│ │└┐nth_fibonacci{n=2}
23+
│ ├─┐nth_fibonacci{n=2}
2624
│ │ ├─ms DEBUG Recursing
27-
│ │ ├┐nth_fibonacci{n=2}
28-
│ │ │└┐nth_fibonacci{n=1}
25+
│ │ ├─┐nth_fibonacci{n=1}
2926
│ │ │ ├─ms DEBUG Base case
30-
│ │ ├┐nth_fibonacci{n=2}
31-
│ │ │└┐nth_fibonacci{n=0}
27+
│ │ ├─┘
28+
│ │ ├─┐nth_fibonacci{n=0}
3229
│ │ │ ├─ms DEBUG Base case
33-
│ ├┐nth_fibonacci{n=3}
34-
│ │└┐nth_fibonacci{n=1}
30+
│ │ ├─┘
31+
│ ├─┘
32+
│ ├─┐nth_fibonacci{n=1}
3533
│ │ ├─ms DEBUG Base case
34+
│ ├─┘
35+
├─┘
3636
├─ms DEBUG Pushing 4 fibonacci
37-
├┐fibonacci_seq{to=5}
38-
│└┐nth_fibonacci{n=4}
37+
├─┐nth_fibonacci{n=4}
3938
│ ├─ms DEBUG Recursing
40-
│ ├┐nth_fibonacci{n=4}
41-
│ │└┐nth_fibonacci{n=3}
39+
│ ├─┐nth_fibonacci{n=3}
4240
│ │ ├─ms DEBUG Recursing
43-
│ │ ├┐nth_fibonacci{n=3}
44-
│ │ │└┐nth_fibonacci{n=2}
41+
│ │ ├─┐nth_fibonacci{n=2}
4542
│ │ │ ├─ms DEBUG Recursing
46-
│ │ │ ├┐nth_fibonacci{n=2}
47-
│ │ │ │└┐nth_fibonacci{n=1}
43+
│ │ │ ├─┐nth_fibonacci{n=1}
4844
│ │ │ │ ├─ms DEBUG Base case
49-
│ │ │ ├┐nth_fibonacci{n=2}
50-
│ │ │ │└┐nth_fibonacci{n=0}
45+
│ │ │ ├─┘
46+
│ │ │ ├─┐nth_fibonacci{n=0}
5147
│ │ │ │ ├─ms DEBUG Base case
52-
│ │ ├┐nth_fibonacci{n=3}
53-
│ │ │└┐nth_fibonacci{n=1}
48+
│ │ │ ├─┘
49+
│ │ ├─┘
50+
│ │ ├─┐nth_fibonacci{n=1}
5451
│ │ │ ├─ms DEBUG Base case
55-
│ ├┐nth_fibonacci{n=4}
56-
│ │└┐nth_fibonacci{n=2}
52+
│ │ ├─┘
53+
│ ├─┘
54+
│ ├─┐nth_fibonacci{n=2}
5755
│ │ ├─ms DEBUG Recursing
58-
│ │ ├┐nth_fibonacci{n=2}
59-
│ │ │└┐nth_fibonacci{n=1}
56+
│ │ ├─┐nth_fibonacci{n=1}
6057
│ │ │ ├─ms DEBUG Base case
61-
│ │ ├┐nth_fibonacci{n=2}
62-
│ │ │└┐nth_fibonacci{n=0}
58+
│ │ ├─┘
59+
│ │ ├─┐nth_fibonacci{n=0}
6360
│ │ │ ├─ms DEBUG Base case
61+
│ │ ├─┘
62+
│ ├─┘
63+
├─┘
6464
├─ms DEBUG Pushing 5 fibonacci
65-
├┐fibonacci_seq{to=5}
66-
│└┐nth_fibonacci{n=5}
65+
├─┐nth_fibonacci{n=5}
6766
│ ├─ms DEBUG Recursing
68-
│ ├┐nth_fibonacci{n=5}
69-
│ │└┐nth_fibonacci{n=4}
67+
│ ├─┐nth_fibonacci{n=4}
7068
│ │ ├─ms DEBUG Recursing
71-
│ │ ├┐nth_fibonacci{n=4}
72-
│ │ │└┐nth_fibonacci{n=3}
69+
│ │ ├─┐nth_fibonacci{n=3}
7370
│ │ │ ├─ms DEBUG Recursing
74-
│ │ │ ├┐nth_fibonacci{n=3}
75-
│ │ │ │└┐nth_fibonacci{n=2}
71+
│ │ │ ├─┐nth_fibonacci{n=2}
7672
│ │ │ │ ├─ms DEBUG Recursing
77-
│ │ │ │ ├┐nth_fibonacci{n=2}
78-
│ │ │ │ │└┐nth_fibonacci{n=1}
73+
│ │ │ │ ├─┐nth_fibonacci{n=1}
7974
│ │ │ │ │ ├─ms DEBUG Base case
80-
│ │ │ │ ├┐nth_fibonacci{n=2}
81-
│ │ │ │ │└┐nth_fibonacci{n=0}
75+
│ │ │ │ ├─┘
76+
│ │ │ │ ├─┐nth_fibonacci{n=0}
8277
│ │ │ │ │ ├─ms DEBUG Base case
83-
│ │ │ ├┐nth_fibonacci{n=3}
84-
│ │ │ │└┐nth_fibonacci{n=1}
78+
│ │ │ │ ├─┘
79+
│ │ │ ├─┘
80+
│ │ │ ├─┐nth_fibonacci{n=1}
8581
│ │ │ │ ├─ms DEBUG Base case
86-
│ │ ├┐nth_fibonacci{n=4}
87-
│ │ │└┐nth_fibonacci{n=2}
82+
│ │ │ ├─┘
83+
│ │ ├─┘
84+
│ │ ├─┐nth_fibonacci{n=2}
8885
│ │ │ ├─ms DEBUG Recursing
89-
│ │ │ ├┐nth_fibonacci{n=2}
90-
│ │ │ │└┐nth_fibonacci{n=1}
86+
│ │ │ ├─┐nth_fibonacci{n=1}
9187
│ │ │ │ ├─ms DEBUG Base case
92-
│ │ │ ├┐nth_fibonacci{n=2}
93-
│ │ │ │└┐nth_fibonacci{n=0}
88+
│ │ │ ├─┘
89+
│ │ │ ├─┐nth_fibonacci{n=0}
9490
│ │ │ │ ├─ms DEBUG Base case
95-
│ ├┐nth_fibonacci{n=5}
96-
│ │└┐nth_fibonacci{n=3}
91+
│ │ │ ├─┘
92+
│ │ ├─┘
93+
│ ├─┘
94+
│ ├─┐nth_fibonacci{n=3}
9795
│ │ ├─ms DEBUG Recursing
98-
│ │ ├┐nth_fibonacci{n=3}
99-
│ │ │└┐nth_fibonacci{n=2}
96+
│ │ ├─┐nth_fibonacci{n=2}
10097
│ │ │ ├─ms DEBUG Recursing
101-
│ │ │ ├┐nth_fibonacci{n=2}
102-
│ │ │ │└┐nth_fibonacci{n=1}
98+
│ │ │ ├─┐nth_fibonacci{n=1}
10399
│ │ │ │ ├─ms DEBUG Base case
104-
│ │ │ ├┐nth_fibonacci{n=2}
105-
│ │ │ │└┐nth_fibonacci{n=0}
100+
│ │ │ ├─┘
101+
│ │ │ ├─┐nth_fibonacci{n=0}
106102
│ │ │ │ ├─ms DEBUG Base case
107-
│ │ ├┐nth_fibonacci{n=3}
108-
│ │ │└┐nth_fibonacci{n=1}
103+
│ │ │ ├─┘
104+
│ │ ├─┘
105+
│ │ ├─┐nth_fibonacci{n=1}
109106
│ │ │ ├─ms DEBUG Base case
107+
│ │ ├─┘
108+
│ ├─┘
109+
├─┘
110+
110111
INFO The first 5 fibonacci numbers are [1, 1, 2, 3, 5, 8]

0 commit comments

Comments
 (0)