Skip to main content

tuwunel_core/log/capture/
mod.rs

1pub mod data;
2mod guard;
3pub mod layer;
4pub mod state;
5pub mod util;
6
7use std::sync::{Arc, Mutex};
8
9pub use data::Data;
10use guard::Guard;
11pub use layer::{Layer, Value};
12pub use state::State;
13pub use util::*;
14
15pub type Filter = dyn Fn(Data<'_>) -> bool + Send + Sync + 'static;
16pub type Closure = dyn FnMut(Data<'_>) + Send + Sync + 'static;
17
18/// Capture instance state.
19pub struct Capture {
20	state: Arc<State>,
21	filter: Option<Box<Filter>>,
22	closure: Mutex<Box<Closure>>,
23}
24
25impl Capture {
26	/// Construct a new capture instance. Capture does not start until the Guard
27	/// is in scope.
28	#[must_use]
29	pub fn new<F, C>(state: &Arc<State>, filter: Option<F>, closure: C) -> Arc<Self>
30	where
31		F: Fn(Data<'_>) -> bool + Send + Sync + 'static,
32		C: FnMut(Data<'_>) + Send + Sync + 'static,
33	{
34		Arc::new(Self {
35			state: state.clone(),
36			filter: filter.map(|p| -> Box<Filter> { Box::new(p) }),
37			closure: Mutex::new(Box::new(closure)),
38		})
39	}
40
41	#[must_use]
42	pub fn start(self: &Arc<Self>) -> Guard {
43		self.state.add(self);
44		Guard { capture: self.clone() }
45	}
46
47	pub fn stop(self: &Arc<Self>) { self.state.del(self); }
48}