diff --git a/tracing-subscriber/src/fmt/fmt_subscriber.rs b/tracing-subscriber/src/fmt/fmt_subscriber.rs index 740ff08f9d..5429859db9 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -254,19 +254,6 @@ where } } - /// Enable ANSI terminal colors for formatted output. - #[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 { @@ -336,19 +323,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 terminal colors for formatted output. + 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](format::Json). /// diff --git a/tracing-subscriber/src/fmt/time/mod.rs b/tracing-subscriber/src/fmt/time/mod.rs index c75feca271..2a4a0b6b88 100644 --- a/tracing-subscriber/src/fmt/time/mod.rs +++ b/tracing-subscriber/src/fmt/time/mod.rs @@ -111,10 +111,10 @@ impl From for Uptime { } } -#[cfg(feature = "chrono")] -impl FormatTime for SystemTime { +impl FormatTime for Uptime { fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { - write!(w, "{}", chrono::Local::now().format("%b %d %H:%M:%S%.3f")) + let e = self.epoch.elapsed(); + write!(w, "{:4}.{:09}s", e.as_secs(), e.subsec_nanos()) } } @@ -129,121 +129,107 @@ impl FormatTime for SystemTime { } } -impl FormatTime for Uptime { - fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { - let e = self.epoch.elapsed(); - write!(w, "{:4}.{:09}s", e.as_secs(), e.subsec_nanos()) +cfg_feature!("chrono", { + impl FormatTime for SystemTime { + fn format_time(&self, w: &mut dyn fmt::Write) -> fmt::Result { + write!(w, "{}", chrono::Local::now().format("%b %d %H:%M:%S%.3f")) + } } -} - -/// The RFC 3339 format is used by default and using -/// this struct allows chrono to bypass the parsing -/// used when a custom format string is provided -#[cfg(feature = "chrono")] -#[derive(Debug, Clone, Eq, PartialEq)] -enum ChronoFmtType { - Rfc3339, - Custom(String), -} -#[cfg(feature = "chrono")] -impl Default for ChronoFmtType { - fn default() -> Self { - ChronoFmtType::Rfc3339 + /// The RFC 3339 format is used by default and using + /// this struct allows chrono to bypass the parsing + /// used when a custom format string is provided + #[derive(Debug, Clone, Eq, PartialEq)] + enum ChronoFmtType { + Rfc3339, + Custom(String), } -} - -/// 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")] -#[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 Default for ChronoFmtType { + fn default() -> Self { + ChronoFmtType::Rfc3339 } } + /// Retrieve and print the current UTC time. + #[derive(Debug, Clone, Eq, PartialEq, Default)] + pub struct ChronoUtc { + format: ChronoFmtType, + } - /// Format the time using the given format string. - /// - /// See [`chrono::format::strftime`] - /// for details on the supported syntax. - /// - pub fn with_format(format_string: String) -> Self { - ChronoUtc { - format: ChronoFmtType::Custom(format_string), + 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. + /// + 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. - /// - 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. + /// + 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")] diff --git a/tracing-subscriber/src/lib.rs b/tracing-subscriber/src/lib.rs index 19ff58f7dc..a48986d673 100644 --- a/tracing-subscriber/src/lib.rs +++ b/tracing-subscriber/src/lib.rs @@ -108,9 +108,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; @@ -119,9 +116,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;