forked from getsentry/sentry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread-demo.rs
57 lines (48 loc) · 1.75 KB
/
thread-demo.rs
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
use std::sync::Arc;
use std::thread;
fn main() {
init_log();
// this initializes sentry. It also gives the thread that calls this some
// special behavior in that all other threads spawned will get a hub based on
// the hub from here.
let _sentry = sentry::init(sentry::ClientOptions {
release: sentry::release_name!(),
debug: true,
..Default::default()
});
// the log integration sends to Hub::current()
log::info!("Spawning thread");
thread::spawn(|| {
// The thread spawned here gets a new hub cloned from the hub of the
// main thread.
log::info!("Spawned thread, configuring scope.");
// now we want to create a new hub based on the thread's normal hub for
// working with it explicitly.
let hub = Arc::new(sentry::Hub::new_from_top(sentry::Hub::current()));
// reconfigure that scope.
hub.configure_scope(|scope| {
scope.set_tag("worker", "worker1");
});
// we can now bind a new thread and have the other thread run some code
// bound to the hub we just created.
thread::spawn(move || {
sentry::Hub::run(hub, || {
// the log integration picks up the Hub::current which is now bound
// to the outer hub.
log::error!("Failing!");
});
})
.join()
.unwrap();
})
.join()
.unwrap();
}
fn init_log() {
let mut log_builder = pretty_env_logger::formatted_builder();
log_builder.parse_filters("info");
let logger = sentry_log::SentryLogger::with_dest(log_builder.build());
log::set_boxed_logger(Box::new(logger))
.map(|()| log::set_max_level(log::LevelFilter::Info))
.unwrap();
}