From e71c90c97d2c71de29f233ea7149484b9a34b4e9 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Sat, 31 Oct 2020 16:54:24 +0900 Subject: [PATCH 1/3] refactor lib.rs --- tracing-subscriber/src/lib.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tracing-subscriber/src/lib.rs b/tracing-subscriber/src/lib.rs index bb9cacfeff..d8a85300d9 100644 --- a/tracing-subscriber/src/lib.rs +++ b/tracing-subscriber/src/lib.rs @@ -109,9 +109,6 @@ mod macros; pub mod field; pub mod filter; -#[cfg(feature = "fmt")] -#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))] -pub mod fmt; pub mod prelude; pub mod registry; pub mod reload; @@ -120,9 +117,13 @@ pub(crate) mod sync; pub(crate) mod thread; pub mod util; -#[cfg(feature = "env-filter")] -#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] -pub use filter::EnvFilter; +cfg_feature!("fmt", { + pub mod fmt; +}); + +cfg_feature!("env-filter", { + pub use filter::EnvFilter; +}); pub use subscribe::Subscribe; From 4deff5ed8dda1779085a43993b5cd8552d968e95 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Sat, 31 Oct 2020 16:54:36 +0900 Subject: [PATCH 2/3] refactor time module --- tracing-subscriber/src/fmt/time/mod.rs | 142 ++++++++++++------------- 1 file changed, 66 insertions(+), 76 deletions(-) diff --git a/tracing-subscriber/src/fmt/time/mod.rs b/tracing-subscriber/src/fmt/time/mod.rs index e388583b0e..c708e60b17 100644 --- a/tracing-subscriber/src/fmt/time/mod.rs +++ b/tracing-subscriber/src/fmt/time/mod.rs @@ -153,99 +153,89 @@ impl Default for ChronoFmtType { } } -/// Retrieve and print the current UTC time. -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -#[derive(Debug, Clone, Eq, PartialEq, Default)] -pub struct ChronoUtc { - format: ChronoFmtType, -} +cfg_feature!("chrono", { + /// Retrieve and print the current UTC time. + #[derive(Debug, Clone, Eq, PartialEq, Default)] + pub struct ChronoUtc { + format: ChronoFmtType, + } -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -impl ChronoUtc { - /// Format the time using the [`RFC 3339`] format - /// (a subset of [`ISO 8601`]). - /// - /// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 - /// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 - pub fn rfc3339() -> Self { - ChronoUtc { - format: ChronoFmtType::Rfc3339, + impl ChronoUtc { + /// Format the time using the [`RFC 3339`] format + /// (a subset of [`ISO 8601`]). + /// + /// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 + /// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 + pub fn rfc3339() -> Self { + ChronoUtc { + format: ChronoFmtType::Rfc3339, + } } - } - /// Format the time using the given format string. - /// - /// See [`chrono::format::strftime`] - /// for details on the supported syntax. - /// - /// [`chrono::format::strftime`]: https://docs.rs/chrono/0.4.9/chrono/format/strftime/index.html - pub fn with_format(format_string: String) -> Self { - ChronoUtc { - format: ChronoFmtType::Custom(format_string), + /// Format the time using the given format string. + /// + /// See [`chrono::format::strftime`] + /// for details on the supported syntax. + /// + /// [`chrono::format::strftime`]: https://docs.rs/chrono/0.4.9/chrono/format/strftime/index.html + pub fn with_format(format_string: String) -> Self { + ChronoUtc { + format: ChronoFmtType::Custom(format_string), + } } } -} -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -impl FormatTime for ChronoUtc { - fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { - let time = chrono::Utc::now(); - match self.format { - ChronoFmtType::Rfc3339 => write!(w, "{}", time.to_rfc3339()), - ChronoFmtType::Custom(ref format_str) => write!(w, "{}", time.format(format_str)), + impl FormatTime for ChronoUtc { + fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { + let time = chrono::Utc::now(); + match self.format { + ChronoFmtType::Rfc3339 => write!(w, "{}", time.to_rfc3339()), + ChronoFmtType::Custom(ref format_str) => write!(w, "{}", time.format(format_str)), + } } } -} -/// Retrieve and print the current local time. -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -#[derive(Debug, Clone, Eq, PartialEq, Default)] -pub struct ChronoLocal { - format: ChronoFmtType, -} + /// Retrieve and print the current local time. + #[derive(Debug, Clone, Eq, PartialEq, Default)] + pub struct ChronoLocal { + format: ChronoFmtType, + } -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -impl ChronoLocal { - /// Format the time using the [`RFC 3339`] format - /// (a subset of [`ISO 8601`]). - /// - /// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 - /// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 - pub fn rfc3339() -> Self { - ChronoLocal { - format: ChronoFmtType::Rfc3339, + impl ChronoLocal { + /// Format the time using the [`RFC 3339`] format + /// (a subset of [`ISO 8601`]). + /// + /// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 + /// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 + pub fn rfc3339() -> Self { + ChronoLocal { + format: ChronoFmtType::Rfc3339, + } } - } - /// Format the time using the given format string. - /// - /// See [`chrono::format::strftime`] - /// for details on the supported syntax. - /// - /// [`chrono::format::strftime`]: https://docs.rs/chrono/0.4.9/chrono/format/strftime/index.html - pub fn with_format(format_string: String) -> Self { - ChronoLocal { - format: ChronoFmtType::Custom(format_string), + /// Format the time using the given format string. + /// + /// See [`chrono::format::strftime`] + /// for details on the supported syntax. + /// + /// [`chrono::format::strftime`]: https://docs.rs/chrono/0.4.9/chrono/format/strftime/index.html + pub fn with_format(format_string: String) -> Self { + ChronoLocal { + format: ChronoFmtType::Custom(format_string), + } } } -} -#[cfg(feature = "chrono")] -#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] -impl FormatTime for ChronoLocal { - fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { - let time = chrono::Local::now(); - match self.format { - ChronoFmtType::Rfc3339 => write!(w, "{} ", time.to_rfc3339()), - ChronoFmtType::Custom(ref format_str) => write!(w, "{} ", time.format(format_str)), + impl FormatTime for ChronoLocal { + fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { + let time = chrono::Local::now(); + match self.format { + ChronoFmtType::Rfc3339 => write!(w, "{} ", time.to_rfc3339()), + ChronoFmtType::Custom(ref format_str) => write!(w, "{} ", time.format(format_str)), + } } } -} +}); #[inline(always)] #[cfg(feature = "ansi")] From 64492610bc1b54ec35f911d26b9807024bc4aa4a Mon Sep 17 00:00:00 2001 From: k-nasa Date: Sat, 31 Oct 2020 16:58:45 +0900 Subject: [PATCH 3/3] refactor fmt/fmt_subscriber --- tracing-subscriber/src/fmt/fmt_subscriber.rs | 45 +++++++++----------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/tracing-subscriber/src/fmt/fmt_subscriber.rs b/tracing-subscriber/src/fmt/fmt_subscriber.rs index 0e60eb8937..1ce199ee36 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -260,19 +260,6 @@ where } } - /// Enable ANSI encoding for formatted events. - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn with_ansi(self, ansi: bool) -> Subscriber, W> { - Subscriber { - fmt_event: self.fmt_event.with_ansi(ansi), - fmt_fields: self.fmt_fields, - fmt_span: self.fmt_span, - make_writer: self.make_writer, - _inner: self._inner, - } - } - /// Sets whether or not an event's target is displayed. pub fn with_target(self, display_target: bool) -> Subscriber, W> { Subscriber { @@ -342,19 +329,29 @@ where _inner: self._inner, } } + cfg_feature!("ansi", { + /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). + pub fn pretty(self) -> Subscriber, W> { + Subscriber { + fmt_event: self.fmt_event.pretty(), + fmt_fields: format::Pretty::default(), + fmt_span: self.fmt_span, + make_writer: self.make_writer, + _inner: self._inner, + } + } - /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn pretty(self) -> Subscriber, W> { - Subscriber { - fmt_event: self.fmt_event.pretty(), - fmt_fields: format::Pretty::default(), - fmt_span: self.fmt_span, - make_writer: self.make_writer, - _inner: self._inner, + /// Enable ANSI encoding for formatted events. + pub fn with_ansi(self, ansi: bool) -> Subscriber, W> { + Subscriber { + fmt_event: self.fmt_event.with_ansi(ansi), + fmt_fields: self.fmt_fields, + fmt_span: self.fmt_span, + make_writer: self.make_writer, + _inner: self._inner, + } } - } + }); /// Sets the subscriber being built to use a [JSON formatter](../fmt/format/struct.Json.html). ///