Enable optional logging support

This commit is contained in:
Diggory Hardy
2019-03-12 17:39:20 +00:00
parent 13d49ec09a
commit d33356f55f
5 changed files with 16 additions and 4 deletions
+3
View File
@@ -14,6 +14,9 @@ members = [
"tests/wasm_bindgen",
]
[dependencies]
log = { version = "0.4", optional = true }
[target.'cfg(unix)'.dependencies]
libc = "0.2.27"
+3
View File
@@ -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:
+2 -2
View File
@@ -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),
}
}
}
+4
View File
@@ -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",
+4 -2
View File
@@ -66,7 +66,8 @@ fn getrandom_init() -> Result<RngSource, Error> {
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(())