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 Debug for Logger #81

Merged
Merged
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
35 changes: 32 additions & 3 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())
Nicholas-Clavette marked this conversation as resolved.
Show resolved Hide resolved
.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 @@ -666,8 +696,7 @@ mod tests {

#[test]
fn send_sync() {
assert_send::<Logger>();
assert_sync::<Logger>();
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
Loading