From dfd4d38bc6d965397b3f758962842375fb027f93 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Thu, 12 Sep 2024 14:44:45 -0400 Subject: [PATCH] logging for wasm and native --- Cargo.lock | 34 ++++++++++++++++++++++++++++++--- Cargo.toml | 22 +++++++++++++-------- src/init.rs | 52 +++++++++++++++++++++++++++++++++++++------------- tests/tests.rs | 1 + 4 files changed, 85 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba99b40..de8e16a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1058,6 +1058,15 @@ dependencies = [ "syn", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "maybe-rayon" version = "0.1.1" @@ -1557,8 +1566,17 @@ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -1569,9 +1587,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.4", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.4" @@ -2129,10 +2153,14 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", ] diff --git a/Cargo.toml b/Cargo.toml index ed33e5c..bdec4fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,12 @@ wasm-opt = ["-O4", "-O4"] [features] default = ["native"] -wasm = ["console_error_panic_hook"] -console_error_panic_hook = ["dep:console_error_panic_hook"] + +wasm = ["console_error_panic_hook", "dep:tracing-web"] native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"] +console_error_panic_hook = ["dep:console_error_panic_hook"] + [dependencies] ## Web dependencies wasm-bindgen = "0.2.84" @@ -32,6 +34,11 @@ js-sys = "0.3.69" wasm-bindgen-futures = "0.4.42" web-sys = { version = "0.3.69", features = ["console"] } +# WASM specific dependencies +tracing-web = { version = "0.1.3", optional = true } +console_error_panic_hook = { version = "0.1.7", optional = true } +tonic-web-wasm-client = "0.6.0" + ## Zcash dependencies zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", features = ["transparent-inputs", "orchard", "sapling", "unstable"] } zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["lightwalletd-tonic", "wasm-bindgen"] } @@ -45,23 +52,22 @@ prost = { version = "0.12", default-features = false } tonic = { version = "0.12", default-features = false, features = [ "prost", ] } -tonic-web-wasm-client = "0.6.0" + +# Used in Native tests +tokio = { version = "1.0", features = ["rt", "macros"], optional = true } getrandom = { version = "0.2", features = ["js"] } thiserror = "1.0.63" -console_error_panic_hook = { version = "0.1.7", optional = true } indexed_db_futures = "0.5.0" sha2 = "0.10" ripemd = "0.1" bip0039 = "0.12.0" secrecy = "0.8.0" futures-util = "0.3.30" -tracing-web = "0.1.3" -tracing-subscriber = "0.3.18" -tracing = "0.1.40" nonempty = "0.7" hex = "0.4.3" -tokio = { version = "1.0", features = ["rt", "macros"], optional = true } +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +tracing = "0.1.40" [dev-dependencies] diff --git a/src/init.rs b/src/init.rs index af49f13..ecb5429 100644 --- a/src/init.rs +++ b/src/init.rs @@ -3,9 +3,10 @@ use wasm_bindgen::prelude::*; -use tracing_subscriber::fmt::format::Pretty; +use tracing::level_filters::LevelFilter; + use tracing_subscriber::prelude::*; -use tracing_web::{performance_layer, MakeWebConsoleWriter}; +use tracing_subscriber::EnvFilter; fn set_panic_hook() { // When the `console_error_panic_hook` feature is enabled, we can call the @@ -17,18 +18,43 @@ fn set_panic_hook() { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); } - fn setup_tracing() { - let fmt_layer = tracing_subscriber::fmt::layer() - .with_ansi(false) // Only partially supported across browsers - .without_time() // std::time is not available in browsers - .with_writer(MakeWebConsoleWriter::new()); // write events to the console - let perf_layer = performance_layer().with_details_from_fields(Pretty::default()); - - tracing_subscriber::registry() - .with(fmt_layer) - .with(perf_layer) - .init(); + #[cfg(not(feature = "wasm"))] + let subscriber = { + let filter_layer = EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env() + .unwrap(); + let fmt_layer = tracing_subscriber::fmt::layer().with_ansi(true).pretty(); + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + }; + + #[cfg(feature = "wasm")] + let subscriber = { + use tracing_subscriber::fmt::format::Pretty; + use tracing_web::{performance_layer, MakeWebConsoleWriter}; + + // For WASM, we must set the directives here at compile time. + let filter_layer = EnvFilter::default() + .add_directive(LevelFilter::INFO.into()) + .add_directive("zcash_client_backend=debug".parse().unwrap()); + + let fmt_layer = tracing_subscriber::fmt::layer() + .with_ansi(false) // Only partially supported across browsers + .without_time() // std::time is not available in browsers + .with_writer(MakeWebConsoleWriter::new()); // write events to the console + + let perf_layer = performance_layer().with_details_from_fields(Pretty::default()); + + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + .with(perf_layer) + }; + + subscriber.init(); } #[wasm_bindgen(start)] diff --git a/tests/tests.rs b/tests/tests.rs index 45dbaf4..300bb33 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -50,6 +50,7 @@ async fn test_get_and_scan_range() { #[cfg(feature = "native")] #[tokio::test] async fn test_get_and_scan_range_native() { + initialize(); let url = "https://testnet.zec.rocks:443"; let c = tonic::transport::Channel::from_shared(url).unwrap();