From 2c39b6094ecda9a39cdbc969ddd3455b52d1550c Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Wed, 13 Jul 2022 21:13:14 +0200 Subject: [PATCH] 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 --- tracing-appender/src/worker.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tracing-appender/src/worker.rs b/tracing-appender/src/worker.rs index 5508baca85..622cb2c61a 100644 --- a/tracing-appender/src/worker.rs +++ b/tracing-appender/src/worker.rs @@ -68,22 +68,25 @@ impl Worker { /// Creates a worker thread that processes a channel until it's disconnected pub(crate) fn worker_thread(mut self) -> std::thread::JoinHandle<()> { - thread::spawn(move || { - loop { - match self.work() { - Ok(WorkerState::Continue) | Ok(WorkerState::Empty) => {} - Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => { - let _ = self.shutdown.recv(); - break; - } - Err(_) => { - // TODO: Expose a metric for IO Errors, or print to stderr + thread::Builder::new() + .name("tracing-appender".to_string()) + .spawn(move || { + loop { + match self.work() { + Ok(WorkerState::Continue) | Ok(WorkerState::Empty) => {} + Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => { + let _ = self.shutdown.recv(); + break; + } + Err(_) => { + // TODO: Expose a metric for IO Errors, or print to stderr + } } } - } - if let Err(e) = self.writer.flush() { - eprintln!("Failed to flush. Error: {}", e); - } - }) + if let Err(e) = self.writer.flush() { + eprintln!("Failed to flush. Error: {}", e); + } + }) + .expect("failed to spawn `tracing-appender` non-blocking worker thread") } }