spdlog-rs 0.5.3

Fast, highly configurable Rust logging crate, inspired by the C++ logging library spdlog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Provides error types.
//!
//! # Default error handler
//!
//! If a logger or sink does not have an error handler set up, a default error
//! handler will be used, which will print the error to `stderr`.

use std::{
    error::Error as StdError,
    fmt::{self, Display},
    io::{self, Write as _},
    result,
    sync::Arc,
};

pub use crate::env_level::EnvLevelError;
#[cfg(feature = "multi-thread")]
use crate::{sink::Task, RecordOwned};

/// Contains most errors of this crate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Returned by [`Formatter`]s when an error occurs in formatting a record.
    ///
    /// [`Formatter`]: crate::formatter::Formatter
    FormatRecord(fmt::Error),

    /// Returned by [`Sink`]s when an error occurs in writing a record to the
    /// target.
    ///
    /// [`Sink`]: crate::sink::Sink
    WriteRecord(io::Error),

    /// Returned by [`Sink`]s when an error occurs in flushing the buffer.
    ///
    /// [`Sink`]: crate::sink::Sink
    FlushBuffer(io::Error),

    /// Returned by [`Sink`]s when an error occurs in creating a directory.
    ///
    /// [`Sink`]: crate::sink::Sink
    CreateDirectory(io::Error),

    /// Returned by [`Sink`]s when an error occurs in opening a file.
    ///
    /// [`Sink`]: crate::sink::Sink
    OpenFile(io::Error),

    /// Returned by [`Sink`]s when an error occurs in querying the metadata of a
    /// file.
    ///
    /// [`Sink`]: crate::sink::Sink
    QueryFileMetadata(io::Error),

    /// Returned by [`Sink`]s when an error occurs in renaming a file.
    ///
    /// [`Sink`]: crate::sink::Sink
    RenameFile(io::Error),

    /// Returned by [`Sink`]s when an error occurs in removing a file.
    ///
    /// [`Sink`]: crate::sink::Sink
    RemoveFile(io::Error),

    /// Returned by [`from_str`] when the string doesn't match any of the log
    /// levels.
    ///
    /// [`from_str`]: std::str::FromStr::from_str
    ParseLevel(String),

    /// Returned if an invalid argument was passed in.
    InvalidArgument(InvalidArgumentError),

    /// Returned by [`Sink`]s when an error occurs in sending to the channel.
    ///
    /// [`Sink`]: crate::sink::Sink
    #[cfg(feature = "multi-thread")]
    SendToChannel(SendToChannelError, SendToChannelErrorDropped),

    /// Returned by [`runtime_pattern!`] when the pattern is failed to be built
    /// at runtime.
    ///
    /// [`runtime_pattern!`]: crate::formatter::runtime_pattern
    #[cfg(feature = "runtime-pattern")]
    BuildPattern(BuildPatternError),

    /// Returned by [`Formatter`]s when an error occurs in serializing a log.
    ///
    /// [`Formatter`]: crate::formatter::Formatter
    #[cfg(feature = "serde")]
    SerializeRecord(io::Error),

    /// Returned from a downstream implementation of `spdlog-rs`. Its actual
    /// error type may be a downstream struct.
    ///
    /// When downstream crates encounter errors, other more specific error
    /// variants should be used first, this variant should only be used as a
    /// last option when other variant types are incompatible.
    Downstream(Box<dyn StdError + Send + Sync>),

    /// Returned when multiple errors occurred.
    Multiple(Vec<Error>),

    #[cfg(test)]
    #[doc(hidden)]
    __ForInternalTestsUseOnly(i32),
}

