Skip to content

Commit b6b7caa

Browse files
authored
[ty] Change layout of extra verbose output and respect --color for verbose output (#18089)
1 parent 46be305 commit b6b7caa

File tree

7 files changed

+30
-71
lines changed

7 files changed

+30
-71
lines changed

Cargo.lock

Lines changed: 1 addition & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
163163
"env-filter",
164164
"fmt",
165165
] }
166-
tracing-tree = { version = "0.4.0" }
167166
tryfn = { version = "0.2.1" }
168167
typed-arena = { version = "2.0.2" }
169168
unic-ucd-category = { version = "0.9" }

crates/ruff_db/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ path-slash = { workspace = true }
3636
thiserror = { workspace = true }
3737
tracing = { workspace = true }
3838
tracing-subscriber = { workspace = true, optional = true }
39-
tracing-tree = { workspace = true, optional = true }
4039
rustc-hash = { workspace = true }
4140
zip = { workspace = true }
4241

@@ -55,4 +54,4 @@ cache = ["ruff_cache"]
5554
os = ["ignore", "dep:etcetera"]
5655
serde = ["dep:serde", "camino/serde1"]
5756
# Exposes testing utilities.
58-
testing = ["tracing-subscriber", "tracing-tree"]
57+
testing = ["tracing-subscriber"]

crates/ruff_db/src/testing.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ pub fn setup_logging_with_filter(filter: &str) -> Option<LoggingGuard> {
141141
#[derive(Debug)]
142142
pub struct LoggingBuilder {
143143
filter: EnvFilter,
144-
hierarchical: bool,
145144
}
146145

147146
impl LoggingBuilder {
@@ -154,50 +153,26 @@ impl LoggingBuilder {
154153
.parse()
155154
.expect("Hardcoded directive to be valid"),
156155
),
157-
hierarchical: false,
158156
}
159157
}
160158

161159
pub fn with_filter(filter: &str) -> Option<Self> {
162160
let filter = EnvFilter::builder().parse(filter).ok()?;
163161

164-
Some(Self {
165-
filter,
166-
hierarchical: false,
167-
})
168-
}
169-
170-
pub fn with_hierarchical(mut self, hierarchical: bool) -> Self {
171-
self.hierarchical = hierarchical;
172-
self
162+
Some(Self { filter })
173163
}
174164

175165
pub fn build(self) -> LoggingGuard {
176166
let registry = tracing_subscriber::registry().with(self.filter);
177167

178-
let guard = if self.hierarchical {
179-
let subscriber = registry.with(
180-
tracing_tree::HierarchicalLayer::default()
181-
.with_indent_lines(true)
182-
.with_indent_amount(2)
183-
.with_bracketed_fields(true)
184-
.with_thread_ids(true)
185-
.with_targets(true)
186-
.with_writer(std::io::stderr)
187-
.with_timer(tracing_tree::time::Uptime::default()),
188-
);
189-
190-
tracing::subscriber::set_default(subscriber)
191-
} else {
192-
let subscriber = registry.with(
193-
tracing_subscriber::fmt::layer()
194-
.compact()
195-
.with_writer(std::io::stderr)
196-
.with_timer(tracing_subscriber::fmt::time()),
197-
);
168+
let subscriber = registry.with(
169+
tracing_subscriber::fmt::layer()
170+
.compact()
171+
.with_writer(std::io::stderr)
172+
.with_timer(tracing_subscriber::fmt::time()),
173+
);
198174

199-
tracing::subscriber::set_default(subscriber)
200-
};
175+
let guard = tracing::subscriber::set_default(subscriber);
201176

202177
LoggingGuard { _guard: guard }
203178
}

crates/ty/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ salsa = { workspace = true }
3535
tracing = { workspace = true, features = ["release_max_level_debug"] }
3636
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
3737
tracing-flame = { workspace = true }
38-
tracing-tree = { workspace = true }
3938
wild = { workspace = true }
4039

4140
[dev-dependencies]

crates/ty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {
6060

6161
let verbosity = args.verbosity.level();
6262
countme::enable(verbosity.is_trace());
63-
let _guard = setup_tracing(verbosity)?;
63+
let _guard = setup_tracing(verbosity, args.color.unwrap_or_default())?;
6464

6565
tracing::warn!(
6666
"ty is pre-release software and not ready for production use. \

crates/ty/src/logging.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Sets up logging for ty
22
3+
use crate::args::TerminalColor;
34
use anyhow::Context;
45
use colored::Colorize;
56
use std::fmt;
67
use std::fs::File;
7-
use std::io::BufWriter;
8+
use std::io::{BufWriter, IsTerminal};
89
use tracing::{Event, Subscriber};
910
use tracing_subscriber::filter::LevelFilter;
1011
use tracing_subscriber::fmt::format::Writer;
@@ -76,7 +77,10 @@ impl VerbosityLevel {
7677
}
7778
}
7879

79-
pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result<TracingGuard> {
80+
pub(crate) fn setup_tracing(
81+
level: VerbosityLevel,
82+
color: TerminalColor,
83+
) -> anyhow::Result<TracingGuard> {
8084
use tracing_subscriber::prelude::*;
8185

8286
// The `TY_LOG` environment variable overrides the default log level.
@@ -115,16 +119,21 @@ pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result<TracingGuar
115119
.with(filter)
116120
.with(profiling_layer);
117121

122+
let ansi = match color {
123+
TerminalColor::Auto => {
124+
colored::control::SHOULD_COLORIZE.should_colorize() && std::io::stderr().is_terminal()
125+
}
126+
TerminalColor::Always => true,
127+
TerminalColor::Never => false,
128+
};
129+
118130
if level.is_trace() {
119131
let subscriber = registry.with(
120-
tracing_tree::HierarchicalLayer::default()
121-
.with_indent_lines(true)
122-
.with_indent_amount(2)
123-
.with_bracketed_fields(true)
132+
tracing_subscriber::fmt::layer()
133+
.event_format(tracing_subscriber::fmt::format().pretty())
124134
.with_thread_ids(true)
125-
.with_targets(true)
126-
.with_writer(std::io::stderr)
127-
.with_timer(tracing_tree::time::Uptime::default()),
135+
.with_ansi(ansi)
136+
.with_writer(std::io::stderr),
128137
);
129138

130139
subscriber.init();
@@ -136,6 +145,7 @@ pub(crate) fn setup_tracing(level: VerbosityLevel) -> anyhow::Result<TracingGuar
136145
display_timestamp: level.is_extra_verbose(),
137146
show_spans: false,
138147
})
148+
.with_ansi(ansi)
139149
.with_writer(std::io::stderr),
140150
);
141151

0 commit comments

Comments
 (0)