Skip to content

Commit 467ae91

Browse files
committed
Update logging section and explain RUSTC_LOG_COLOR
1 parent e7da4fd commit 467ae91

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

src/compiler-debugging.md

+54-25
Original file line numberDiff line numberDiff line change
@@ -150,65 +150,94 @@ Cool, now I have a backtrace for the error!
150150
## Getting logging output
151151
[getting-logging-output]: #getting-logging-output
152152

153-
These crates are used in compiler for logging:
153+
The compiler uses the [`tracing`] crate for logging.
154154

155-
* [log]
156-
* [env-logger]
155+
[`tracing`]: https://docs.rs/tracing
157156

158-
[log]: https://docs.rs/log/0.4.6/log/index.html
159-
[env-logger]: https://docs.rs/env_logger
160-
161-
The compiler has a lot of `debug!` calls, which print out logging information
157+
The compiler has a lot of [`debug!`] calls, which print out logging information
162158
at many points. These are very useful to at least narrow down the location of
163159
a bug if not to find it entirely, or just to orient yourself as to why the
164160
compiler is doing a particular thing.
165161

166-
To see the logs, you need to set the `RUSTC_LOG` environment variable to
167-
your log filter, e.g. to get the logs for a specific module, you can run the
168-
compiler as `RUSTC_LOG=module::path rustc my-file.rs`. All `debug!` output will
169-
then appear in standard error.
162+
[`debug!`]: https://docs.rs/tracing/0.1/tracing/macro.debug.html
163+
164+
To see the logs, you need to set the `RUSTC_LOG` environment variable to your
165+
log filter. Your log filter can be just `debug` to get all `debug!` output, or
166+
`path::to::module` to get *all* output (not just `debug!`) from a particular
167+
module, or `path::to::module=debug` to get only `debug!` output form a
168+
particular module.
169+
170+
For example, to get the `debug!` output for a specific module, you can run the
171+
compiler with `RUSTC_LOG=path::to::module=debug rustc my-file.rs`. All `debug!`
172+
output will then appear in standard error.
170173

171-
If you are developing rustdoc, use `RUSTDOC_LOG` instead.
174+
If you are developing rustdoc, use `RUSTDOC_LOG` instead. If you are developing
175+
Miri, use `MIRI_LOG` instead. You get the idea :)
172176

173-
See the [env-logger] doc for more info on the full syntax. (Note: unlike the
174-
compiler, the env-logger crate and its examples use the `RUST_LOG` env
175-
variable.)
177+
See the [`tracing`] crate's docs, and specifically the docs for [`debug!`] to
178+
see the full syntax you can use. See the [env-logger] doc for more info on the
179+
full syntax. (Note: unlike the compiler, the [`tracing`] crate and its examples
180+
use the `RUST_LOG` environment variable. rustc, rustdoc, and other tools set
181+
custom environment variables.)
176182
177183
**Note that unless you use a very strict filter, the logger will emit a lot of
178184
output, so use the most specific module(s) you can (comma-separated if
179185
multiple)**. It's typically a good idea to pipe standard error to a file and
180186
look at the log output with a text editor.
181187

182-
So to put it together.
188+
So, to put it together:
183189

184190
```bash
185191
# This puts the output of all debug calls in `rustc_middle/src/traits` into
186192
# standard error, which might fill your console backscroll.
187-
$ RUSTC_LOG=rustc_middle::traits rustc +stage1 my-file.rs
193+
$ RUSTC_LOG=rustc_middle::traits=debug rustc +stage1 my-file.rs
188194
189195
# This puts the output of all debug calls in `rustc_middle/src/traits` in
190196
# `traits-log`, so you can then see it with a text editor.
191-
$ RUSTC_LOG=rustc_middle::traits rustc +stage1 my-file.rs 2>traits-log
197+
$ RUSTC_LOG=rustc_middle::traits=debug rustc +stage1 my-file.rs 2>traits-log
192198
193-
# Not recommended. This will show the output of all `debug!` calls
199+
# Not recommended! This will show the output of all `debug!` calls
194200
# in the Rust compiler, and there are a *lot* of them, so it will be
195201
# hard to find anything.
196202
$ RUSTC_LOG=debug rustc +stage1 my-file.rs 2>all-log
197203
198-
# This will show the output of all `info!` calls in `rustc_trans`.
204+
# This will show the output of all `info!` calls in `rustc_codegen_ssa`.
199205
#
200-
# There's an `info!` statement in `trans_instance` that outputs
206+
# There's an `info!` statement in `codegen_instance` that outputs
201207
# every function that is translated. This is useful to find out
202208
# which function triggers an LLVM assertion, and this is an `info!`
203209
# log rather than a `debug!` log so it will work on the official
204210
# compilers.
205-
$ RUSTC_LOG=rustc_trans=info rustc +stage1 my-file.rs
211+
$ RUSTC_LOG=rustc_codegen_ssa=info rustc +stage1 my-file.rs
206212
207-
# This will show the output of all `info!` calls made by rustdoc or any rustc library it calls.
213+
# This will show the output of all `info!` calls made by rustdoc
214+
# or any rustc library it calls.
208215
$ RUSTDOC_LOG=info rustdoc +stage1 my-file.rs
209216
210-
# This will only show `debug!` calls made by rustdoc directly, not any `rustc*` crate.
211-
$ RUSTDOC_LOG=rustdoc rustdoc +stage1 my-file.rs
217+
# This will only show `debug!` calls made by rustdoc directly,
218+
# not any `rustc*` crate.
219+
$ RUSTDOC_LOG=rustdoc=debug rustdoc +stage1 my-file.rs
220+
```
221+
222+
### Log colors
223+
224+
By default, rustc (and other tools, like rustdoc and Miri) will be smart about
225+
when to use ANSI colors in the log output. If they are outputting to a terminal,
226+
they will use colors, and if they are outputting to a file or being piped
227+
somewhere else, they will not. However, it's hard to read log output in your
228+
terminal unless you have a very strict filter, so you may want to pipe the
229+
output to a pager like `less`. But then there won't be any colors, which makes
230+
it hard to pick out what you're looking for!
231+
232+
You can override whether to have colors in log output with the `RUSTC_LOG_COLOR`
233+
environment variable (or `RUSTDOC_LOG_COLOR` for rustdoc, or `MIRI_LOG_COLOR`
234+
for Miri, etc.). There are three options: `auto` (the default), `always`, and
235+
`never`. So, if you want to enable colors when piping to `less`, use something
236+
similar to this command:
237+
238+
```bash
239+
# The `-R` switch tells less to print ANSI colors without escaping them.
240+
$ RUSTC_LOG=debug RUSTC_LOG_COLOR=always rustc +stage1 ... | less -R
212241
```
213242
214243
### How to keep or remove `debug!` and `trace!` calls from the resulting binary

0 commit comments

Comments
 (0)