1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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_lines(true)
.with_indent_amount(2)
.with_thread_names(true)
.with_thread_ids(true)
.with_verbose_exit(true)
.with_verbose_entry(true)
.with_targets(true);
let subscriber = Registry::default().with(layer);
tracing::subscriber::set_global_default(subscriber).unwrap();
#[cfg(feature = "tracing-log")]
tracing_log::LogTracer::init().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(3000));
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");
log::error!("this is a log message");
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");
}
|