add markdown log format for capture

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-12 18:52:24 +00:00
parent c914a4fd91
commit 1f9225e4d1
6 changed files with 95 additions and 17 deletions

View file

@ -10,7 +10,7 @@ pub use data::Data;
use guard::Guard;
pub use layer::{Layer, Value};
pub use state::State;
pub use util::to_html;
pub use util::*;
pub type Filter = dyn Fn(Data<'_>) -> bool + Send + Sync + 'static;
pub type Closure = dyn FnMut(Data<'_>) + Send + Sync + 'static;

View file

@ -1,19 +1,37 @@
use std::sync::{Arc, Mutex};
use super::{super::fmt, Closure};
use super::{
super::{fmt, Level},
Closure, Data,
};
use crate::Result;
pub fn to_html<S>(out: &Arc<Mutex<S>>) -> Box<Closure>
pub fn fmt_html<S>(out: Arc<Mutex<S>>) -> Box<Closure>
where
S: std::fmt::Write + Send + 'static,
{
let out = out.clone();
Box::new(move |data| {
fmt::html(
&mut *out.lock().expect("locked"),
&data.level(),
data.span_name(),
data.message(),
)
.expect("log line appended");
})
fmt(fmt::html, out)
}
pub fn fmt_markdown<S>(out: Arc<Mutex<S>>) -> Box<Closure>
where
S: std::fmt::Write + Send + 'static,
{
fmt(fmt::markdown, out)
}
pub fn fmt<F, S>(fun: F, out: Arc<Mutex<S>>) -> Box<Closure>
where
F: Fn(&mut S, &Level, &str, &str) -> Result<()> + Send + Sync + Copy + 'static,
S: std::fmt::Write + Send + 'static,
{
Box::new(move |data| call(fun, &mut *out.lock().expect("locked"), &data))
}
fn call<F, S>(fun: F, out: &mut S, data: &Data<'_>)
where
F: Fn(&mut S, &Level, &str, &str) -> Result<()>,
S: std::fmt::Write,
{
fun(out, &data.level(), data.span_name(), data.message()).expect("log line appended");
}

View file

@ -16,3 +16,32 @@ where
Ok(())
}
pub fn markdown<S>(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()>
where
S: Write,
{
let level = level.as_str().to_uppercase();
writeln!(out, "`{level:>5}` `{span:^12}` `{msg}`")?;
Ok(())
}
pub fn markdown_table<S>(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()>
where
S: Write,
{
let level = level.as_str().to_uppercase();
writeln!(out, "| `{level:>5}` | `{span:^12}` | `{msg} |")?;
Ok(())
}
pub fn markdown_table_head<S>(out: &mut S) -> Result<()>
where
S: Write,
{
write!(out, "| level | span | message |\n|------:|:----:|:--------|\n")?;
Ok(())
}