use tracing_subscriber::Registry;
use super::*;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum LogOutputKind {
StdOut,
StdErr,
File,
Api,
Layer(String),
}
#[must_use]
pub struct LogOutput {
pub(super) kind: LogOutputKind,
pub(super) color: bool,
pub(super) path: PathBuf,
pub(super) append: bool,
pub(super) directives: Vec<VeilidLogDirective>,
pub(super) layer: Option<LogOutputLayer>,
}
pub type LogOutputLayer =
Box<dyn tracing_subscriber::layer::Layer<Registry> + Send + Sync + 'static>;
impl LogOutput {
pub fn stdout(color: bool) -> Self {
Self {
kind: LogOutputKind::StdOut,
color,
path: PathBuf::new(),
append: false,
directives: vec![],
layer: None,
}
}
pub fn stderr(color: bool) -> Self {
Self {
kind: LogOutputKind::StdErr,
color,
path: PathBuf::new(),
append: false,
directives: vec![],
layer: None,
}
}
pub fn file<P: AsRef<Path>>(path: P, append: bool) -> Self {
Self {
kind: LogOutputKind::File,
color: false,
path: path.as_ref().to_owned(),
append,
directives: vec![],
layer: None,
}
}
pub fn api() -> Self {
Self {
kind: LogOutputKind::Api,
color: false,
path: PathBuf::new(),
append: false,
directives: vec![],
layer: None,
}
}
pub fn layer<L>(name: String, layer: LogOutputLayer) -> Self {
Self {
kind: LogOutputKind::Layer(name),
color: false,
path: PathBuf::new(),
append: false,
directives: vec![],
layer: Some(layer),
}
}
pub fn with_common_log_level(mut self, level: VeilidConfigLogLevel) -> Self {
self.directives
.push(VeilidLogDirective::try_facility_level("veilid::common", Some(level)).unwrap());
self
}
pub fn try_with_directives<C: TryIntoIterVeilidLogDirective>(
mut self,
directives: C,
) -> VeilidAPIResult<Self> {
let mut directives = directives.try_into_iter()?.collect();
self.directives.append(&mut directives);
Ok(self)
}
}