Skip to content

Commit

Permalink
Implement Debug for Logger (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Clavette <[email protected]>
Co-authored-by: Asuna <[email protected]>
  • Loading branch information
3 people committed Jan 20, 2025
1 parent 94bf642 commit 3b81097
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
37 changes: 33 additions & 4 deletions spdlog/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{result::Result as StdResult, time::Duration};
use std::{fmt::Debug, result::Result as StdResult, time::Duration};

use crate::{
env_level,
Expand Down Expand Up @@ -115,6 +115,36 @@ pub struct Logger {
periodic_flusher: Mutex<Option<(Duration, PeriodicWorker)>>,
}

impl Debug for Logger {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Logger")
.field("name", &self.name)
.field("level_filter", &self.level_filter())
.field(
"sinks",
&self
.sinks
.iter()
.map(|sink| sink.level_filter())
.collect::<Vec<_>>(),
)
.field("flush_level_filter", &self.flush_level_filter())
.field("error_handler", &self.error_handler.read())
.field(
"periodic_flusher",
&self
.periodic_flusher
.lock()
.as_deref()
.map(|opt| opt.as_ref().map(|(dur, _)| *dur))
.as_ref()
.map(|dur| dur as &dyn Debug)
.unwrap_or(&"*lock is poisoned*"),
)
.finish()
}
}

impl Logger {
/// Gets a [`LoggerBuilder`] with default parameters:
///
Expand Down Expand Up @@ -665,9 +695,8 @@ mod tests {
use crate::{prelude::*, test_utils::*};

#[test]
fn send_sync() {
assert_send::<Logger>();
assert_sync::<Logger>();
fn logger_traits() {
assert_trait!(Logger: Send + Sync + Debug);
}

#[test]
Expand Down
13 changes: 10 additions & 3 deletions spdlog/src/test_utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,16 @@ pub fn build_test_logger(cb: impl FnOnce(&mut LoggerBuilder) -> &mut LoggerBuild
builder.build().unwrap()
}

pub fn assert_send<T: Send>() {}

pub fn assert_sync<T: Sync>() {}
#[doc(hidden)]
#[macro_export]
macro_rules! assert_trait {
($type:ty: $($traits:tt)+) => {{
fn __assert_trait<T: $($traits)+>() {}
__assert_trait::<$type>();
}};
}
#[allow(unused_imports)]
pub use assert_trait;

#[must_use]
pub fn echo_logger_from_pattern(
Expand Down

0 comments on commit 3b81097

Please sign in to comment.