impl StdError for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FormatRecord(err) => write!(f, "format record error: {err}"),
            Self::WriteRecord(err) => write!(f, "write record error: {err}"),
            Self::FlushBuffer(err) => write!(f, "flush buffer error: {err}"),
            Self::CreateDirectory(err) => write!(f, "create directory error: {err}"),
            Self::OpenFile(err) => write!(f, "open file error: {err}"),
            Self::QueryFileMetadata(err) => write!(f, "query file metadata error: {err}"),
            Self::RenameFile(err) => write!(f, "rename file error: {err}"),
            Self::RemoveFile(err) => write!(f, "remove file error: {err}"),
            Self::ParseLevel(level_str) => {
                write!(f, "attempted to convert a string that doesn't match an existing log level: {level_str}")
            }
            Self::InvalidArgument(err) => write!(f, "invalid argument {err}"),
            #[cfg(feature = "multi-thread")]
            Self::SendToChannel(err, _) => write!(f, "failed to send message to channel: {err}"),
            #[cfg(feature = "runtime-pattern")]
            Self::BuildPattern(err) => write!(f, "failed to build pattern at runtime: {err}"),
            #[cfg(feature = "serde")]
            Self::SerializeRecord(err) => write!(f, "failed to serialize log: {err}"),
            Self::Downstream(err) => write!(f, "{err}"),
            Self::Multiple(errs) => write!(f, "{errs:?}"),
            #[cfg(test)]
            Self::__ForInternalTestsUseOnly(i) => write!(f, "{i}"),
        }
    }
}

impl From<InvalidArgumentError> for Error {
    fn from(err: InvalidArgumentError) -> Self {
        Self::InvalidArgument(err)
    }
}

/// Indicates that an invalid parameter was specified.
#[derive(Debug)]
#[non_exhaustive]
pub enum InvalidArgumentError {
    /// Invalid logger name.
    ///
    /// See the documentation of [`LoggerBuilder::name`] for the name
    /// requirements.
    ///
    /// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
    LoggerName(SetLoggerNameError),

    /// Invalid [`RotationPolicy`].
    ///
    /// See the documentation of [`RotationPolicy`] for the input requirements.
    ///
    /// [`RotationPolicy`]: crate::sink::RotationPolicy
    RotationPolicy(String),

    /// Invalid thread pool capacity.
    #[deprecated(
        since = "0.5.0",
        note = "non-zero thread pool capacity is now guarded by NonZeroUsize type"
    )]
    ThreadPoolCapacity(String),
}

impl StdError for InvalidArgumentError {}

impl Display for InvalidArgumentError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LoggerName(err) => write!(f, "'logger name': {err}"),
            Self::RotationPolicy(value) => write!(f, "'rotation policy': {value}"),
            #[allow(deprecated)]
            Self::ThreadPoolCapacity(value) => write!(f, "'thread pool capacity': {value}"),
        }
    }
}

impl From<SetLoggerNameError> for InvalidArgumentError {
    fn from(err: SetLoggerNameError) -> Self {
        Self::LoggerName(err)
    }
}

/// Indicates that an invalid logger name was set.
///
/// See the documentation of [`LoggerBuilder::name`] for the name requirements.
///
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
#[derive(Debug)]
pub struct SetLoggerNameError {
    name: String,
}

impl SetLoggerNameError {
    #[must_use]
    pub(crate) fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }

    #[cfg(test)]
    #[must_use]
    pub(crate) fn name(&self) -> &str {
        &self.name
    }
}

impl StdError for SetLoggerNameError {}

impl Display for SetLoggerNameError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "name '{}' contains disallowed characters", self.name)
    }
}

/// Indicates that an error occurred while sending to channel.
#[cfg(feature = "multi-thread")]
#[derive(Debug)]
#[non_exhaustive]
pub enum SendToChannelError {
    /// The channel is full.
    ///
    /// The variant returned only when [`OverflowPolicy::DropIncoming`] is used.
    ///
    /// [`OverflowPolicy::DropIncoming`]: crate::sink::async_sink::OverflowPolicy::DropIncoming
    Full,

    /// The channel is disconnected.
    Disconnected,
}

#[cfg(feature = "multi-thread")]
impl StdError for SendToChannelError {}

#[cfg(feature = "multi-thread")]
impl Display for SendToChannelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Full => write!(f, "the channel is full"),
            Self::Disconnected => write!(f, "the channel is disconnected"),
        }
    }
}

