-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Expand file tree
/
Copy pathlib.rs
More file actions
288 lines (260 loc) · 10.1 KB
/
lib.rs
File metadata and controls
288 lines (260 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! This crate allows tools to enable rust logging without having to magically
//! match rustc's tracing crate version.
//!
//! For example if someone is working on rustc_ast and wants to write some
//! minimal code against it to run in a debugger, with access to the `debug!`
//! logs emitted by rustc_ast, that can be done by writing:
//!
//! ```toml
//! [dependencies]
//! rustc_ast = { path = "../rust/compiler/rustc_ast" }
//! rustc_log = { path = "../rust/compiler/rustc_log" }
//! ```
//!
//! ```
//! fn main() {
//! rustc_log::init_logger(rustc_log::LoggerConfig::from_env("LOG")).unwrap();
//! /* ... */
//! }
//! ```
//!
//! Now `LOG=debug cargo +nightly run` will run your minimal main.rs and show
//! rustc's debug logging. In a workflow like this, one might also add
//! `std::env::set_var("LOG", "debug")` to the top of main so that `cargo
//! +nightly run` by itself is sufficient to get logs.
//!
//! The reason rustc_log is a tiny separate crate, as opposed to exposing the
//! same things in rustc_driver only, is to enable the above workflow. If you
//! had to depend on rustc_driver in order to turn on rustc's debug logs, that's
//! an enormously bigger dependency tree; every change you make to rustc_ast (or
//! whichever piece of the compiler you are interested in) would involve
//! rebuilding all the rest of rustc up to rustc_driver in order to run your
//! main.rs. Whereas by depending only on rustc_log and the few crates you are
//! debugging, you can make changes inside those crates and quickly run main.rs
//! to read the debug logs.
use std::env::{self, VarError};
use std::fmt::{self, Display};
use std::fs::File;
use std::io::{self, IsTerminal};
use std::sync::Mutex;
use tracing::dispatcher::SetGlobalDefaultError;
use tracing::{Event, Subscriber};
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
use tracing_subscriber::fmt::FmtContext;
use tracing_subscriber::fmt::format::{self, FmtSpan, FormatEvent, FormatFields};
use tracing_subscriber::fmt::writer::BoxMakeWriter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{Layer, Registry};
// Re-export tracing
pub use {tracing, tracing_core, tracing_subscriber};
/// The values of all the environment variables that matter for configuring a logger.
/// Errors are explicitly preserved so that we can share error handling.
pub struct LoggerConfig {
pub filter: Result<String, VarError>,
pub color_logs: Result<String, VarError>,
pub verbose_entry_exit: Result<String, VarError>,
pub verbose_thread_ids: Result<String, VarError>,
pub backtrace: Result<String, VarError>,
pub json: Result<String, VarError>,
pub output_target: Result<String, VarError>,
pub wraptree: Result<String, VarError>,
pub lines: Result<String, VarError>,
}
impl LoggerConfig {
pub fn from_env(env: &str) -> Self {
// NOTE: documented in the dev guide. If you change this, also update it!
LoggerConfig {
filter: env::var(env),
color_logs: env::var(format!("{env}_COLOR")),
verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
backtrace: env::var(format!("{env}_BACKTRACE")),
wraptree: env::var(format!("{env}_WRAPTREE")),
lines: env::var(format!("{env}_LINES")),
json: env::var(format!("{env}_FORMAT_JSON")),
output_target: env::var(format!("{env}_OUTPUT_TARGET")),
}
}
}
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
init_logger_with_additional_layer(cfg, Registry::default)
}
/// Trait alias for the complex return type of `build_subscriber` in
/// [init_logger_with_additional_layer]. A [Registry] with any composition of [tracing::Subscriber]s
/// (e.g. `Registry::default().with(custom_layer)`) should be compatible with this type.
/// Having an alias is also useful so rustc_driver_impl does not need to explicitly depend on
/// `tracing_subscriber`.
pub trait BuildSubscriberRet:
tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync
{
}
impl<
T: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span> + Send + Sync,
> BuildSubscriberRet for T
{
}
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
/// Additionally add a custom layer to collect logging and tracing events via `build_subscriber`,
/// for example: `|| Registry::default().with(custom_layer)`.
pub fn init_logger_with_additional_layer<F, T>(
cfg: LoggerConfig,
build_subscriber: F,
) -> Result<(), Error>
where
F: FnOnce() -> T,
T: BuildSubscriberRet,
{
let filter = match cfg.filter {
Ok(env) => EnvFilter::new(env),
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
};
let color_logs = match cfg.color_logs {
Ok(value) => match value.as_ref() {
"always" => true,
"never" => false,
"auto" => stderr_isatty(),
_ => return Err(Error::InvalidColorValue(value)),
},
Err(VarError::NotPresent) => stderr_isatty(),
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
};
let verbose_entry_exit = match cfg.verbose_entry_exit {
Ok(v) => &v != "0",
Err(_) => false,
};
let verbose_thread_ids = match cfg.verbose_thread_ids {
Ok(v) => &v == "1",
Err(_) => false,
};
let lines = match cfg.lines {
Ok(v) => &v == "1",
Err(_) => false,
};
let json = match cfg.json {
Ok(v) => &v == "1",
Err(_) => false,
};
let output_target: BoxMakeWriter = match cfg.output_target {
Ok(v) => match File::options().create(true).write(true).truncate(true).open(&v) {
Ok(i) => BoxMakeWriter::new(Mutex::new(i)),
Err(e) => {
eprintln!("couldn't open {v} as a log target: {e:?}");
BoxMakeWriter::new(io::stderr)
}
},
Err(_) => BoxMakeWriter::new(io::stderr),
};
let layer = if json {
let format = tracing_subscriber::fmt::format()
.json()
.with_span_list(true)
.with_source_location(true);
let fmt_layer = tracing_subscriber::fmt::layer()
.json()
.event_format(format)
.with_writer(output_target)
.with_target(true)
.with_ansi(false)
.with_thread_ids(verbose_thread_ids)
.with_thread_names(verbose_thread_ids)
.with_span_events(FmtSpan::ACTIVE);
Layer::boxed(fmt_layer)
} else {
let mut layer = tracing_tree::HierarchicalLayer::default()
.with_writer(output_target)
.with_ansi(color_logs)
.with_targets(true)
.with_verbose_exit(verbose_entry_exit)
.with_verbose_entry(verbose_entry_exit)
.with_indent_amount(2)
.with_indent_lines(lines)
.with_thread_ids(verbose_thread_ids)
.with_thread_names(verbose_thread_ids);
if let Ok(v) = cfg.wraptree {
match v.parse::<usize>() {
Ok(v) => layer = layer.with_wraparound(v),
Err(_) => return Err(Error::InvalidWraptree(v)),
}
}
Layer::boxed(layer)
};
let subscriber = build_subscriber();
// NOTE: It is important to make sure that the filter is applied on the last layer
match cfg.backtrace {
Ok(backtrace_target) => {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_writer(io::stderr)
.without_time()
.event_format(BacktraceFormatter { backtrace_target });
let subscriber = subscriber.with(layer).with(fmt_layer).with(filter);
tracing::subscriber::set_global_default(subscriber)?;
}
Err(_) => {
tracing::subscriber::set_global_default(subscriber.with(layer).with(filter))?;
}
};
Ok(())
}
struct BacktraceFormatter {
backtrace_target: String,
}
impl<S, N> FormatEvent<S, N> for BacktraceFormatter
where
S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
_ctx: &FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let target = event.metadata().target();
if !target.contains(&self.backtrace_target) {
return Ok(());
}
// Use Backtrace::force_capture because we don't want to depend on the
// RUST_BACKTRACE environment variable being set.
let backtrace = std::backtrace::Backtrace::force_capture();
writeln!(writer, "stack backtrace: \n{backtrace:?}")
}
}
pub fn stdout_isatty() -> bool {
io::stdout().is_terminal()
}
pub fn stderr_isatty() -> bool {
io::stderr().is_terminal()
}
#[derive(Debug)]
pub enum Error {
InvalidColorValue(String),
NonUnicodeColorValue,
InvalidWraptree(String),
AlreadyInit(SetGlobalDefaultError),
}
impl std::error::Error for Error {}
impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidColorValue(value) => write!(
formatter,
"invalid log color value '{value}': expected one of always, never, or auto",
),
Error::NonUnicodeColorValue => write!(
formatter,
"non-Unicode log color value: expected one of always, never, or auto",
),
Error::InvalidWraptree(value) => write!(
formatter,
"invalid log WRAPTREE value '{value}': expected a non-negative integer",
),
Error::AlreadyInit(tracing_error) => Display::fmt(tracing_error, formatter),
}
}
}
impl From<SetGlobalDefaultError> for Error {
fn from(tracing_error: SetGlobalDefaultError) -> Self {
Error::AlreadyInit(tracing_error)
}
}