Skip to content

Commit cc98f98

Browse files
kolbmaSergioBenitez
authored andcommitted
Implement and log with panic-free 'write_out!'.
Resolves #2019.
1 parent 2cbaf05 commit cc98f98

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

core/lib/src/log.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ define_log_macro!(trace, trace_);
3535
define_log_macro!(launch_info: info, "rocket::launch", $);
3636
define_log_macro!(launch_info_: info, "rocket::launch_", $);
3737

38+
// `print!` panics when stdout isn't available, but this macro doesn't.
39+
// See SergioBenitez/Rocket#2019 and rust-lang/rust#46016 for more.
40+
macro_rules! write_out {
41+
($($arg:tt)*) => ({
42+
use std::io::{Write, stdout, stderr};
43+
44+
let _ = write!(stdout(), $($arg)*).or_else(|e| write!(stderr(), "{}", e));
45+
})
46+
}
47+
3848
#[derive(Debug)]
3949
struct RocketLogger;
4050

@@ -86,7 +96,7 @@ impl log::Log for RocketLogger {
8696
// In Rocket, we abuse targets with suffix "_" to indicate indentation.
8797
let indented = record.target().ends_with('_');
8898
if indented {
89-
print!(" {} ", Paint::default(">>").bold());
99+
write_out!(" {} ", Paint::default(">>").bold());
90100
}
91101

92102
// Downgrade a physical launch `warn` to logical `info`.
@@ -96,30 +106,30 @@ impl log::Log for RocketLogger {
96106

97107
match level {
98108
log::Level::Error if !indented => {
99-
println!("{} {}",
100-
Paint::red("Error:").bold(),
101-
Paint::red(record.args()).wrap())
109+
write_out!("{} {}\n",
110+
Paint::red("Error:").bold(),
111+
Paint::red(record.args()).wrap());
102112
}
103113
log::Level::Warn if !indented => {
104-
println!("{} {}",
105-
Paint::yellow("Warning:").bold(),
106-
Paint::yellow(record.args()).wrap())
114+
write_out!("{} {}\n",
115+
Paint::yellow("Warning:").bold(),
116+
Paint::yellow(record.args()).wrap());
107117
}
108-
log::Level::Info => println!("{}", Paint::blue(record.args()).wrap()),
109-
log::Level::Trace => println!("{}", Paint::magenta(record.args()).wrap()),
110-
log::Level::Warn => println!("{}", Paint::yellow(record.args()).wrap()),
111-
log::Level::Error => println!("{}", Paint::red(record.args()).wrap()),
118+
log::Level::Info => write_out!("{}\n", Paint::blue(record.args()).wrap()),
119+
log::Level::Trace => write_out!("{}\n", Paint::magenta(record.args()).wrap()),
120+
log::Level::Warn => write_out!("{}\n", Paint::yellow(record.args()).wrap()),
121+
log::Level::Error => write_out!("{}\n", Paint::red(record.args()).wrap()),
112122
log::Level::Debug => {
113-
print!("\n{} ", Paint::blue("-->").bold());
123+
write_out!("\n{} ", Paint::blue("-->").bold());
114124
if let Some(file) = record.file() {
115-
print!("{}", Paint::blue(file));
125+
write_out!("{}", Paint::blue(file));
116126
}
117127

118128
if let Some(line) = record.line() {
119-
println!(":{}", Paint::blue(line));
129+
write_out!(":{}\n", Paint::blue(line));
120130
}
121131

122-
println!("\t{}", record.args());
132+
write_out!("\t{}\n", record.args());
123133
}
124134
}
125135
}

0 commit comments

Comments
 (0)