/// Contains data that is dropped after sending to the channel failed.
///
/// You can handle them manually or just ignore them.
#[cfg(feature = "multi-thread")]
#[derive(Debug)]
#[non_exhaustive]
pub enum SendToChannelErrorDropped {
    /// A `log` operation and a record are dropped.
    Record(Box<RecordOwned>), // Boxed because `RecordOwned` is a bit large.
    /// A `flush` operation is dropped.
    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]
    pub(crate) fn from_crossbeam_send(err: crossbeam::channel::SendError<Task>) -> Self {
        Self::SendToChannel(
            SendToChannelError::Disconnected,
            SendToChannelErrorDropped::from_task(err.0),
        )
    }

    #[must_use]
    pub(crate) fn from_crossbeam_try_send(err: crossbeam::channel::TrySendError<Task>) -> Self {
        use crossbeam::channel::TrySendError;

        let (error, dropped_task) = match err {
            TrySendError::Full(dropped) => (SendToChannelError::Full, dropped),
            TrySendError::Disconnected(dropped) => (SendToChannelError::Disconnected, dropped),
        };

        Self::SendToChannel(error, SendToChannelErrorDropped::from_task(dropped_task))
    }
}

#[cfg(feature = "multi-thread")]
impl SendToChannelErrorDropped {
    #[must_use]
    pub(crate) fn from_task(task: Task) -> Self {
        match task {
            Task::Log { record, .. } => Self::Record(Box::new(record)),
            Task::Flush { .. } => Self::Flush,
            #[cfg(test)]
            Task::__ForTestUse { .. } => unreachable!(),
        }
    }
}

/// Indicates that an error occurred while building a pattern at compile-time.
#[cfg(feature = "runtime-pattern")]
#[derive(Debug)]
pub struct BuildPatternError(pub(crate) spdlog_internal::pattern_parser::Error);

#[cfg(feature = "runtime-pattern")]
impl StdError for BuildPatternError {}

#[cfg(feature = "runtime-pattern")]
impl Display for BuildPatternError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// The result type of this crate.
pub type Result<T> = result::Result<T, Error>;

/// Represents an error handler.
///
/// In most cases, it can be constructed by just a `.into()`.
///
/// Call [`ErrorHandler::default`] to construct an empty error handler, when an
/// error is triggered, a built-in fallback handler will be used which prints
/// the error to `stderr`.
#[derive(Clone)]
pub struct ErrorHandler(Option<Arc<dyn Fn(Error) + Send + Sync>>);

impl ErrorHandler {
    /// Constructs an error handler with a custom function.
    #[must_use]
    pub fn new<F>(custom: F) -> Self
    where
        F: Fn(Error) + Send + Sync + 'static,
    {
        Self(Some(Arc::new(custom)))
    }

    /// Calls the error handler with an error.
    pub fn call(&self, err: Error) {
        self.call_internal("External", err);
    }

    pub(crate) fn call_internal(&self, from: impl AsRef<str>, err: Error) {
        if let Some(handler) = &self.0 {
            handler(err);
        } else {
            Self::default_impl(from, err);
        }
    }

    fn default_impl(from: impl AsRef<str>, error: Error) {
        if let Error::Multiple(errs) = error {
            errs.into_iter()
                .for_each(|err| Self::default_impl(from.as_ref(), err));
            return;
        }

        let date = chrono::Local::now()
            .format("%Y-%m-%d %H:%M:%S.%3f")
            .to_string();

        // https://github.com/SpriteOvO/spdlog-rs/discussions/87
        //
        // Don't use `eprintln!` here, as it may fail to write and then panic.
        let _ = writeln!(
            io::stderr(),
            "[*** SPDLOG-RS UNHANDLED ERROR ***] [{}] [{}] {}",
            date,
            from.as_ref(),
            error
        );
    }
}

impl<F> From<F> for ErrorHandler
where
    F: Fn(Error) + Send + Sync + 'static,
{
    fn from(handler: F) -> Self {
        Self::new(handler)
    }
}

impl Default for ErrorHandler {
    /// Constructs an error handler with the built-in handler which prints
    /// errors to `stderr`.
    fn default() -> Self {
        Self(None)
    }
}

impl fmt::Debug for ErrorHandler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ErrorHandler")
            .field(&self.0.as_ref().map_or("default", |_| "custom"))
            .finish()
    }
}

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

    #[test]
    fn error_traits() {
        assert_trait!(Error: Send + Sync);
    }

    #[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)])
        ));
    }
}