diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-02-12 17:51:26 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-02-20 08:20:02 -0800 |
commit | b4bd00fdad094b16c785b8ead9f92d68264f5fb4 (patch) | |
tree | 540ad033c789aa08a1ffed17e9c161df331600d2 /sys_util/src/signalfd.rs | |
parent | 0373b9f154b5ec1894138b7c10ad495fcce7b64f (diff) | |
download | crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.gz crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.bz2 crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.lz crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.xz crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.zst crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.zip |
error: Print errors using Display impl
I have been running into Debug-printed error messages too often and needing to look up in the source code each level of nested errors to find out from the comment on the error variant what the short name of the variant means in human terms. Worse, many errors (like the one shown below) already had error strings written but were being printed from the calling code in the less helpful Debug representation anyway. Before: [ERROR:src/main.rs:705] The architecture failed to build the vm: NoVarEmpty After: [ERROR:src/main.rs:705] The architecture failed to build the vm: /var/empty doesn't exist, can't jail devices. TEST=cargo check --all-features TEST=FEATURES=test emerge-amd64-generic crosvm Change-Id: I77122c7d6861b2d610de2fff718896918ab21e10 Reviewed-on: https://chromium-review.googlesource.com/1469225 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'sys_util/src/signalfd.rs')
-rw-r--r-- | sys_util/src/signalfd.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/sys_util/src/signalfd.rs b/sys_util/src/signalfd.rs index c4fd92d..479972e 100644 --- a/sys_util/src/signalfd.rs +++ b/sys_util/src/signalfd.rs @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use std::fmt::{self, Display}; use std::fs::File; use std::mem; use std::os::raw::c_int; @@ -29,6 +30,32 @@ pub enum Error { SignalFdPartialRead(usize), } +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + + match self { + CreateSigset(e) => write!( + f, + "failed to construct sigset when creating signalfd: {}", + e, + ), + CreateSignalFd(e) => write!(f, "failed to create a new signalfd: {}", e), + CreateBlockSignal(e) => write!( + f, + "failed to block the signal when creating signalfd: {}", + e, + ), + SignalFdRead(e) => write!(f, "unable to read from signalfd: {}", e), + SignalFdPartialRead(read) => write!( + f, + "signalfd failed to return a full siginfo struct, read only {} bytes", + read, + ), + } + } +} + pub type Result<T> = result::Result<T, Error>; /// A safe wrapper around a Linux signalfd (man 2 signalfd). @@ -114,7 +141,7 @@ impl Drop for SignalFd { // was promised - unmasking the signal when we go out of scope. let res = signal::unblock_signal(self.signal); if let Err(e) = res { - error!("signalfd failed to unblock signal {}: {:?}", self.signal, e); + error!("signalfd failed to unblock signal {}: {}", self.signal, e); } } } |