show env_logger working in wiggle tracing example

This commit is contained in:
Pat Hickey
2020-06-02 17:46:31 -07:00
parent 33a94ca7d5
commit 1b95b24686
3 changed files with 52 additions and 44 deletions

2
Cargo.lock generated
View File

@@ -2113,6 +2113,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7c6b59d116d218cb2d990eb06b77b64043e0268ef7323aae63d8b30ae462923"
dependencies = [
"cfg-if",
"log",
"tracing-attributes",
"tracing-core",
]
@@ -2656,6 +2657,7 @@ dependencies = [
name = "wiggle-test"
version = "0.17.0"
dependencies = [
"env_logger 0.7.1",
"proptest",
"thiserror",
"tracing",

View File

@@ -12,12 +12,13 @@ include = ["src/**/*", "LICENSE"]
[dependencies]
proptest = "0.9"
wiggle = { path = ".." }
wiggle = { path = "..", features = ["tracing_log"] }
[dev-dependencies]
thiserror = "1.0"
tracing = "0.1.14"
tracing-subscriber = "0.2.4"
env_logger="0.7"
[badges]

View File

@@ -68,51 +68,56 @@ impl<'a> one_error_conversion::OneErrorConversion for WasiCtx<'a> {
}
fn main() {
let subscriber = tracing_subscriber::fmt()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(tracing::Level::TRACE)
// builds the subscriber.
.finish();
if std::env::var("RUST_LOG").is_err() {
// with no RUST_LOG env variable: use the tracing subscriber.
let subscriber = tracing_subscriber::fmt()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(tracing::Level::TRACE)
// builds the subscriber.
.finish();
tracing::subscriber::set_global_default(subscriber).expect("set global tracing subscriber");
} else {
// with RUST_LOG set: use the env_logger backend to tracing.
env_logger::init();
}
tracing::subscriber::with_default(subscriber, || {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
// Exercise each of the branches in `foo`.
// Start with the success case:
let r0 = one_error_conversion::foo(&ctx, &host_memory, 0, 0, 8);
assert_eq!(
r0,
i32::from(types::Errno::Ok),
"Expected return value for strike=0"
);
assert!(ctx.log.borrow().is_empty(), "No error log for strike=0");
// Exercise each of the branches in `foo`.
// Start with the success case:
let r0 = one_error_conversion::foo(&ctx, &host_memory, 0, 0, 8);
assert_eq!(
r0,
i32::from(types::Errno::Ok),
"Expected return value for strike=0"
);
assert!(ctx.log.borrow().is_empty(), "No error log for strike=0");
// First error case:
let r1 = one_error_conversion::foo(&ctx, &host_memory, 1, 0, 8);
assert_eq!(
r1,
i32::from(types::Errno::PicketLine),
"Expected return value for strike=1"
);
assert_eq!(
ctx.log.borrow_mut().pop().expect("one log entry"),
"Won't cross picket line: I'm not a scab",
"Expected log entry for strike=1",
);
// First error case:
let r1 = one_error_conversion::foo(&ctx, &host_memory, 1, 0, 8);
assert_eq!(
r1,
i32::from(types::Errno::PicketLine),
"Expected return value for strike=1"
);
assert_eq!(
ctx.log.borrow_mut().pop().expect("one log entry"),
"Won't cross picket line: I'm not a scab",
"Expected log entry for strike=1",
);
// Second error case:
let r2 = one_error_conversion::foo(&ctx, &host_memory, 2, 0, 8);
assert_eq!(
r2,
i32::from(types::Errno::InvalidArg),
"Expected return value for strike=2"
);
assert_eq!(
ctx.log.borrow_mut().pop().expect("one log entry"),
"Invalid argument: out-of-bounds: 2",
"Expected log entry for strike=2",
);
});
// Second error case:
let r2 = one_error_conversion::foo(&ctx, &host_memory, 2, 0, 8);
assert_eq!(
r2,
i32::from(types::Errno::InvalidArg),
"Expected return value for strike=2"
);
assert_eq!(
ctx.log.borrow_mut().pop().expect("one log entry"),
"Invalid argument: out-of-bounds: 2",
"Expected log entry for strike=2",
);
}