use super::format::{DefaultMsgFormat, MsgFormat};
use super::{Facility, SyslogDrain};
use crate::build::BuilderCommon;
#[cfg(feature = "slog-kvfilter")]
use crate::types::KVFilterParameters;
use crate::types::{OverflowStrategy, Severity, SourceLocation};
use crate::Build;
use crate::Result;
use slog::Logger;
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::fmt::Debug;
use std::sync::Arc;
#[derive(Debug)]
pub struct SyslogBuilder {
pub(super) common: BuilderCommon,
pub(super) facility: Facility,
pub(super) ident: Option<Cow<'static, CStr>>,
pub(super) option: libc::c_int,
pub(super) format: Arc<dyn MsgFormat>,
}
impl Default for SyslogBuilder {
fn default() -> Self {
SyslogBuilder {
common: BuilderCommon::default(),
facility: Facility::default(),
ident: None,
option: 0,
format: Arc::new(DefaultMsgFormat),
}
}
}
impl SyslogBuilder {
pub fn new() -> Self {
SyslogBuilder::default()
}
pub fn source_location(&mut self, source_location: SourceLocation) -> &mut Self {
self.common.source_location = source_location;
self
}
pub fn facility(&mut self, facility: Facility) -> &mut Self {
self.facility = facility;
self
}
pub fn overflow_strategy(&mut self, overflow_strategy: OverflowStrategy) -> &mut Self {
self.common.overflow_strategy = overflow_strategy;
self
}
pub fn ident_str(&mut self, ident: impl AsRef<str>) -> &mut Self {
let cs = CString::new(ident.as_ref()).expect(
"`sloggers::syslog::SyslogBuilder::ident` called with string that contains null bytes",
);
self.ident(cs)
}
pub fn ident(&mut self, ident: impl Into<Cow<'static, CStr>>) -> &mut Self {
self.ident = Some(ident.into());
self
}
#[inline]
pub fn log_pid(&mut self) -> &mut Self {
self.option |= libc::LOG_PID;
self
}
#[inline]
pub fn log_ndelay(&mut self) -> &mut Self {
self.option = (self.option & !libc::LOG_ODELAY) | libc::LOG_NDELAY;
self
}
#[inline]
pub fn log_odelay(&mut self) -> &mut Self {
self.option = (self.option & !libc::LOG_NDELAY) | libc::LOG_ODELAY;
self
}
#[inline]
pub fn log_nowait(&mut self) -> &mut Self {
self.option |= libc::LOG_NOWAIT;
self
}
#[inline]
pub fn log_perror(&mut self) -> &mut Self {
self.option |= libc::LOG_PERROR;
self
}
pub fn format(&mut self, format: impl MsgFormat + 'static) -> &mut Self {
self.format_arc(Arc::new(format))
}
pub fn format_arc(&mut self, format: Arc<dyn MsgFormat>) -> &mut Self {
self.format = format;
self
}
pub fn level(&mut self, severity: Severity) -> &mut Self {
self.common.level = severity;
self
}
pub fn channel_size(&mut self, channel_size: usize) -> &mut Self {
self.common.channel_size = channel_size;
self
}
#[cfg(feature = "slog-kvfilter")]
pub fn kvfilter(&mut self, parameters: KVFilterParameters) -> &mut Self {
self.common.kvfilterparameters = Some(parameters);
self
}
}
impl Build for SyslogBuilder {
fn build(&self) -> Result<Logger> {
let drain = SyslogDrain::new(self);
let logger = self.common.build_with_drain(drain);
Ok(logger)
}
}