From 55d8ffcfedeec9eccb566c0571aa7e520585cfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Zwoli=C5=84ski?= Date: Mon, 20 Nov 2023 13:59:01 +0100 Subject: [PATCH 1/2] refactor: switch browser logging to tracing-web --- Cargo.lock | 26 ++++++++++++++++---------- node-wasm/Cargo.toml | 4 +++- node-wasm/src/utils.rs | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 205cb3ec..4aff5076 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -703,8 +703,10 @@ dependencies = [ "serde", "serde-wasm-bindgen", "serde_repr", + "time", "tracing", - "tracing-wasm", + "tracing-subscriber", + "tracing-web", "wasm-bindgen", "wasm-bindgen-futures", ] @@ -4544,6 +4546,7 @@ checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48" dependencies = [ "deranged", "itoa", + "js-sys", "serde", "time-core", "time-macros", @@ -4716,20 +4719,20 @@ dependencies = [ [[package]] name = "tracing-log" -version = "0.1.3" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" dependencies = [ - "lazy_static", "log", + "once_cell", "tracing-core", ] [[package]] name = "tracing-subscriber" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ "matchers", "nu-ansi-term", @@ -4738,20 +4741,23 @@ dependencies = [ "sharded-slab", "smallvec", "thread_local", + "time", "tracing", "tracing-core", "tracing-log", ] [[package]] -name = "tracing-wasm" -version = "0.2.1" +name = "tracing-web" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4575c663a174420fa2d78f4108ff68f65bf2fbb7dd89f33749b6e826b3626e07" +checksum = "0ff5efc53ed5a7c4b99b3dd24fd10f41e7aa1b284a4e64ae9167d97e31afe124" dependencies = [ - "tracing", + "js-sys", + "tracing-core", "tracing-subscriber", "wasm-bindgen", + "web-sys", ] [[package]] diff --git a/node-wasm/Cargo.toml b/node-wasm/Cargo.toml index 8b541902..5c5de60b 100644 --- a/node-wasm/Cargo.toml +++ b/node-wasm/Cargo.toml @@ -20,7 +20,9 @@ js-sys = "0.3.64" serde = { version = "1.0.164", features = ["derive"] } serde_repr = "0.1" serde-wasm-bindgen = "0.6.0" +time = { version = "0.3", features = ["wasm-bindgen"] } tracing = "0.1.37" -tracing-wasm = "0.2.1" +tracing-subscriber = { version = "0.3.18", features = ["time"] } +tracing-web = "0.1.2" wasm-bindgen = "0.2.88" wasm-bindgen-futures = "0.4.37" diff --git a/node-wasm/src/utils.rs b/node-wasm/src/utils.rs index ed122bcf..3129b417 100644 --- a/node-wasm/src/utils.rs +++ b/node-wasm/src/utils.rs @@ -2,6 +2,11 @@ use std::fmt; use celestia_node::network; use serde_repr::{Deserialize_repr, Serialize_repr}; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::fmt::format::Pretty; +use tracing_subscriber::fmt::time::UtcTime; +use tracing_subscriber::prelude::*; +use tracing_web::{performance_layer, MakeConsoleWriter}; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -18,7 +23,17 @@ pub enum Network { pub fn setup_logging() { console_error_panic_hook::set_once(); - tracing_wasm::set_as_global_default(); + let fmt_layer = tracing_subscriber::fmt::layer() + .with_ansi(false) // Only partially supported across browsers + .with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers + .with_writer(MakeConsoleWriter) // write events to the console + .with_filter(LevelFilter::INFO); // TODO: allow customizing the log level + let perf_layer = performance_layer().with_details_from_fields(Pretty::default()); + + tracing_subscriber::registry() + .with(fmt_layer) + .with(perf_layer) + .init(); } impl From for network::Network { From 2cf08bd9e6844e6ff1d56c31ec8b3a53b8517f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Zwoli=C5=84ski?= Date: Mon, 20 Nov 2023 15:06:02 +0100 Subject: [PATCH 2/2] switch to with_ansi(true) as we support chrome only --- node-wasm/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-wasm/src/utils.rs b/node-wasm/src/utils.rs index 3129b417..39b4cf7d 100644 --- a/node-wasm/src/utils.rs +++ b/node-wasm/src/utils.rs @@ -24,7 +24,7 @@ pub fn setup_logging() { console_error_panic_hook::set_once(); let fmt_layer = tracing_subscriber::fmt::layer() - .with_ansi(false) // Only partially supported across browsers + .with_ansi(true) // Only partially supported across browsers, but we target only chrome now .with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers .with_writer(MakeConsoleWriter) // write events to the console .with_filter(LevelFilter::INFO); // TODO: allow customizing the log level