Skip to content

Commit

Permalink
Implement DedupSink
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Dec 19, 2023
1 parent 0d182e2 commit 58f98e3
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 10 deletions.
52 changes: 52 additions & 0 deletions spdlog/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ pub enum Error {
#[cfg(feature = "multi-thread")]
#[error("failed to send message to channel: {0}")]
SendToChannel(SendToChannelError, SendToChannelErrorDropped),

/// This variant returned when multiple errors occurred.
#[error("{0:?}")]
Multiple(Vec<Error>),

#[cfg(test)]
#[error("{0}")]
__ForInternalTestsUseOnly(i32),
}

/// This error type contains a variety of possible invalid arguments.
Expand Down Expand Up @@ -180,6 +188,26 @@ pub enum SendToChannelErrorDropped {
Flush,
}

impl Error {
pub(crate) fn push_err<T>(result: Result<T>, new: Self) -> Result<T> {
match result {
Ok(_) => Err(new),
Err(Self::Multiple(mut errors)) => {
errors.push(new);
Err(Self::Multiple(errors))
}
Err(prev) => Err(Error::Multiple(vec![prev, new])),
}
}

pub(crate) fn push_result<T, N>(result: Result<T>, new: Result<N>) -> Result<T> {
match new {
Ok(_) => result,
Err(err) => Self::push_err(result, err),
}
}
}

#[cfg(feature = "multi-thread")]
impl Error {
#[must_use]
Expand Down Expand Up @@ -222,3 +250,27 @@ pub type ErrorHandler = fn(Error);

const_assert!(Atomic::<ErrorHandler>::is_lock_free());
const_assert!(Atomic::<Option<ErrorHandler>>::is_lock_free());

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn push_err() {
macro_rules! make_err {
( $($inputs:tt)+ ) => {
Error::__ForInternalTestsUseOnly($($inputs)*)
};
}

assert!(matches!(
Error::push_err(Ok(()), make_err!(1)),
Err(make_err!(1))
));

assert!(matches!(
Error::push_err::<()>(Err(make_err!(1)), make_err!(2)),
Err(Error::Multiple(v)) if matches!(v[..], [make_err!(1), make_err!(2)])
));
}
}
9 changes: 9 additions & 0 deletions spdlog/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ impl<'a> Record<'a> {

// When adding more getters, also add to `RecordOwned`

#[must_use]
pub(crate) fn replace_payload(&'a self, new: impl Into<Cow<'a, str>>) -> Self {
Self {
logger_name: self.logger_name,
payload: new.into(),
inner: Cow::Borrowed(&self.inner),
}
}

#[cfg(feature = "log")]
#[must_use]
pub(crate) fn from_log_crate_record(
Expand Down
Loading

0 comments on commit 58f98e3

Please sign in to comment.