Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement config feature #47

Draft
wants to merge 4 commits into
base: main-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spdlog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ release-level-info = []
release-level-debug = []
release-level-trace = []

config = ["serde", "erased-serde", "toml"]
source-location = []
native = []
libsystemd = ["libsystemd-sys"]
Expand All @@ -45,15 +46,18 @@ cfg-if = "1.0.0"
chrono = "0.4.22"
crossbeam = { version = "0.8.2", optional = true }
dyn-clone = "1.0.14"
erased-serde = { version = "0.3.31", optional = true }
flexible-string = { version = "0.1.0", optional = true }
if_chain = "1.0.2"
is-terminal = "0.4"
log = { version = "0.4.8", optional = true }
once_cell = "1.16.0"
serde = { version = "1.0.163", optional = true }
spdlog-internal = { version = "=0.1.0", path = "../spdlog-internal", optional = true }
spdlog-macros = { version = "0.1.0", path = "../spdlog-macros" }
spin = "0.9.8"
thiserror = "1.0.37"
toml = { version = "0.8.8", optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["consoleapi", "debugapi", "handleapi", "processenv", "processthreadsapi", "winbase", "wincon"] }
Expand Down Expand Up @@ -81,6 +85,7 @@ tracing-subscriber = "=0.3.16"
tracing-appender = "=0.2.2"
paste = "1.0.14"
trybuild = "1.0.90"
toml = "0.8.6"

[build-dependencies]
rustc_version = "0.4.0"
Expand Down
145 changes: 145 additions & 0 deletions spdlog/src/config/deser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use std::{marker::PhantomData, result::Result as StdResult};

use erased_serde::Deserializer as ErasedDeserializer;
use serde::{
de::{
value::{MapAccessDeserializer, UnitDeserializer},
Error as SerdeDeError, MapAccess, Visitor,
},
Deserialize, Deserializer,
};

use crate::{config, formatter::*, sync::*, Logger, LoggerBuilder, LoggerParams, Result, Sink};

trait Component {
type Value;

fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result;
fn build(name: &str, de: &mut dyn ErasedDeserializer) -> Result<Self::Value>;
}

struct ComponentFormatter;

impl Component for ComponentFormatter {
type Value = Box<dyn Formatter>;

fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a spdlog-rs formatter")
}

fn build(name: &str, de: &mut dyn ErasedDeserializer) -> Result<Self::Value> {
config::registry().build_formatter(&name, de)
}
}

struct ComponentSink;

impl Component for ComponentSink {
type Value = Arc<dyn Sink>;

fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a spdlog-rs sink")
}

fn build(name: &str, de: &mut dyn ErasedDeserializer) -> Result<Self::Value> {
config::registry().build_sink(&name, de)
}
}

// Unit for 0 parameter components, map for components with parameters
struct UnitOrMapDeserializer<A> {
map: A,
}

impl<'de, A> Deserializer<'de> for UnitOrMapDeserializer<A>
where
A: MapAccess<'de>,
{
type Error = A::Error;

fn deserialize_any<V>(self, visitor: V) -> StdResult<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_map(self.map)
}

fn deserialize_unit<V>(self, visitor: V) -> StdResult<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}

fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> StdResult<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}

serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option enum unit_struct seq tuple
tuple_struct map struct identifier ignored_any
}
}

struct ComponentVisitor<C>(PhantomData<C>);

impl<'de, C> Visitor<'de> for ComponentVisitor<C>
where
C: Component,
{
type Value = C::Value;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
C::expecting(formatter)
}

fn visit_map<A>(self, mut map: A) -> StdResult<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let name = map
.next_entry::<String, String>()?
.filter(|(key, _)| key == "name")
.map(|(_, value)| value)
.ok_or_else(|| SerdeDeError::missing_field("name"))?;

let mut erased_de = <dyn ErasedDeserializer>::erase(UnitOrMapDeserializer { map });
let component = C::build(&name, &mut erased_de).map_err(SerdeDeError::custom)?;

Ok(component)
}
}

pub fn formatter<'de, D>(de: D) -> StdResult<Option<Box<dyn Formatter>>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Some(de.deserialize_map(ComponentVisitor::<
ComponentFormatter,
>(PhantomData))?))
}

pub fn sink<'de, D>(de: D) -> StdResult<Option<Arc<dyn Sink>>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Some(de.deserialize_map(
ComponentVisitor::<ComponentSink>(PhantomData),
)?))
}

pub fn logger<'de, D>(de: D) -> StdResult<Logger, D::Error>
where
D: Deserializer<'de>,
{
let params = LoggerParams::deserialize(de)?;
LoggerBuilder::build_config(params).map_err(SerdeDeError::custom)
}
Loading
Loading