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:
parent
ddb9a55817
commit
57a455e5f2
|
@ -47,7 +47,7 @@ impl<T: Send + io::Write> log::Log for Logger<T> {
|
||||||
fn log(&self, record: &log::Record) {
|
fn log(&self, record: &log::Record) {
|
||||||
if self.enabled(record.metadata()) && record.target().starts_with("alacritty") {
|
if self.enabled(record.metadata()) && record.target().starts_with("alacritty") {
|
||||||
if let Ok(ref mut writer) = self.output.lock() {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)*);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue