Skip to content

Commit 77848bb

Browse files
committed
Implement DedupSink
1 parent 0d182e2 commit 77848bb

File tree

5 files changed

+396
-10
lines changed

5 files changed

+396
-10
lines changed

spdlog/src/error.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ pub enum Error {
9191
#[cfg(feature = "multi-thread")]
9292
#[error("failed to send message to channel: {0}")]
9393
SendToChannel(SendToChannelError, SendToChannelErrorDropped),
94+
95+
/// This variant returned when multiple errors occurred.
96+
#[error("{0:?}")]
97+
Multiple(Vec<Error>),
98+
99+
#[cfg(test)]
100+
#[error("{0}")]
101+
__ForInternalTestsUseOnly(i32),
94102
}
95103

96104
/// This error type contains a variety of possible invalid arguments.
@@ -180,6 +188,26 @@ pub enum SendToChannelErrorDropped {
180188
Flush,
181189
}
182190

191+
impl Error {
192+
pub(crate) fn push_err<T>(result: Result<T>, new: Self) -> Result<T> {
193+
match result {
194+
Ok(_) => Err(new),
195+
Err(Self::Multiple(mut errors)) => {
196+
errors.push(new);
197+
Err(Self::Multiple(errors))
198+
}
199+
Err(prev) => Err(Error::Multiple(vec![prev, new])),
200+
}
201+
}
202+
203+
pub(crate) fn push_result<T, N>(result: Result<T>, new: Result<N>) -> Result<T> {
204+
match new {
205+
Ok(_) => result,
206+
Err(err) => Self::push_err(result, err),
207+
}
208+
}
209+
}
210+
183211
#[cfg(feature = "multi-thread")]
184212
impl Error {
185213
#[must_use]
@@ -222,3 +250,27 @@ pub type ErrorHandler = fn(Error);
222250

223251
const_assert!(Atomic::<ErrorHandler>::is_lock_free());
224252
const_assert!(Atomic::<Option<ErrorHandler>>::is_lock_free());
253+
254+
#[cfg(test)]
255+
mod tests {
256+
use super::*;
257+
258+
#[test]
259+
fn push_err() {
260+
macro_rules! make_err {
261+
( $($inputs:tt)+ ) => {
262+
Error::__ForInternalTestsUseOnly($($inputs)*)
263+
};
264+
}
265+
266+
assert!(matches!(
267+
Error::push_err(Ok(()), make_err!(1)),
268+
Err(make_err!(1))
269+
));
270+
271+
assert!(matches!(
272+
Error::push_err::<()>(Err(make_err!(1)), make_err!(2)),
273+
Err(Error::Multiple(v)) if matches!(v[..], [make_err!(1), make_err!(2)])
274+
));
275+
}
276+
}

spdlog/src/record.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ impl<'a> Record<'a> {
118118

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

121+
#[must_use]
122+
pub(crate) fn replace_payload(&'a self, new: impl Into<Cow<'a, str>>) -> Self {
123+
Self {
124+
logger_name: self.logger_name,
125+
payload: new.into(),
126+
inner: Cow::Borrowed(&self.inner),
127+
}
128+
}
129+
121130
#[cfg(feature = "log")]
122131
#[must_use]
123132
pub(crate) fn from_log_crate_record(

0 commit comments

Comments
 (0)