Skip to content

Instantly share code, notes, and snippets.

@SnoozeTime
Created September 25, 2019 05:30
Show Gist options
  • Save SnoozeTime/6d9b07c4d7ea9ef89ebcd4f1fa4b789d to your computer and use it in GitHub Desktop.
Save SnoozeTime/6d9b07c4d7ea9ef89ebcd4f1fa4b789d to your computer and use it in GitHub Desktop.
diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml
index 3287729..dc43ad1 100644
--- a/tracing-subscriber/Cargo.toml
+++ b/tracing-subscriber/Cargo.toml
@@ -20,7 +20,7 @@ keywords = ["logging", "tracing", "metrics", "subscriber"]
[features]
-default = ["env-filter", "smallvec", "fmt", "ansi", "chrono", "tracing-log"]
+default = ["env-filter", "smallvec", "fmt", "chrono", "tracing-log"]
env-filter = ["matchers", "regex", "lazy_static"]
fmt = ["owning_ref"]
ansi = ["fmt", "ansi_term"]
diff --git a/tracing-subscriber/src/fmt/format.rs b/tracing-subscriber/src/fmt/format.rs
index 0d89e1e..233915f 100644
--- a/tracing-subscriber/src/fmt/format.rs
+++ b/tracing-subscriber/src/fmt/format.rs
@@ -147,11 +147,24 @@ where
#[cfg(not(feature = "tracing-log"))]
let meta = event.metadata();
time::write(&self.timer, writer, self.ansi)?;
+
+ let (fmt_level, full_ctx) = {
+
+ #[cfg(feature = "ansi")]
+ {
+ (FmtLevel::new(meta.level(), self.ansi), FullCtx::new(&ctx, self.ansi))
+ }
+ #[cfg(not(feature = "ansi"))]
+ {
+ (FmtLevel::new(meta.level()), FullCtx::new(&ctx))
+ }
+ };
+
write!(
writer,
"{} {}{}: ",
- FmtLevel::new(meta.level(), self.ansi),
- FullCtx::new(&ctx, self.ansi),
+ fmt_level,
+ full_ctx,
if self.display_target {
meta.target()
} else {
@@ -184,11 +197,23 @@ where
#[cfg(not(feature = "tracing-log"))]
let meta = event.metadata();
time::write(&self.timer, writer, self.ansi)?;
+
+
+ let (fmt_level, fmt_ctx) = {
+ #[cfg(feature = "ansi")]
+ {
+ (FmtLevel::new(meta.level(), self.ansi), FmtCtx::new(&ctx, self.ansi))
+ }
+ #[cfg(not(feature = "ansi"))]
+ {
+ (FmtLevel::new(meta.level()), FmtCtx::new(&ctx))
+ }
+ };
write!(
writer,
"{} {}{}: ",
- FmtLevel::new(meta.level(), self.ansi),
- FmtCtx::new(&ctx, self.ansi),
+ fmt_level,
+ fmt_ctx,
if self.display_target {
meta.target()
} else {
@@ -292,14 +317,21 @@ impl<'a> fmt::Debug for Recorder<'a> {
struct FmtCtx<'a, N> {
ctx: &'a span::Context<'a, N>,
- #[allow(unused)]
+ #[cfg(feature = "ansi")]
ansi: bool,
}
impl<'a, N: 'a> FmtCtx<'a, N> {
+
+ #[cfg(feature = "ansi")]
pub(crate) fn new(ctx: &'a span::Context<'a, N>, ansi: bool) -> Self {
Self { ctx, ansi }
}
+
+ #[cfg(not(feature = "ansi"))]
+ pub(crate) fn new(ctx: &'a span::Context<'a, N>) -> Self {
+ Self { ctx }
+ }
}
#[cfg(feature = "ansi")]
@@ -348,14 +380,20 @@ impl<'a, N> fmt::Display for FmtCtx<'a, N> {
struct FullCtx<'a, N> {
ctx: &'a span::Context<'a, N>,
- #[allow(unused)]
+ #[cfg(feature = "ansi")]
ansi: bool,
}
impl<'a, N: 'a> FullCtx<'a, N> {
+ #[cfg(feature = "ansi")]
pub(crate) fn new(ctx: &'a span::Context<'a, N>, ansi: bool) -> Self {
Self { ctx, ansi }
}
+
+ #[cfg(not(feature = "ansi"))]
+ pub(crate) fn new(ctx: &'a span::Context<'a, N>) -> Self {
+ Self { ctx }
+ }
}
#[cfg(feature = "ansi")]
@@ -411,14 +449,20 @@ impl<'a, N> fmt::Display for FullCtx<'a, N> {
struct FmtLevel<'a> {
level: &'a Level,
- #[allow(unused)]
+ #[cfg(feature = "ansi")]
ansi: bool,
}
impl<'a> FmtLevel<'a> {
+ #[cfg(feature = "ansi")]
pub(crate) fn new(level: &'a Level, ansi: bool) -> Self {
Self { level, ansi }
}
+
+ #[cfg(not(feature = "ansi"))]
+ pub(crate) fn new(level: &'a Level) -> Self {
+ Self { level }
+ }
}
#[cfg(not(feature = "ansi"))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment