diff --git a/Cargo.toml b/Cargo.toml index 769318e..dda4c3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,9 @@ members = [ "tests/wasm_bindgen", ] +[dependencies] +log = { version = "0.4", optional = true } + [target.'cfg(unix)'.dependencies] libc = "0.2.27" diff --git a/README.md b/README.md index 4330a17..6d56630 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ fn get_random_buf() -> Result<[u8; 32], getrandom::Error> { This library is `no_std` compatible on SGX but requires `std` on most platforms. +The `log` library is supported as an optional dependency. If enabled, error +reporting will be improved on some platforms. + For WebAssembly (`wasm32`), Enscripten targets are supported directly; otherwise one of the following features must be enabled: diff --git a/src/error.rs b/src/error.rs index 862919b..9639478 100644 --- a/src/error.rs +++ b/src/error.rs @@ -65,7 +65,7 @@ impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.msg() { Some(msg) => write!(f, "Error(\"{}\")", msg), - None => write!(f, "Error({:08X})", self.0), + None => write!(f, "Error(0x{:08X})", self.0), } } } @@ -74,7 +74,7 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.msg() { Some(msg) => write!(f, "{}", msg), - None => write!(f, "getrandom: unknown code {:08X}", self.0), + None => write!(f, "getrandom: unknown code 0x{:08X}", self.0), } } } diff --git a/src/lib.rs b/src/lib.rs index 4655f90..f92e359 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,10 @@ extern crate wasm_bindgen; feature = "stdweb"))] #[macro_use] extern crate stdweb; +#[cfg(feature = "log")] #[macro_use] extern crate log; +#[allow(unused)] +#[cfg(not(feature = "log"))] macro_rules! warn { ($($x:tt)*) => () } + #[cfg(any( target_os = "android", target_os = "netbsd", diff --git a/src/wasm32_stdweb.rs b/src/wasm32_stdweb.rs index 2c21bc3..de9eea4 100644 --- a/src/wasm32_stdweb.rs +++ b/src/wasm32_stdweb.rs @@ -66,7 +66,8 @@ fn getrandom_init() -> Result { else { unreachable!() } } else { let err: WebError = js!{ return @{ result }.error }.try_into().unwrap(); - Err(ERROR_UNAVAILABLE) // TODO: forward err + warn!("getrandom unavailable: {}", err); + Err(ERROR_UNAVAILABLE) } } @@ -101,7 +102,8 @@ fn getrandom_fill(source: &mut RngSource, dest: &mut [u8]) -> Result<(), Error> if js!{ return @{ result.as_ref() }.success } != true { let err: WebError = js!{ return @{ result }.error }.try_into().unwrap(); - return Err(ERROR_UNKNOWN) // TODO: forward err + warn!("getrandom failed: {}", err); + return Err(ERROR_UNKNOWN) } } Ok(())