Skip to content

Commit cce7ac8

Browse files
committed
Allow process_wrapper to output diagnostics to a file.
1 parent bb22201 commit cce7ac8

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

util/process_wrapper/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,35 @@ fn main() {
8989

9090
let mut child_stderr = child.stderr.take().unwrap();
9191

92+
let mut output_file: Box<Option<std::fs::File>> =
93+
Box::new(opts.output_file.clone().map(|output_file_name| {
94+
OpenOptions::new()
95+
.create(true)
96+
.truncate(true)
97+
.write(true)
98+
.open(output_file_name)
99+
.expect("process wrapper error: unable to open output_file")
100+
}));
101+
92102
let mut was_killed = false;
93103
let result = if let Some(format) = opts.rustc_output_format {
94104
let quit_on_rmeta = opts.rustc_quit_on_rmeta;
95105
// Process json rustc output and kill the subprocess when we get a signal
96106
// that we emitted a metadata file.
97107
let mut me = false;
98108
let metadata_emitted = &mut me;
99-
let result = process_output(&mut child_stderr, stderr.as_mut(), move |line| {
100-
if quit_on_rmeta {
101-
rustc::stop_on_rmeta_completion(line, format, metadata_emitted)
102-
} else {
103-
rustc::process_json(line, format)
104-
}
105-
});
109+
let result = process_output(
110+
&mut child_stderr,
111+
stderr.as_mut(),
112+
output_file.as_mut(),
113+
move |line| {
114+
if quit_on_rmeta {
115+
rustc::stop_on_rmeta_completion(line, format, metadata_emitted)
116+
} else {
117+
rustc::process_json(line, format)
118+
}
119+
},
120+
);
106121
if me {
107122
// If recv returns Ok(), a signal was sent in this channel so we should terminate the child process.
108123
// We can safely ignore the Result from kill() as we don't care if the process already terminated.
@@ -112,7 +127,12 @@ fn main() {
112127
result
113128
} else {
114129
// Process output normally by forwarding stderr
115-
process_output(&mut child_stderr, stderr.as_mut(), LineOutput::Message)
130+
process_output(
131+
&mut child_stderr,
132+
stderr.as_mut(),
133+
output_file.as_mut(),
134+
LineOutput::Message,
135+
)
116136
};
117137
result.expect("process wrapper error: failed to process stderr");
118138

util/process_wrapper/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub(crate) struct Options {
4141
pub(crate) stdout_file: Option<String>,
4242
// If set, redirects the child process stderr to this file.
4343
pub(crate) stderr_file: Option<String>,
44+
// If set, also logs all unprocessed output from the rustc output to this file.
45+
// Meant to be used to get json output out of rustc for tooling usage.
46+
pub(crate) output_file: Option<String>,
4447
// If set, it configures rustc to emit an rmeta file and then
4548
// quit.
4649
pub(crate) rustc_quit_on_rmeta: bool,
@@ -60,6 +63,7 @@ pub(crate) fn options() -> Result<Options, OptionError> {
6063
let mut copy_output_raw = None;
6164
let mut stdout_file = None;
6265
let mut stderr_file = None;
66+
let mut output_file = None;
6367
let mut rustc_quit_on_rmeta_raw = None;
6468
let mut rustc_output_format_raw = None;
6569
let mut flags = Flags::new();
@@ -92,6 +96,11 @@ pub(crate) fn options() -> Result<Options, OptionError> {
9296
"Redirect subprocess stderr in this file.",
9397
&mut stderr_file,
9498
);
99+
flags.define_flag(
100+
"--output-file",
101+
"Log all unprocessed subprocess stderr in this file.",
102+
&mut output_file,
103+
);
95104
flags.define_flag(
96105
"--rustc-quit-on-rmeta",
97106
"If enabled, this wrapper will terminate rustc after rmeta has been emitted.",
@@ -200,6 +209,7 @@ pub(crate) fn options() -> Result<Options, OptionError> {
200209
copy_output,
201210
stdout_file,
202211
stderr_file,
212+
output_file,
203213
rustc_quit_on_rmeta,
204214
rustc_output_format,
205215
})

util/process_wrapper/output.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ pub(crate) enum LineOutput {
3232
/// to write_end.
3333
pub(crate) fn process_output<F>(
3434
read_end: &mut dyn Read,
35-
write_end: &mut dyn Write,
35+
output_write_end: &mut dyn Write,
36+
opt_file_write_end: &mut Option<std::fs::File>,
3637
mut process_line: F,
3738
) -> io::Result<()>
3839
where
3940
F: FnMut(String) -> LineOutput,
4041
{
4142
let mut reader = io::BufReader::new(read_end);
42-
let mut writer = io::LineWriter::new(write_end);
43+
let mut output_writer = io::LineWriter::new(output_write_end);
44+
let mut file_writer = opt_file_write_end.as_mut().map(io::LineWriter::new);
4345
loop {
4446
let mut line = String::new();
4547
let read_bytes = reader.read_line(&mut line)?;
4648
if read_bytes == 0 {
4749
break;
4850
}
51+
if let Some(ref mut file) = file_writer {
52+
file.write_all(line.as_bytes())?
53+
}
4954
match process_line(line) {
50-
LineOutput::Message(to_write) => writer.write_all(to_write.as_bytes())?,
55+
LineOutput::Message(to_write) => output_writer.write_all(to_write.as_bytes())?,
5156
LineOutput::Skip => {}
5257
LineOutput::Terminate => return Ok(()),
5358
};

util/process_wrapper/rustc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ pub(crate) fn process_json(line: String, error_format: ErrorFormat) -> LineOutpu
7373
)
7474
});
7575
match parsed.try_into() {
76-
Ok(RustcMessage::Message(rendered)) => output_based_on_error_format(line, rendered, error_format),
76+
Ok(RustcMessage::Message(rendered)) => {
77+
output_based_on_error_format(line, rendered, error_format)
78+
}
7779
_ => LineOutput::Skip,
7880
}
7981
}
@@ -100,7 +102,9 @@ pub(crate) fn stop_on_rmeta_completion(
100102
*kill = true;
101103
LineOutput::Terminate
102104
}
103-
Ok(RustcMessage::Message(rendered)) => output_based_on_error_format(line, rendered, error_format),
105+
Ok(RustcMessage::Message(rendered)) => {
106+
output_based_on_error_format(line, rendered, error_format)
107+
}
104108
_ => LineOutput::Skip,
105109
}
106110
}

0 commit comments

Comments
 (0)