File: concurrent_verbose.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (110 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download | duplicates (8)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use futures::FutureExt;
use tracing::{debug, debug_span, info, span, warn, Instrument, 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(4)
        .with_thread_names(true)
        .with_thread_ids(true)
        .with_verbose_exit(true)
        .with_verbose_entry(true)
        .with_span_retrace(true)
        .with_deferred_spans(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(1000));

    info!("listening");

    let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);

    span!(Level::INFO, "empty-span").in_scope(|| {
        // empty span
    });

    debug!("starting countdowns");
    debug_span!("countdowns").in_scope(|| {
        let mut countdown_a = CountdownFuture {
            label: "a",
            count: 3,
        }
        .instrument(span!(Level::DEBUG, "countdown_a"))
        .fuse();

        let mut countdown_b = CountdownFuture {
            label: "b",
            count: 5,
        }
        .instrument(span!(Level::DEBUG, "countdown_b"))
        .fuse();

        // We don't care if the futures are ready, as we poll manually
        let waker = futures::task::noop_waker();
        let mut cx = Context::from_waker(&waker);

        let _ = countdown_a.poll_unpin(&mut cx);
        let _ = countdown_b.poll_unpin(&mut cx);

        std::thread::sleep(std::time::Duration::from_millis(300));

        let _ = countdown_b.poll_unpin(&mut cx);
        let _ = countdown_a.poll_unpin(&mut cx);

        peer1.in_scope(|| {
            warn!("peer1 warning");
        });

        tracing::info!("finished polling countdowns");
    });

    drop(peer1);

    tracing::info!("all done!");

    info!("exit")
}

struct CountdownFuture {
    label: &'static str,
    count: u32,
}

impl Future for CountdownFuture {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        debug!(label=?self.label, count=?self.count, "polling countdown");
        self.count -= 1;

        if self.count == 0 {
            Poll::Ready(())
        } else {
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }
}