Skip to main content

tuwunel_core/log/
suppress.rs

1use std::sync::Arc;
2
3use super::EnvFilter;
4use crate::Server;
5
6/// Temporarily suppresses the console log subscriber layer.
7///
8/// Construction replaces the console filter with an empty filter and retains a
9/// restoration filter. Dropping the guard restores that value.
10pub struct Suppress {
11	server: Arc<Server>,
12	restore: EnvFilter,
13}
14
15impl Suppress {
16	/// Suppresses console logging until the returned guard is dropped.
17	///
18	/// The current console filter is saved when available; otherwise one is
19	/// rebuilt from the configured directives. The stored filter and cloned
20	/// server handle let `Drop` reach the reload map and restore logging.
21	///
22	/// # Panics
23	///
24	/// Panics if the shared reload-handle map mutex is poisoned.
25	pub fn new(server: &Arc<Server>) -> Self {
26		let handle = "console";
27		let config = &server.config.log;
28		let suppress = EnvFilter::default();
29		let restore = server
30			.log
31			.reload
32			.current(handle)
33			.unwrap_or_else(|| EnvFilter::try_new(config).unwrap_or_default());
34
35		server
36			.log
37			.reload
38			.reload(&suppress, Some(&[handle]))
39			.expect("log filter reloaded");
40
41		Self { server: server.clone(), restore }
42	}
43}
44
45impl Drop for Suppress {
46	fn drop(&mut self) {
47		self.server
48			.log
49			.reload
50			.reload(&self.restore, Some(&["console"]))
51			.expect("log filter reloaded");
52	}
53}