#![allow(dead_code)]
use std::{fmt, io::Error};
use flogging::*;
#[derive(Debug, Default)]
pub(crate) struct CustomHandler {
name: String,
formatter: Formatter,
log: Vec<String>,
}
impl CustomHandler {
fn new(name: &str) -> Self {
CustomHandler {
name: name.to_string(),
formatter: FormatType::Simple.create(None),
log: Vec::new(),
}
}
fn log(&self) -> String {
let mut buf = String::new();
for s in &self.log {
buf.push_str(s);
buf.push('\n');
}
buf
}
}
impl fmt::Display for CustomHandler {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let len = self.name.len() + self.formatter.to_string().len();
let line = "-".repeat(len + 3);
write!(
f,
"{} : {}\n{line}\n{}",
self.name,
self.formatter,
self.log()
)
}
}
impl HandlerTrait for CustomHandler {
fn create(name: &str) -> Result<Self, Error>
where
Self: Sized,
{
Ok(Self::new(name))
}
fn close(&mut self) {}
fn flush(&mut self) {
self.log.clear();
}
fn get_formatter(&self) -> Formatter {
self.formatter.clone()
}
fn get_log(&self) -> String {
self.log()
}
fn is_open(&self) -> bool {
true
}
fn publish(&mut self, log_entry: &LogEntry) {
println!("{}", self.formatter.format(log_entry));
self.log.push(self.formatter.format(log_entry));
}
fn set_formatter(&mut self, format: Formatter) {
self.formatter = format;
}
fn set_test_mode(&mut self, _state: bool) {}
}