Skip to main content

tuwunel_core/log/capture/
util.rs

1use std::sync::{Arc, Mutex};
2
3use super::{
4	super::{Level, fmt},
5	Closure, Data,
6};
7use crate::Result;
8
9pub fn fmt_html<S>(out: Arc<Mutex<S>>) -> Box<Closure>
10where
11	S: std::fmt::Write + Send + 'static,
12{
13	fmt(fmt::html, out)
14}
15
16pub fn fmt_markdown<S>(out: Arc<Mutex<S>>) -> Box<Closure>
17where
18	S: std::fmt::Write + Send + 'static,
19{
20	fmt(fmt::markdown, out)
21}
22
23pub fn fmt<F, S>(fun: F, out: Arc<Mutex<S>>) -> Box<Closure>
24where
25	F: Fn(&mut S, &Level, &str, &str) -> Result + Send + Sync + Copy + 'static,
26	S: std::fmt::Write + Send + 'static,
27{
28	Box::new(move |data| call(fun, &mut *out.lock().expect("locked"), &data))
29}
30
31fn call<F, S>(fun: F, out: &mut S, data: &Data<'_>)
32where
33	F: Fn(&mut S, &Level, &str, &str) -> Result,
34	S: std::fmt::Write,
35{
36	fun(out, &data.level(), data.span_name(), data.message()).expect("log line appended");
37}