Ignore errors when logger can't write to output

The (e)print macro will panic when there is no output available to
write to, however in our scenario where we only log user errors to
stderr, the better choice would be to ignore when writing to stdout or
stderr is not possible.

This changes the (e)print macro to make use of `write` and ignore
any potential errors.

Since (e)println rely on (e)print, this also solves potential failuers
when calling (e)println.

With this change implemented, all of logging, (e)println and (e)print
should never fail even if the stdout/stderr is not available.
This commit is contained in:
Christian Duerr 2018-07-25 21:05:49 +00:00 committed by GitHub
parent ddb9a55817
commit 57a455e5f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -47,7 +47,7 @@ impl<T: Send + io::Write> log::Log for Logger<T> {
fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) && record.target().starts_with("alacritty") {
if let Ok(ref mut writer) = self.output.lock() {
writer.write_all(format!("{}\n", record.args()).as_ref()).expect("Error while logging!");
let _ = writer.write_all(format!("{}\n", record.args()).as_ref());
}
}
}

View File

@ -29,3 +29,19 @@ macro_rules! maybe {
}
}
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
use std::io::Write;
let _ = write!(::std::io::stdout(), $($arg)*);
}};
}
#[macro_export]
macro_rules! eprint {
($($arg:tt)*) => {{
use std::io::Write;
let _ = write!(::std::io::stderr(), $($arg)*);
}};
}