Skip to content

Commit 8e273a2

Browse files
committed
chore: add tests for #66
1 parent 5eec932 commit 8e273a2

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

examples/basic_verbose_entry.rs

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

examples/basic_verbose_entry.stdout

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

examples/basic_verbose_exit.rs

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

examples/basic_verbose_exit.stdout

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

0 commit comments

Comments
 (0)