use std::fmt::Arguments;
use std::fmt::Debug;
use std::fmt::Formatter;
use log::Record;
use crate::layout::Layout;
type FormatFunction = dyn Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static;
pub struct CustomLayout {
f: Box<FormatFunction>,
}
impl Debug for CustomLayout {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "CustomLayout {{ ... }}")
}
}
impl CustomLayout {
pub fn new(
layout: impl Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static,
) -> Self {
CustomLayout {
f: Box::new(layout),
}
}
pub(crate) fn format<F>(&self, record: &Record, f: &F) -> anyhow::Result<()>
where
F: Fn(Arguments) -> anyhow::Result<()>,
{
(self.f)(record, f)
}
}
impl From<CustomLayout> for Layout {
fn from(layout: CustomLayout) -> Self {
Layout::Custom(layout)
}
}