From 2ebcd2b24636a11fae275cfd21abbd788d654648 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Sun, 26 Mar 2023 20:19:33 +0200 Subject: [PATCH 1/2] subscriber: add ability to disable ANSI without crate feature Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the `ansi` crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies `ansi` does. This removes the crate feature requirement on `fmt::Subscriber::with_ansi()`, but will print a warning if trying to enable ANSI terminal colors without the crate feature. --- tracing-subscriber/src/fmt/fmt_subscriber.rs | 12 ++++++++++-- tracing-subscriber/src/fmt/mod.rs | 2 -- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tracing-subscriber/src/fmt/fmt_subscriber.rs b/tracing-subscriber/src/fmt/fmt_subscriber.rs index 9887b711ff..f6e73101e2 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -275,9 +275,17 @@ impl Subscriber { } /// Enable ANSI terminal colors for formatted output. - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] pub fn with_ansi(self, ansi: bool) -> Self { + #[cfg(not(feature = "ansi"))] + if ansi { + const ERROR: &str = + "tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; + #[cfg(debug_assertions)] + panic!("{}", ERROR); + #[cfg(not(debug_assertions))] + eprintln!("{}", ERROR); + } + Subscriber { is_ansi: ansi, ..self diff --git a/tracing-subscriber/src/fmt/mod.rs b/tracing-subscriber/src/fmt/mod.rs index ba5066d2ec..24eb0a9226 100644 --- a/tracing-subscriber/src/fmt/mod.rs +++ b/tracing-subscriber/src/fmt/mod.rs @@ -611,8 +611,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) -> CollectorBuilder, F, W> { CollectorBuilder { inner: self.inner.with_ansi(ansi), From f035446dbf6b99cf413fe33912eb49fdf050a8d1 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Fri, 14 Apr 2023 20:25:30 +0200 Subject: [PATCH 2/2] Address review Co-Authored-By: Eliza Weisman --- tracing-subscriber/src/fmt/fmt_subscriber.rs | 18 ++++++++++++++++-- tracing-subscriber/src/fmt/mod.rs | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/tracing-subscriber/src/fmt/fmt_subscriber.rs b/tracing-subscriber/src/fmt/fmt_subscriber.rs index f6e73101e2..1b89039261 100644 --- a/tracing-subscriber/src/fmt/fmt_subscriber.rs +++ b/tracing-subscriber/src/fmt/fmt_subscriber.rs @@ -274,12 +274,26 @@ impl Subscriber { } } - /// Enable ANSI terminal colors for formatted output. + /// Sets whether or not the formatter emits ANSI terminal escape codes + /// for colors and other text formatting. + /// + /// Enabling ANSI escapes (calling `with_ansi(true)`) requires the "ansi" + /// crate feature flag. Calling `with_ansi(true)` without the "ansi" + /// feature flag enabled will panic if debug assertions are enabled, or + /// print a warning otherwise. + /// + /// This method itself is still available without the feature flag. This + /// is to allow ANSI escape codes to be explicitly *disabled* without + /// having to opt-in to the dependencies required to emit ANSI formatting. + /// This way, code which constructs a formatter that should never emit + /// ANSI escape codes can ensure that they are not used, regardless of + /// whether or not other crates in the dependency graph enable the "ansi" + /// feature flag. pub fn with_ansi(self, ansi: bool) -> Self { #[cfg(not(feature = "ansi"))] if ansi { const ERROR: &str = - "tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; + "tracing-subscriber: the `ansi` crate feature is required to enable ANSI terminal colors"; #[cfg(debug_assertions)] panic!("{}", ERROR); #[cfg(not(debug_assertions))] diff --git a/tracing-subscriber/src/fmt/mod.rs b/tracing-subscriber/src/fmt/mod.rs index 24eb0a9226..38acf4d092 100644 --- a/tracing-subscriber/src/fmt/mod.rs +++ b/tracing-subscriber/src/fmt/mod.rs @@ -610,7 +610,21 @@ where } } - /// Enable ANSI terminal colors for formatted output. + /// Sets whether or not the formatter emits ANSI terminal escape codes + /// for colors and other text formatting. + /// + /// Enabling ANSI escapes (calling `with_ansi(true)`) requires the "ansi" + /// crate feature flag. Calling `with_ansi(true)` without the "ansi" + /// feature flag enabled will panic if debug assertions are enabled, or + /// print a warning otherwise. + /// + /// This method itself is still available without the feature flag. This + /// is to allow ANSI escape codes to be explicitly *disabled* without + /// having to opt-in to the dependencies required to emit ANSI formatting. + /// This way, code which constructs a formatter that should never emit + /// ANSI escape codes can ensure that they are not used, regardless of + /// whether or not other crates in the dependency graph enable the "ansi" + /// feature flag. pub fn with_ansi(self, ansi: bool) -> CollectorBuilder, F, W> { CollectorBuilder { inner: self.inner.with_ansi(ansi),