use crate::{Error, ErrorKind};
#[cfg(feature = "slog-kvfilter")]
use regex::Regex;
use serde::{Deserialize, Serialize};
use slog::{Drain, Level, LevelFilter};
#[cfg(feature = "slog-kvfilter")]
use slog_kvfilter::KVFilterList;
use std::str::FromStr;
#[allow(missing_docs)]
#[derive(
Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
Trace,
Debug,
#[default]
Info,
Warning,
Error,
Critical,
}
impl Severity {
pub fn as_level(self) -> Level {
match self {
Severity::Trace => Level::Trace,
Severity::Debug => Level::Debug,
Severity::Info => Level::Info,
Severity::Warning => Level::Warning,
Severity::Error => Level::Error,
Severity::Critical => Level::Critical,
}
}
pub fn set_level_filter<D: Drain>(self, drain: D) -> LevelFilter<D> {
LevelFilter::new(drain, self.as_level())
}
}
impl FromStr for Severity {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"trace" => Ok(Severity::Trace),
"debug" => Ok(Severity::Debug),
"info" => Ok(Severity::Info),
"warning" => Ok(Severity::Warning),
"error" => Ok(Severity::Error),
"critical" => Ok(Severity::Critical),
_ => track_panic!(ErrorKind::Invalid, "Undefined severity: {:?}", s),
}
}
}
#[derive(Debug, Clone)]
#[allow(missing_docs, clippy::upper_case_acronyms)]
#[cfg(feature = "slog-kvfilter")]
#[non_exhaustive]
pub struct KVFilterParameters {
pub severity: Severity,
pub only_pass_any_on_all_keys: Option<KVFilterList>,
pub always_suppress_any: Option<KVFilterList>,
pub only_pass_on_regex: Option<Regex>,
pub always_suppress_on_regex: Option<Regex>,
}
#[cfg(feature = "slog-kvfilter")]
impl Default for KVFilterParameters {
fn default() -> Self {
KVFilterParameters {
severity: Severity::Info,
only_pass_any_on_all_keys: None,
always_suppress_any: None,
only_pass_on_regex: None,
always_suppress_on_regex: None,
}
}
}
#[cfg(feature = "slog-kvfilter")]
impl KVFilterParameters {
#[inline]
pub fn new() -> Self {
Default::default()
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Format {
#[default]
Full,
Compact,
#[cfg(feature = "json")]
Json,
}
impl FromStr for Format {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"full" => Ok(Format::Full),
"compact" => Ok(Format::Compact),
#[cfg(feature = "json")]
"json" => Ok(Format::Json),
_ => track_panic!(ErrorKind::Invalid, "Undefined log format: {:?}", s),
}
}
}
#[allow(missing_docs)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TimeZone {
Utc,
#[default]
Local,
}
impl FromStr for TimeZone {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"utc" => Ok(TimeZone::Utc),
"local" => Ok(TimeZone::Local),
_ => track_panic!(ErrorKind::Invalid, "Undefined time zone: {:?}", s),
}
}
}
#[allow(missing_docs)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SourceLocation {
None,
#[default]
ModuleAndLine,
FileAndLine,
LocalFileAndLine,
}
impl FromStr for SourceLocation {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"none" => Ok(SourceLocation::None),
"module_and_line" => Ok(SourceLocation::ModuleAndLine),
"file_and_line" => Ok(SourceLocation::FileAndLine),
"local_file_and_line" => Ok(SourceLocation::LocalFileAndLine),
_ => track_panic!(
ErrorKind::Invalid,
"Undefined source code location: {:?}",
s
),
}
}
}
#[allow(missing_docs)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum OverflowStrategy {
#[default]
DropAndReport,
Drop,
Block,
}
impl FromStr for OverflowStrategy {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"drop" => Ok(OverflowStrategy::Drop),
"drop_and_report" => Ok(OverflowStrategy::DropAndReport),
"block" => Ok(OverflowStrategy::Block),
_ => track_panic!(ErrorKind::Invalid, "Invalid overflow strategy: {:?}", s),
}
}
}
impl OverflowStrategy {
pub fn to_async_type(self) -> slog_async::OverflowStrategy {
match self {
OverflowStrategy::Drop => slog_async::OverflowStrategy::Drop,
OverflowStrategy::DropAndReport => slog_async::OverflowStrategy::DropAndReport,
OverflowStrategy::Block => slog_async::OverflowStrategy::Block,
}
}
}