Skip to content

Commit

Permalink
logging for wasm and native
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 12, 2024
1 parent 442c1f6 commit dfd4d38
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
34 changes: 31 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ 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"
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"] }
Expand All @@ -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]
Expand Down
52 changes: 39 additions & 13 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit dfd4d38

Please sign in to comment.