diff options
author | Christian Duerr <chrisduerr@users.noreply.github.com> | 2018-07-25 21:05:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-25 21:05:49 +0000 |
commit | 57a455e5f209dd965fd6b495d7f2b033fd5288c0 (patch) | |
tree | 911d2000d0d5df72f4d0a0b7feb04aa9aa466850 | |
parent | ddb9a558170ad16f19135b2f6a5a7a7e8ac61c3c (diff) | |
download | alacritty-57a455e5f209dd965fd6b495d7f2b033fd5288c0.tar.gz alacritty-57a455e5f209dd965fd6b495d7f2b033fd5288c0.zip |
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.
-rw-r--r-- | src/logging.rs | 2 | ||||
-rw-r--r-- | src/macros.rs | 16 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/logging.rs b/src/logging.rs index a691778a..10929980 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -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()); } } } diff --git a/src/macros.rs b/src/macros.rs index 35e69f2d..464110e6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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)*); + }}; +} |