Skip to content

Commit 9909b5b

Browse files
gdesmotthawkw
authored andcommitted
appender: name spawned thread (#2219)
## Motivation I find it useful when debugging applications with lots of threads to easily identity them by their names. ## Solution Just name the thread as other crates such as `sentry-rust` are doing. Co-authored-by: Guillaume Desmottes <guillaume@desmottes.be>
1 parent 5f48248 commit 9909b5b

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

tracing-appender/src/worker.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,25 @@ impl<T: Write + Send + Sync + 'static> Worker<T> {
6868

6969
/// Creates a worker thread that processes a channel until it's disconnected
7070
pub(crate) fn worker_thread(mut self) -> std::thread::JoinHandle<()> {
71-
thread::spawn(move || {
72-
loop {
73-
match self.work() {
74-
Ok(WorkerState::Continue) | Ok(WorkerState::Empty) => {}
75-
Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => {
76-
let _ = self.shutdown.recv();
77-
break;
78-
}
79-
Err(_) => {
80-
// TODO: Expose a metric for IO Errors, or print to stderr
71+
thread::Builder::new()
72+
.name("tracing-appender".to_string())
73+
.spawn(move || {
74+
loop {
75+
match self.work() {
76+
Ok(WorkerState::Continue) | Ok(WorkerState::Empty) => {}
77+
Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => {
78+
let _ = self.shutdown.recv();
79+
break;
80+
}
81+
Err(_) => {
82+
// TODO: Expose a metric for IO Errors, or print to stderr
83+
}
8184
}
8285
}
83-
}
84-
if let Err(e) = self.writer.flush() {
85-
eprintln!("Failed to flush. Error: {}", e);
86-
}
87-
})
86+
if let Err(e) = self.writer.flush() {
87+
eprintln!("Failed to flush. Error: {}", e);
88+
}
89+
})
90+
.expect("failed to spawn `tracing-appender` non-blocking worker thread")
8891
}
8992
}

0 commit comments

Comments
 (0)