Skip to content

Commit 1cb523b

Browse files
daxpeddahawkw
andauthored
subscriber: add ability to disable ANSI without crate feature (#2532)
## Motivation 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. I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible. ## Solution This changes `fmt::Subscriber::with_ansi()` to not require the "ansi" feature flag. This way, `with_ansi(false)` can be called even when the "ansi" feature is disabled. Calling `with_ansi(true)` when the "ansi" feature is not enabled will panic in debug mode, or print a warning if debug assertions are disabled. Co-authored-by: Eliza Weisman <[email protected]>
1 parent a351b97 commit 1cb523b

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

tracing-subscriber/src/fmt/fmt_subscriber.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,32 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
274274
}
275275
}
276276

277-
/// Enable ANSI terminal colors for formatted output.
278-
#[cfg(feature = "ansi")]
279-
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
277+
/// Sets whether or not the formatter emits ANSI terminal escape codes
278+
/// for colors and other text formatting.
279+
///
280+
/// Enabling ANSI escapes (calling `with_ansi(true)`) requires the "ansi"
281+
/// crate feature flag. Calling `with_ansi(true)` without the "ansi"
282+
/// feature flag enabled will panic if debug assertions are enabled, or
283+
/// print a warning otherwise.
284+
///
285+
/// This method itself is still available without the feature flag. This
286+
/// is to allow ANSI escape codes to be explicitly *disabled* without
287+
/// having to opt-in to the dependencies required to emit ANSI formatting.
288+
/// This way, code which constructs a formatter that should never emit
289+
/// ANSI escape codes can ensure that they are not used, regardless of
290+
/// whether or not other crates in the dependency graph enable the "ansi"
291+
/// feature flag.
280292
pub fn with_ansi(self, ansi: bool) -> Self {
293+
#[cfg(not(feature = "ansi"))]
294+
if ansi {
295+
const ERROR: &str =
296+
"tracing-subscriber: the `ansi` crate feature is required to enable ANSI terminal colors";
297+
#[cfg(debug_assertions)]
298+
panic!("{}", ERROR);
299+
#[cfg(not(debug_assertions))]
300+
eprintln!("{}", ERROR);
301+
}
302+
281303
Subscriber {
282304
is_ansi: ansi,
283305
..self

tracing-subscriber/src/fmt/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,21 @@ where
610610
}
611611
}
612612

613-
/// Enable ANSI terminal colors for formatted output.
614-
#[cfg(feature = "ansi")]
615-
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
613+
/// Sets whether or not the formatter emits ANSI terminal escape codes
614+
/// for colors and other text formatting.
615+
///
616+
/// Enabling ANSI escapes (calling `with_ansi(true)`) requires the "ansi"
617+
/// crate feature flag. Calling `with_ansi(true)` without the "ansi"
618+
/// feature flag enabled will panic if debug assertions are enabled, or
619+
/// print a warning otherwise.
620+
///
621+
/// This method itself is still available without the feature flag. This
622+
/// is to allow ANSI escape codes to be explicitly *disabled* without
623+
/// having to opt-in to the dependencies required to emit ANSI formatting.
624+
/// This way, code which constructs a formatter that should never emit
625+
/// ANSI escape codes can ensure that they are not used, regardless of
626+
/// whether or not other crates in the dependency graph enable the "ansi"
627+
/// feature flag.
616628
pub fn with_ansi(self, ansi: bool) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
617629
CollectorBuilder {
618630
inner: self.inner.with_ansi(ansi),

0 commit comments

Comments
 (0)