use crate::logger::LOGGER_STDOUT_NAME;
use log::LevelFilter;
use serde::Deserialize;
use std::borrow::Cow;
const DEFAULT_COLOR_ENABLED: bool = true;
const DEFAULT_TARGET_WIDTH: usize = 42;
const DEFAULT_LEVEL_WIDTH: usize = 5;
const DEFAULT_OUTPUT_NAME: &str = LOGGER_STDOUT_NAME;
const DEFAULT_OUTPUT_LEVEL: LevelFilter = LevelFilter::Info;
#[derive(Default, Deserialize)]
pub struct LoggerOutputConfigBuilder {
name: Option<String>,
level_filter: Option<LevelFilter>,
target_filters: Option<Vec<String>>,
target_exclusions: Option<Vec<String>>,
}
impl LoggerOutputConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn name<'a>(mut self, name: impl Into<Cow<'a, str>>) -> Self {
self.name.replace(name.into().into_owned());
self
}
pub fn level_filter(mut self, level: LevelFilter) -> Self {
self.level_filter.replace(level);
self
}
pub fn target_filters(mut self, target_filters: &[&str]) -> Self {
self.target_filters = Some(target_filters.iter().map(|f| f.to_string()).collect::<Vec<String>>());
self
}
pub fn finish(self) -> LoggerOutputConfig {
LoggerOutputConfig {
name: self.name.unwrap_or_else(|| DEFAULT_OUTPUT_NAME.to_owned()),
level_filter: self.level_filter.unwrap_or(DEFAULT_OUTPUT_LEVEL),
target_filters: self
.target_filters
.unwrap_or_else(Vec::new)
.iter()
.map(|f| f.to_lowercase())
.collect(),
target_exclusions: self
.target_exclusions
.unwrap_or_else(Vec::new)
.iter()
.map(|f| f.to_lowercase())
.collect(),
}
}
}
#[derive(Clone)]
pub struct LoggerOutputConfig {
pub(crate) name: String,
pub(crate) level_filter: LevelFilter,
pub(crate) target_filters: Vec<String>,
pub(crate) target_exclusions: Vec<String>,
}
#[derive(Default, Deserialize)]
pub struct LoggerConfigBuilder {
color_enabled: Option<bool>,
target_width: Option<usize>,
level_width: Option<usize>,
outputs: Option<Vec<LoggerOutputConfigBuilder>>,
}
impl LoggerConfigBuilder {
pub fn color_enabled(mut self, color: bool) -> Self {
self.color_enabled.replace(color);
self
}
pub fn with_target_width(mut self, width: usize) -> Self {
self.target_width.replace(width);
self
}
pub fn with_level_width(mut self, width: usize) -> Self {
self.level_width.replace(width);
self
}
pub fn with_output(mut self, output: LoggerOutputConfigBuilder) -> Self {
self.outputs.get_or_insert_with(Vec::new).push(output);
self
}
pub fn level<'a>(&mut self, name: impl Into<Cow<'a, str>>, level: LevelFilter) {
let name = name.into();
if let Some(outputs) = self.outputs.as_deref_mut() {
if let Some(stdout) = outputs.iter_mut().find(|output| match output.name.as_ref() {
Some(output_name) => output_name[..] == name,
None => false,
}) {
stdout.level_filter.replace(level);
}
}
}
pub fn finish(self) -> LoggerConfig {
let outputs = self
.outputs
.map(|os| os.into_iter().map(|o| o.finish()).collect())
.unwrap_or_default();
LoggerConfig {
color_enabled: self.color_enabled.unwrap_or(DEFAULT_COLOR_ENABLED),
target_width: self.target_width.unwrap_or(DEFAULT_TARGET_WIDTH),
level_width: self.level_width.unwrap_or(DEFAULT_LEVEL_WIDTH),
outputs,
}
}
}
#[derive(Clone)]
pub struct LoggerConfig {
pub(crate) color_enabled: bool,
pub(crate) target_width: usize,
pub(crate) level_width: usize,
pub(crate) outputs: Vec<LoggerOutputConfig>,
}
impl LoggerConfig {
pub fn build() -> LoggerConfigBuilder {
LoggerConfigBuilder::default()
}
pub fn color_enabled(&self) -> bool {
self.color_enabled
}
pub fn target_width(&self) -> usize {
self.target_width
}
pub fn level_width(&self) -> usize {
self.level_width
}
pub fn outputs(&self) -> &[LoggerOutputConfig] {
&self.outputs
}
}