From 8f8bd40e8d870028ba299bf124ed45530da50954 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 10:15:25 +0200 Subject: [PATCH 01/12] feat(bindings/crypto-js): Redirect Rust panics to JavaScript console. --- bindings/matrix-sdk-crypto-js/Cargo.toml | 1 + bindings/matrix-sdk-crypto-js/src/lib.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index 94d14468c..b5cbe56af 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -34,6 +34,7 @@ vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "2404f83f7 wasm-bindgen = "0.2.80" wasm-bindgen-futures = "0.4.30" js-sys = "0.3.49" +console_error_panic_hook = "0.1.7" serde_json = "1.0.79" http = "0.2.6" anyhow = "1.0" diff --git a/bindings/matrix-sdk-crypto-js/src/lib.rs b/bindings/matrix-sdk-crypto-js/src/lib.rs index a9b59426f..a985e4138 100644 --- a/bindings/matrix-sdk-crypto-js/src/lib.rs +++ b/bindings/matrix-sdk-crypto-js/src/lib.rs @@ -29,6 +29,16 @@ pub mod sync_events; use js_sys::{Object, Reflect}; use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*}; +/// Run some stuff when the Wasm module is instantiated. +/// +/// Right now, it does the following: +/// +/// * Redirect Rust panics to JavaScript console. +#[wasm_bindgen(start)] +pub fn start() { + console_error_panic_hook::set_once(); +} + /// A really hacky and dirty code to downcast a `JsValue` to `T: /// RefFromWasmAbi`, inspired by /// https://github.com/rustwasm/wasm-bindgen/issues/2231#issuecomment-656293288. From 70561f9649088c6387ee787116fa4bb8fbb2c5b1 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 11:54:28 +0200 Subject: [PATCH 02/12] feat(bindings/crypto-js): Add the `userLogger` function to enable logging into `Console`. --- bindings/matrix-sdk-crypto-js/Cargo.toml | 14 +- bindings/matrix-sdk-crypto-js/src/lib.rs | 1 + bindings/matrix-sdk-crypto-js/src/logger.rs | 162 ++++++++++++++++++++ 3 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 bindings/matrix-sdk-crypto-js/src/logger.rs diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index b5cbe56af..0f7f0fa0c 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -31,10 +31,12 @@ matrix-sdk-common = { version = "0.5.0", path = "../../crates/matrix-sdk-common" matrix-sdk-crypto = { version = "0.5.0", path = "../../crates/matrix-sdk-crypto" } ruma = { git = "https://github.com/ruma/ruma", rev = "96155915f", features = ["client-api-c", "js", "rand", "unstable-msc2676", "unstable-msc2677"] } vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "2404f83f7d3a3779c1f518e4d949f7da9677c3dd", features = ["js"] } -wasm-bindgen = "0.2.80" -wasm-bindgen-futures = "0.4.30" -js-sys = "0.3.49" -console_error_panic_hook = "0.1.7" -serde_json = "1.0.79" -http = "0.2.6" +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" +js-sys = "0.3" +console_error_panic_hook = "0.1" +serde_json = "1.0" +http = "0.2" anyhow = "1.0" +tracing = { version = "0.1", default-features = false, features = ["attributes"] } +tracing-subscriber = { version = "0.3", default-features = false, features = ["registry", "std"] } diff --git a/bindings/matrix-sdk-crypto-js/src/lib.rs b/bindings/matrix-sdk-crypto-js/src/lib.rs index a985e4138..18b9714a7 100644 --- a/bindings/matrix-sdk-crypto-js/src/lib.rs +++ b/bindings/matrix-sdk-crypto-js/src/lib.rs @@ -21,6 +21,7 @@ pub mod encryption; pub mod events; mod future; pub mod identifiers; +mod logger; pub mod machine; pub mod requests; pub mod responses; diff --git a/bindings/matrix-sdk-crypto-js/src/logger.rs b/bindings/matrix-sdk-crypto-js/src/logger.rs new file mode 100644 index 000000000..a3d0a756f --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/src/logger.rs @@ -0,0 +1,162 @@ +use std::fmt::{self, Write as _}; + +use tracing::{ + field::{Field, Visit}, + Event, Id, Level, Metadata, Subscriber, +}; +use tracing_subscriber::{ + layer::{Context, Layer as TracingLayer}, + prelude::*, +}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Debug)] +pub enum LoggerLevel { + Debug, + Info, + Warn, + Trace, + Error, +} + +impl From for Level { + fn from(value: LoggerLevel) -> Self { + use LoggerLevel::*; + + match value { + Debug => Self::DEBUG, + Info => Self::INFO, + Warn => Self::WARN, + Trace => Self::TRACE, + Error => Self::ERROR, + } + } +} + +#[wasm_bindgen(js_name = "useLogger")] +pub fn use_logger(level: LoggerLevel) { + tracing_subscriber::registry().with(Layer::new(level)).init(); +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console, js_name = "debug")] + fn log_debug(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "info")] + fn log_info(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "warn")] + fn log_warn(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "trace")] + fn log_trace(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "log")] + fn log(message: String); + + #[wasm_bindgen(js_namespace = console, js_name= "group")] + fn log_group_open(label: String); + + #[wasm_bindgen(js_namespace = console, js_name= "groupEnd")] + fn log_group_close(); +} + +struct Layer { + level: Level, +} + +impl Layer { + fn new(level: L) -> Self + where + L: Into, + { + Self { level: level.into() } + } +} + +impl TracingLayer for Layer +where + S: Subscriber, +{ + fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { + metadata.level() >= &self.level + } + + fn on_enter(&self, id: &Id, _: Context<'_, S>) { + log_group_open(format!("t{:x}", id.into_u64())); + } + + fn on_exit(&self, _: &Id, _: Context<'_, S>) { + log_group_close(); + } + + fn on_event(&self, event: &Event<'_>, _: Context<'_, S>) { + let mut recorder = StringVisitor::new(); + event.record(&mut recorder); + let metadata = event.metadata(); + let level = metadata.level(); + + let origin = metadata + .file() + .and_then(|file| metadata.line().map(|ln| format!("{}:{}", file, ln))) + .unwrap_or_default(); + + let message = format!("{level} {origin} {recorder}"); + + match *level { + Level::DEBUG => log_debug(message), + Level::INFO => log_info(message), + Level::WARN => log_warn(message), + Level::TRACE => log_trace(message), + Level::ERROR => log(message), + } + } +} + +struct StringVisitor { + string: String, + displaying_arguments: bool, +} + +impl StringVisitor { + fn new() -> Self { + Self { string: String::new(), displaying_arguments: false } + } +} + +impl Visit for StringVisitor { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + match field.name() { + "message" => { + if !self.string.is_empty() { + self.string.push('\n'); + } + + let _ = write!(self.string, "{:?}", value); + } + + field_name => { + if self.displaying_arguments { + self.string.push('\n'); + } else { + self.string.push(' '); + self.displaying_arguments = true; + } + + let _ = write!(self.string, "{} = {:?}", field_name, value); + } + } + } +} + +impl fmt::Display for StringVisitor { + fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result { + if !self.string.is_empty() { + write!(&mut f, " {}", self.string) + } else { + Ok(()) + } + } +} From e7d0ee4379f6222249c701b53e1c3ef244b51b8f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 12:21:38 +0200 Subject: [PATCH 03/12] !fixup --- bindings/matrix-sdk-crypto-js/Cargo.toml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index 0f7f0fa0c..af864acd7 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -31,12 +31,12 @@ matrix-sdk-common = { version = "0.5.0", path = "../../crates/matrix-sdk-common" matrix-sdk-crypto = { version = "0.5.0", path = "../../crates/matrix-sdk-crypto" } ruma = { git = "https://github.com/ruma/ruma", rev = "96155915f", features = ["client-api-c", "js", "rand", "unstable-msc2676", "unstable-msc2677"] } vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "2404f83f7d3a3779c1f518e4d949f7da9677c3dd", features = ["js"] } -wasm-bindgen = "0.2" -wasm-bindgen-futures = "0.4" -js-sys = "0.3" -console_error_panic_hook = "0.1" -serde_json = "1.0" -http = "0.2" -anyhow = "1.0" -tracing = { version = "0.1", default-features = false, features = ["attributes"] } -tracing-subscriber = { version = "0.3", default-features = false, features = ["registry", "std"] } +wasm-bindgen = "0.2.80" +wasm-bindgen-futures = "0.4.30" +js-sys = "0.3.49" +console_error_panic_hook = "0.1.7" +serde_json = "1.0.79" +http = "0.2.6" +anyhow = "1.0.58" +tracing = { version = "0.1.35", default-features = false, features = ["attributes"] } +tracing-subscriber = { version = "0.3.14", default-features = false, features = ["registry", "std"] } From f5016dbb97c3d7f545e479674412d9f719f63d3e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 12:41:53 +0200 Subject: [PATCH 04/12] feat(bindings/crypto-js): Define `Layer.max_level_hint`. --- bindings/matrix-sdk-crypto-js/src/logger.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/logger.rs b/bindings/matrix-sdk-crypto-js/src/logger.rs index a3d0a756f..44c483539 100644 --- a/bindings/matrix-sdk-crypto-js/src/logger.rs +++ b/bindings/matrix-sdk-crypto-js/src/logger.rs @@ -2,6 +2,7 @@ use std::fmt::{self, Write as _}; use tracing::{ field::{Field, Visit}, + metadata::LevelFilter, Event, Id, Level, Metadata, Subscriber, }; use tracing_subscriber::{ @@ -35,8 +36,8 @@ impl From for Level { } #[wasm_bindgen(js_name = "useLogger")] -pub fn use_logger(level: LoggerLevel) { - tracing_subscriber::registry().with(Layer::new(level)).init(); +pub fn use_logger(max_level: LoggerLevel) { + tracing_subscriber::registry().with(Layer::new(max_level)).init(); } #[wasm_bindgen] @@ -64,15 +65,15 @@ extern "C" { } struct Layer { - level: Level, + max_level: Level, } impl Layer { - fn new(level: L) -> Self + fn new(max_level: L) -> Self where L: Into, { - Self { level: level.into() } + Self { max_level: max_level.into() } } } @@ -81,7 +82,11 @@ where S: Subscriber, { fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { - metadata.level() >= &self.level + metadata.level() <= &self.max_level + } + + fn max_level_hint(&self) -> Option { + Some(LevelFilter::from_level(self.max_level)) } fn on_enter(&self, id: &Id, _: Context<'_, S>) { From d39baf129547e818455dbd20c9f3a8f110f67171 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 13:01:07 +0200 Subject: [PATCH 05/12] test(bindings/crypto-js): Encrypt and decrypt a valid message. --- bindings/matrix-sdk-crypto-js/src/{logger.rs => tracing.rs} | 0 bindings/matrix-sdk-crypto-js/tests/machine.test.js | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) rename bindings/matrix-sdk-crypto-js/src/{logger.rs => tracing.rs} (100%) diff --git a/bindings/matrix-sdk-crypto-js/src/logger.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs similarity index 100% rename from bindings/matrix-sdk-crypto-js/src/logger.rs rename to bindings/matrix-sdk-crypto-js/src/tracing.rs diff --git a/bindings/matrix-sdk-crypto-js/tests/machine.test.js b/bindings/matrix-sdk-crypto-js/tests/machine.test.js index 7a851dc4c..dd759f1b8 100644 --- a/bindings/matrix-sdk-crypto-js/tests/machine.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/machine.test.js @@ -299,7 +299,8 @@ describe(OlmMachine.name, () => { room, 'm.room.message', JSON.stringify({ - "hello": "world" + "msgtype": "m.text", + "body": "Hello, World!" }), )); @@ -328,7 +329,8 @@ describe(OlmMachine.name, () => { expect(decrypted).toBeInstanceOf(DecryptedRoomEvent); const event = JSON.parse(decrypted.event); - expect(event.content.hello).toStrictEqual("world"); + expect(event.content.msgtype).toStrictEqual("m.text"); + expect(event.content.body).toStrictEqual("Hello, World!"); expect(decrypted.sender.toString()).toStrictEqual(user.toString()); expect(decrypted.senderDevice.toString()).toStrictEqual(device.toString()); From bb631f2f7945a4c55c2f079b87305453779e9e6b Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 12 Jul 2022 16:00:41 +0200 Subject: [PATCH 06/12] feat(bindings/crypto-js): Add ability to turn `Tracing` on and off, and change logger min level. The patch updates the code to use `tracing_subscriber::reload`, so that we get a `Handle` that can be used to modify the tracing at runtime. This patch also adds a new `tracing` feature. This patch finally adds a test suite for the `Tracing` API. --- bindings/matrix-sdk-crypto-js/Cargo.toml | 3 +- bindings/matrix-sdk-crypto-js/src/lib.rs | 2 +- bindings/matrix-sdk-crypto-js/src/tracing.rs | 161 ++++++++++++++---- .../tests/requests.test.js | 4 +- .../tests/tracing.test.js | 78 +++++++++ 5 files changed, 213 insertions(+), 35 deletions(-) create mode 100644 bindings/matrix-sdk-crypto-js/tests/tracing.test.js diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index af864acd7..dfe0f83c8 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -22,9 +22,10 @@ wasm-opt = ['-Oz'] crate-type = ["cdylib"] [features] -default = [] +default = ["tracing"] qrcode = ["matrix-sdk-crypto/qrcode"] docsrs = [] +tracing = [] [dependencies] matrix-sdk-common = { version = "0.5.0", path = "../../crates/matrix-sdk-common" } diff --git a/bindings/matrix-sdk-crypto-js/src/lib.rs b/bindings/matrix-sdk-crypto-js/src/lib.rs index 18b9714a7..b927fedca 100644 --- a/bindings/matrix-sdk-crypto-js/src/lib.rs +++ b/bindings/matrix-sdk-crypto-js/src/lib.rs @@ -21,11 +21,11 @@ pub mod encryption; pub mod events; mod future; pub mod identifiers; -mod logger; pub mod machine; pub mod requests; pub mod responses; pub mod sync_events; +mod tracing; use js_sys::{Object, Reflect}; use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*}; diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index 44c483539..dcdb07ae1 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -1,47 +1,137 @@ -use std::fmt::{self, Write as _}; +use std::fmt; +#[cfg(feature = "tracing")] +use std::fmt::Write as _; +#[cfg(feature = "tracing")] use tracing::{ field::{Field, Visit}, metadata::LevelFilter, Event, Id, Level, Metadata, Subscriber, }; +#[cfg(feature = "tracing")] use tracing_subscriber::{ layer::{Context, Layer as TracingLayer}, prelude::*, + reload, Registry, }; use wasm_bindgen::prelude::*; +/// Type to install and to manipulate the tracing layer. #[wasm_bindgen] -#[derive(Debug)] +pub struct Tracing { + #[cfg(feature = "tracing")] + handle: reload::Handle, +} + +impl fmt::Debug for Tracing { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Tracing").finish_non_exhaustive() + } +} + +#[wasm_bindgen] +impl Tracing { + /// Check whether the `tracing` feature has been enabled. + #[wasm_bindgen(js_name = "isAvailable")] + pub fn is_available() -> bool { + cfg!(feature = "tracing") + } + + /// Install the tracing layer. + /// + /// **Warning**: It must be installed only **once**, otherwise a + /// runtime error will be raised. + pub fn install(_min_level: LoggerLevel) -> Option { + #[cfg(not(feature = "tracing"))] + { + None + } + + #[cfg(feature = "tracing")] + { + let (filter, reload_handle) = reload::Layer::new(Layer::new(_min_level)); + + tracing_subscriber::registry().with(filter).init(); + + Some(Self { handle: reload_handle }) + } + } + + /// Re-define the minimum logger level. + #[cfg(feature = "tracing")] + #[wasm_bindgen(setter, js_name = "minLevel")] + pub fn min_level(&self, min_level: LoggerLevel) { + let _ = self.handle.modify(|layer| layer.min_level = min_level.into()); + } + + /// Turn the logger on, i.e. it emits logs again if it was turned + /// off. + #[cfg(feature = "tracing")] + #[wasm_bindgen(js_name = "turnOn")] + pub fn turn_on(&self) { + let _ = self.handle.modify(|layer| layer.turn_on()); + } + + /// Turn the logger off, i.e. it no long emits logs. + #[cfg(feature = "tracing")] + #[wasm_bindgen(js_name = "turnOff")] + pub fn turn_off(&self) { + let _ = self.handle.modify(|layer| layer.turn_off()); + } +} + +/// Logger level. +#[wasm_bindgen] +#[derive(Debug, Clone)] pub enum LoggerLevel { - Debug, - Info, - Warn, + /// `TRACE` level. + /// + /// Designate very low priority, often extremely verbose, + /// information. Trace, + + /// `DEBUG` level. + /// + /// Designate lower priority information. + Debug, + + /// `INFO` level. + /// + /// Designate useful information. + Info, + + /// `WARN` level. + /// + /// Designate hazardous situations. + Warn, + + /// `ERROR` level. + /// + /// Designate very serious errors. Error, } +#[cfg(feature = "tracing")] impl From for Level { fn from(value: LoggerLevel) -> Self { use LoggerLevel::*; match value { + Trace => Self::TRACE, Debug => Self::DEBUG, Info => Self::INFO, Warn => Self::WARN, - Trace => Self::TRACE, Error => Self::ERROR, } } } -#[wasm_bindgen(js_name = "useLogger")] -pub fn use_logger(max_level: LoggerLevel) { - tracing_subscriber::registry().with(Layer::new(max_level)).init(); -} - +#[cfg(feature = "tracing")] #[wasm_bindgen] extern "C" { + #[wasm_bindgen(js_namespace = console, js_name = "trace")] + fn log_trace(message: String); + #[wasm_bindgen(js_namespace = console, js_name = "debug")] fn log_debug(message: String); @@ -51,9 +141,6 @@ extern "C" { #[wasm_bindgen(js_namespace = console, js_name = "warn")] fn log_warn(message: String); - #[wasm_bindgen(js_namespace = console, js_name = "trace")] - fn log_trace(message: String); - #[wasm_bindgen(js_namespace = console, js_name = "log")] fn log(message: String); @@ -64,29 +151,45 @@ extern "C" { fn log_group_close(); } +#[cfg(feature = "tracing")] struct Layer { - max_level: Level, + min_level: Level, + enabled: bool, } +#[cfg(feature = "tracing")] impl Layer { - fn new(max_level: L) -> Self + fn new(min_level: L) -> Self where L: Into, { - Self { max_level: max_level.into() } + Self { min_level: min_level.into(), enabled: true } + } + + fn turn_on(&mut self) { + self.enabled = true; + } + + fn turn_off(&mut self) { + self.enabled = false; } } +#[cfg(feature = "tracing")] impl TracingLayer for Layer where S: Subscriber, { fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { - metadata.level() <= &self.max_level + self.enabled && metadata.level() <= &self.min_level } fn max_level_hint(&self) -> Option { - Some(LevelFilter::from_level(self.max_level)) + if !self.enabled { + Some(LevelFilter::OFF) + } else { + Some(LevelFilter::from_level(self.min_level)) + } } fn on_enter(&self, id: &Id, _: Context<'_, S>) { @@ -108,29 +211,31 @@ where .and_then(|file| metadata.line().map(|ln| format!("{}:{}", file, ln))) .unwrap_or_default(); - let message = format!("{level} {origin} {recorder}"); + let message = format!("{level} {origin}{recorder}"); match *level { + Level::TRACE => log_trace(message), Level::DEBUG => log_debug(message), Level::INFO => log_info(message), Level::WARN => log_warn(message), - Level::TRACE => log_trace(message), Level::ERROR => log(message), } } } +#[cfg(feature = "tracing")] struct StringVisitor { string: String, - displaying_arguments: bool, } +#[cfg(feature = "tracing")] impl StringVisitor { fn new() -> Self { - Self { string: String::new(), displaying_arguments: false } + Self { string: String::new() } } } +#[cfg(feature = "tracing")] impl Visit for StringVisitor { fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { match field.name() { @@ -143,19 +248,13 @@ impl Visit for StringVisitor { } field_name => { - if self.displaying_arguments { - self.string.push('\n'); - } else { - self.string.push(' '); - self.displaying_arguments = true; - } - - let _ = write!(self.string, "{} = {:?}", field_name, value); + let _ = write!(self.string, "\n{} = {:?}", field_name, value); } } } } +#[cfg(feature = "tracing")] impl fmt::Display for StringVisitor { fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result { if !self.string.is_empty() { diff --git a/bindings/matrix-sdk-crypto-js/tests/requests.test.js b/bindings/matrix-sdk-crypto-js/tests/requests.test.js index 1f806f4ae..b23a2d31e 100644 --- a/bindings/matrix-sdk-crypto-js/tests/requests.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/requests.test.js @@ -12,7 +12,7 @@ describe('RequestType', () => { }); }); -for (const [request, request_type] of [ +for (const [request, requestType] of [ [KeysUploadRequest, RequestType.KeysUpload], [KeysQueryRequest, RequestType.KeysQuery], [KeysClaimRequest, RequestType.KeysClaim], @@ -28,7 +28,7 @@ for (const [request, request_type] of [ expect(r).toBeInstanceOf(request); expect(r.id).toStrictEqual('foo'); expect(r.body).toStrictEqual('{"bar": "baz"}'); - expect(r.type).toStrictEqual(request_type); + expect(r.type).toStrictEqual(requestType); }); }) diff --git a/bindings/matrix-sdk-crypto-js/tests/tracing.test.js b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js new file mode 100644 index 000000000..89fbce2bb --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js @@ -0,0 +1,78 @@ +const { Tracing, LoggerLevel, OlmMachine, UserId, DeviceId } = require('../pkg/matrix_sdk_crypto'); + +describe('LoggerLevel', () => { + test('has the correct variant values', () => { + expect(LoggerLevel.Trace).toStrictEqual(0); + expect(LoggerLevel.Debug).toStrictEqual(1); + expect(LoggerLevel.Info).toStrictEqual(2); + expect(LoggerLevel.Warn).toStrictEqual(3); + expect(LoggerLevel.Error).toStrictEqual(4); + }); +}); + +describe(Tracing.name, () => { + if (Tracing.isAvailable()) { + let tracing = Tracing.install(LoggerLevel.Debug); + + const originalConsoleDebug = console.debug; + + for (const [testName, testPreState, testPostState, expectedGotcha] of [ + [ + 'can log something', + () => {}, + () => {}, + true, + ], + [ + 'can change the logger level', + () => { tracing.minLevel = LoggerLevel.Warn }, + () => { tracing.minLevel = LoggerLevel.Debug }, + false, + ], + [ + 'can be turned off', + () => { tracing.turnOff() }, + () => {}, + false, + ], + [ + 'can be turned on', + () => { tracing.turnOn() }, + () => {}, + true, + ], + + // This one *must* be the last. We are turning tracing off + // again for the other tests. + [ + 'can be turned off', + () => { tracing.turnOff() }, + () => {}, + false, + ], + ]) { + test(testName, async () => { + testPreState(); + + let gotcha = false; + + console.debug = (msg) => { + gotcha = true; + expect(msg).not.toHaveLength(0); + }; + + // Do something that emits a `DEBUG` log. + await new OlmMachine(new UserId('@alice:example.org'), new DeviceId('foo')); + + console.debug = originalConsoleDebug; + testPostState(); + + expect(gotcha).toStrictEqual(expectedGotcha); + }); + } + } else { + test('cannot be constructed', () => { + expect(Tracing.install(LoggerLevel.Error)).toBeUndefined(); + }); + } +}); From 283c5ff51e039dcb2ae7c3bcce07c72e2ee1d05f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 14 Jul 2022 09:04:44 +0200 Subject: [PATCH 07/12] fix(bindings/crypto-js): Let's not deal with `Console.group`. Events and spans from `tracing` can happen asynchronously, and could mess the `Console.group` structure. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index dcdb07ae1..1fea6d91c 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -143,12 +143,6 @@ extern "C" { #[wasm_bindgen(js_namespace = console, js_name = "log")] fn log(message: String); - - #[wasm_bindgen(js_namespace = console, js_name= "group")] - fn log_group_open(label: String); - - #[wasm_bindgen(js_namespace = console, js_name= "groupEnd")] - fn log_group_close(); } #[cfg(feature = "tracing")] @@ -192,14 +186,6 @@ where } } - fn on_enter(&self, id: &Id, _: Context<'_, S>) { - log_group_open(format!("t{:x}", id.into_u64())); - } - - fn on_exit(&self, _: &Id, _: Context<'_, S>) { - log_group_close(); - } - fn on_event(&self, event: &Event<'_>, _: Context<'_, S>) { let mut recorder = StringVisitor::new(); event.record(&mut recorder); From c763ce3f4177b71ffa1935569583582599142670 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Jul 2022 09:23:37 +0200 Subject: [PATCH 08/12] feat(bindings/crypto-js): `Tracing` can be installed more than once. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 41 +++++++++++++++---- .../tests/tracing.test.js | 6 +++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index 1fea6d91c..d7ed61d5b 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -1,12 +1,12 @@ -use std::fmt; +use std::{fmt, sync::Arc}; #[cfg(feature = "tracing")] -use std::fmt::Write as _; +use std::{fmt::Write as _, sync::Once}; #[cfg(feature = "tracing")] use tracing::{ field::{Field, Visit}, metadata::LevelFilter, - Event, Id, Level, Metadata, Subscriber, + Event, Level, Metadata, Subscriber, }; #[cfg(feature = "tracing")] use tracing_subscriber::{ @@ -16,11 +16,13 @@ use tracing_subscriber::{ }; use wasm_bindgen::prelude::*; +type TracingInner = Arc>; + /// Type to install and to manipulate the tracing layer. #[wasm_bindgen] pub struct Tracing { #[cfg(feature = "tracing")] - handle: reload::Handle, + handle: TracingInner, } impl fmt::Debug for Tracing { @@ -39,8 +41,12 @@ impl Tracing { /// Install the tracing layer. /// - /// **Warning**: It must be installed only **once**, otherwise a - /// runtime error will be raised. + /// `Tracing` is a singleton. Once it is installed, consecutive + /// calls to `install` will construct a new `Tracing` object but + /// with the exact same inner state. Calling `install` with a new + /// `min_level` will just update the `min_level` parameter; in + /// that regard, it is similar to calling the `min_level` method + /// on an existing `Tracing` object. pub fn install(_min_level: LoggerLevel) -> Option { #[cfg(not(feature = "tracing"))] { @@ -49,11 +55,28 @@ impl Tracing { #[cfg(feature = "tracing")] { - let (filter, reload_handle) = reload::Layer::new(Layer::new(_min_level)); + static mut INSTALL: Option = None; + static INSTALLED: Once = Once::new(); - tracing_subscriber::registry().with(filter).init(); + INSTALLED.call_once(|| { + let (filter, reload_handle) = reload::Layer::new(Layer::new(_min_level.clone())); - Some(Self { handle: reload_handle }) + tracing_subscriber::registry().with(filter).init(); + + unsafe { INSTALL = Some(Arc::new(reload_handle)) }; + }); + + let tracing = Tracing { + handle: unsafe { INSTALL.as_ref() } + .cloned() + .expect("`Tracing` has not been installed correctly"), + }; + + // If it's not the first call to `install`, the + // `min_level` can be different. Let's update it. + tracing.min_level(_min_level); + + Some(tracing) } } diff --git a/bindings/matrix-sdk-crypto-js/tests/tracing.test.js b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js index 89fbce2bb..0ce28cc6f 100644 --- a/bindings/matrix-sdk-crypto-js/tests/tracing.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js @@ -14,6 +14,12 @@ describe(Tracing.name, () => { if (Tracing.isAvailable()) { let tracing = Tracing.install(LoggerLevel.Debug); + test('can installed several times', () => { + Tracing.install(LoggerLevel.Debug); + Tracing.install(LoggerLevel.Warn); + Tracing.install(LoggerLevel.Debug); + }); + const originalConsoleDebug = console.debug; for (const [testName, testPreState, testPostState, expectedGotcha] of [ From decd3fcb430d2d40b4ba4ff1f438cbd1e8471479 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Jul 2022 09:44:42 +0200 Subject: [PATCH 09/12] feat(bindings/crypto-js) Simplify code for `feature = "tracing"`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch creates one `inner` module for when `feature = "tracing"`, and one for when `no(feature = "tracing")`. Then, let's expose everything from `inner::*`. This patch also replaces `Tracing.install` by `new Tracing`. In case of `not(feature = "tracing")`, `new Tracing` raises an error. The goal is to remove all the `#[cfg(…)]` annotations everywhere. Now there is only 2 of them. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 423 +++++++++--------- .../tests/tracing.test.js | 10 +- 2 files changed, 223 insertions(+), 210 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index d7ed61d5b..cf1f98d63 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -1,108 +1,5 @@ -use std::{fmt, sync::Arc}; -#[cfg(feature = "tracing")] -use std::{fmt::Write as _, sync::Once}; - -#[cfg(feature = "tracing")] -use tracing::{ - field::{Field, Visit}, - metadata::LevelFilter, - Event, Level, Metadata, Subscriber, -}; -#[cfg(feature = "tracing")] -use tracing_subscriber::{ - layer::{Context, Layer as TracingLayer}, - prelude::*, - reload, Registry, -}; use wasm_bindgen::prelude::*; -type TracingInner = Arc>; - -/// Type to install and to manipulate the tracing layer. -#[wasm_bindgen] -pub struct Tracing { - #[cfg(feature = "tracing")] - handle: TracingInner, -} - -impl fmt::Debug for Tracing { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Tracing").finish_non_exhaustive() - } -} - -#[wasm_bindgen] -impl Tracing { - /// Check whether the `tracing` feature has been enabled. - #[wasm_bindgen(js_name = "isAvailable")] - pub fn is_available() -> bool { - cfg!(feature = "tracing") - } - - /// Install the tracing layer. - /// - /// `Tracing` is a singleton. Once it is installed, consecutive - /// calls to `install` will construct a new `Tracing` object but - /// with the exact same inner state. Calling `install` with a new - /// `min_level` will just update the `min_level` parameter; in - /// that regard, it is similar to calling the `min_level` method - /// on an existing `Tracing` object. - pub fn install(_min_level: LoggerLevel) -> Option { - #[cfg(not(feature = "tracing"))] - { - None - } - - #[cfg(feature = "tracing")] - { - static mut INSTALL: Option = None; - static INSTALLED: Once = Once::new(); - - INSTALLED.call_once(|| { - let (filter, reload_handle) = reload::Layer::new(Layer::new(_min_level.clone())); - - tracing_subscriber::registry().with(filter).init(); - - unsafe { INSTALL = Some(Arc::new(reload_handle)) }; - }); - - let tracing = Tracing { - handle: unsafe { INSTALL.as_ref() } - .cloned() - .expect("`Tracing` has not been installed correctly"), - }; - - // If it's not the first call to `install`, the - // `min_level` can be different. Let's update it. - tracing.min_level(_min_level); - - Some(tracing) - } - } - - /// Re-define the minimum logger level. - #[cfg(feature = "tracing")] - #[wasm_bindgen(setter, js_name = "minLevel")] - pub fn min_level(&self, min_level: LoggerLevel) { - let _ = self.handle.modify(|layer| layer.min_level = min_level.into()); - } - - /// Turn the logger on, i.e. it emits logs again if it was turned - /// off. - #[cfg(feature = "tracing")] - #[wasm_bindgen(js_name = "turnOn")] - pub fn turn_on(&self) { - let _ = self.handle.modify(|layer| layer.turn_on()); - } - - /// Turn the logger off, i.e. it no long emits logs. - #[cfg(feature = "tracing")] - #[wasm_bindgen(js_name = "turnOff")] - pub fn turn_off(&self) { - let _ = self.handle.modify(|layer| layer.turn_off()); - } -} - /// Logger level. #[wasm_bindgen] #[derive(Debug, Clone)] @@ -135,141 +32,257 @@ pub enum LoggerLevel { } #[cfg(feature = "tracing")] -impl From for Level { - fn from(value: LoggerLevel) -> Self { - use LoggerLevel::*; +mod inner { + use std::{ + fmt, + fmt::Write as _, + sync::{Arc, Once}, + }; - match value { - Trace => Self::TRACE, - Debug => Self::DEBUG, - Info => Self::INFO, - Warn => Self::WARN, - Error => Self::ERROR, + use super::*; + use tracing::{ + field::{Field, Visit}, + metadata::LevelFilter, + Event, Level, Metadata, Subscriber, + }; + use tracing_subscriber::{ + layer::{Context, Layer as TracingLayer}, + prelude::*, + reload, Registry, + }; + + type TracingInner = Arc>; + + /// Type to install and to manipulate the tracing layer. + #[wasm_bindgen] + pub struct Tracing { + handle: TracingInner, + } + + impl fmt::Debug for Tracing { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Tracing").finish_non_exhaustive() } } -} -#[cfg(feature = "tracing")] -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console, js_name = "trace")] - fn log_trace(message: String); + #[wasm_bindgen] + impl Tracing { + /// Check whether the `tracing` feature has been enabled. + #[wasm_bindgen(js_name = "isAvailable")] + pub fn is_available() -> bool { + true + } - #[wasm_bindgen(js_namespace = console, js_name = "debug")] - fn log_debug(message: String); + /// Install the tracing layer. + /// + /// `Tracing` is a singleton. Once it is installed, consecutive + /// calls to `install` will construct a new `Tracing` object but + /// with the exact same inner state. Calling `install` with a new + /// `min_level` will just update the `min_level` parameter; in + /// that regard, it is similar to calling the `min_level` method + /// on an existing `Tracing` object. + #[wasm_bindgen(constructor)] + pub fn new(min_level: LoggerLevel) -> Result { + static mut INSTALL: Option = None; + static INSTALLED: Once = Once::new(); - #[wasm_bindgen(js_namespace = console, js_name = "info")] - fn log_info(message: String); + INSTALLED.call_once(|| { + let (filter, reload_handle) = reload::Layer::new(Layer::new(min_level.clone())); - #[wasm_bindgen(js_namespace = console, js_name = "warn")] - fn log_warn(message: String); + tracing_subscriber::registry().with(filter).init(); - #[wasm_bindgen(js_namespace = console, js_name = "log")] - fn log(message: String); -} + unsafe { INSTALL = Some(Arc::new(reload_handle)) }; + }); -#[cfg(feature = "tracing")] -struct Layer { - min_level: Level, - enabled: bool, -} + let tracing = Tracing { + handle: unsafe { INSTALL.as_ref() } + .cloned() + .expect("`Tracing` has not been installed correctly"), + }; -#[cfg(feature = "tracing")] -impl Layer { - fn new(min_level: L) -> Self + // If it's not the first call to `install`, the + // `min_level` can be different. Let's update it. + tracing.min_level(min_level); + + Ok(tracing) + } + + /// Re-define the minimum logger level. + #[wasm_bindgen(setter, js_name = "minLevel")] + pub fn min_level(&self, min_level: LoggerLevel) { + let _ = self.handle.modify(|layer| layer.min_level = min_level.into()); + } + + /// Turn the logger on, i.e. it emits logs again if it was turned + /// off. + #[wasm_bindgen(js_name = "turnOn")] + pub fn turn_on(&self) { + let _ = self.handle.modify(|layer| layer.turn_on()); + } + + /// Turn the logger off, i.e. it no long emits logs. + #[wasm_bindgen(js_name = "turnOff")] + pub fn turn_off(&self) { + let _ = self.handle.modify(|layer| layer.turn_off()); + } + } + + impl From for Level { + fn from(value: LoggerLevel) -> Self { + use LoggerLevel::*; + + match value { + Trace => Self::TRACE, + Debug => Self::DEBUG, + Info => Self::INFO, + Warn => Self::WARN, + Error => Self::ERROR, + } + } + } + + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_namespace = console, js_name = "trace")] + fn log_trace(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "debug")] + fn log_debug(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "info")] + fn log_info(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "warn")] + fn log_warn(message: String); + + #[wasm_bindgen(js_namespace = console, js_name = "log")] + fn log(message: String); + } + + struct Layer { + min_level: Level, + enabled: bool, + } + + impl Layer { + fn new(min_level: L) -> Self + where + L: Into, + { + Self { min_level: min_level.into(), enabled: true } + } + + fn turn_on(&mut self) { + self.enabled = true; + } + + fn turn_off(&mut self) { + self.enabled = false; + } + } + + impl TracingLayer for Layer where - L: Into, + S: Subscriber, { - Self { min_level: min_level.into(), enabled: true } - } + fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { + self.enabled && metadata.level() <= &self.min_level + } - fn turn_on(&mut self) { - self.enabled = true; - } + fn max_level_hint(&self) -> Option { + if !self.enabled { + Some(LevelFilter::OFF) + } else { + Some(LevelFilter::from_level(self.min_level)) + } + } - fn turn_off(&mut self) { - self.enabled = false; - } -} + fn on_event(&self, event: &Event<'_>, _: Context<'_, S>) { + let mut recorder = StringVisitor::new(); + event.record(&mut recorder); + let metadata = event.metadata(); + let level = metadata.level(); -#[cfg(feature = "tracing")] -impl TracingLayer for Layer -where - S: Subscriber, -{ - fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool { - self.enabled && metadata.level() <= &self.min_level - } + let origin = metadata + .file() + .and_then(|file| metadata.line().map(|ln| format!("{}:{}", file, ln))) + .unwrap_or_default(); - fn max_level_hint(&self) -> Option { - if !self.enabled { - Some(LevelFilter::OFF) - } else { - Some(LevelFilter::from_level(self.min_level)) + let message = format!("{level} {origin}{recorder}"); + + match *level { + Level::TRACE => log_trace(message), + Level::DEBUG => log_debug(message), + Level::INFO => log_info(message), + Level::WARN => log_warn(message), + Level::ERROR => log(message), + } } } - fn on_event(&self, event: &Event<'_>, _: Context<'_, S>) { - let mut recorder = StringVisitor::new(); - event.record(&mut recorder); - let metadata = event.metadata(); - let level = metadata.level(); + struct StringVisitor { + string: String, + } - let origin = metadata - .file() - .and_then(|file| metadata.line().map(|ln| format!("{}:{}", file, ln))) - .unwrap_or_default(); - - let message = format!("{level} {origin}{recorder}"); - - match *level { - Level::TRACE => log_trace(message), - Level::DEBUG => log_debug(message), - Level::INFO => log_info(message), - Level::WARN => log_warn(message), - Level::ERROR => log(message), + impl StringVisitor { + fn new() -> Self { + Self { string: String::new() } } } -} -#[cfg(feature = "tracing")] -struct StringVisitor { - string: String, -} + impl Visit for StringVisitor { + fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { + match field.name() { + "message" => { + if !self.string.is_empty() { + self.string.push('\n'); + } -#[cfg(feature = "tracing")] -impl StringVisitor { - fn new() -> Self { - Self { string: String::new() } - } -} - -#[cfg(feature = "tracing")] -impl Visit for StringVisitor { - fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { - match field.name() { - "message" => { - if !self.string.is_empty() { - self.string.push('\n'); + let _ = write!(self.string, "{:?}", value); } - let _ = write!(self.string, "{:?}", value); + field_name => { + let _ = write!(self.string, "\n{} = {:?}", field_name, value); + } } + } + } - field_name => { - let _ = write!(self.string, "\n{} = {:?}", field_name, value); + impl fmt::Display for StringVisitor { + fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result { + if !self.string.is_empty() { + write!(&mut f, " {}", self.string) + } else { + Ok(()) } } } } -#[cfg(feature = "tracing")] -impl fmt::Display for StringVisitor { - fn fmt(&self, mut f: &mut fmt::Formatter<'_>) -> fmt::Result { - if !self.string.is_empty() { - write!(&mut f, " {}", self.string) - } else { - Ok(()) +#[cfg(not(feature = "tracing"))] +mod inner { + use super::*; + + /// Type to install and to manipulate the tracing layer. + #[wasm_bindgen] + #[derive(Debug)] + pub struct Tracing; + + #[wasm_bindgen] + impl Tracing { + /// Check whether the `tracing` feature has been enabled. + #[wasm_bindgen(js_name = "isAvailable")] + pub fn is_available() -> bool { + false + } + + /// The `tracing` feature is not enabled, so this constructor + /// will raise an error. + #[wasm_bindgen(constructor)] + pub fn new(_min_level: LoggerLevel) -> Result { + Err(JsError::new("The `tracing` feature is disabled. Check `Tracing.isAvailable` before constructing `Tracing`")) } } } + +pub use inner::*; diff --git a/bindings/matrix-sdk-crypto-js/tests/tracing.test.js b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js index 0ce28cc6f..bf27431ee 100644 --- a/bindings/matrix-sdk-crypto-js/tests/tracing.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/tracing.test.js @@ -12,12 +12,12 @@ describe('LoggerLevel', () => { describe(Tracing.name, () => { if (Tracing.isAvailable()) { - let tracing = Tracing.install(LoggerLevel.Debug); + let tracing = new Tracing(LoggerLevel.Debug); test('can installed several times', () => { - Tracing.install(LoggerLevel.Debug); - Tracing.install(LoggerLevel.Warn); - Tracing.install(LoggerLevel.Debug); + new Tracing(LoggerLevel.Debug); + new Tracing(LoggerLevel.Warn); + new Tracing(LoggerLevel.Debug); }); const originalConsoleDebug = console.debug; @@ -78,7 +78,7 @@ describe(Tracing.name, () => { } } else { test('cannot be constructed', () => { - expect(Tracing.install(LoggerLevel.Error)).toBeUndefined(); + expect(() => { new Tracing(LoggerLevel.Error) }).toThrow(); }); } }); From 3d1c96fbecf4c77f1abb0406fec42f8cd92a600d Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Jul 2022 09:51:44 +0200 Subject: [PATCH 10/12] feat(bindings/crypto-js): Redirect errors to `console.error`. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index cf1f98d63..98599dd36 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -155,8 +155,8 @@ mod inner { #[wasm_bindgen(js_namespace = console, js_name = "warn")] fn log_warn(message: String); - #[wasm_bindgen(js_namespace = console, js_name = "log")] - fn log(message: String); + #[wasm_bindgen(js_namespace = console, js_name = "error")] + fn log_error(message: String); } struct Layer { @@ -215,7 +215,7 @@ mod inner { Level::DEBUG => log_debug(message), Level::INFO => log_info(message), Level::WARN => log_warn(message), - Level::ERROR => log(message), + Level::ERROR => log_error(message), } } } From daa0fc02069afb837a90babfac97ae54c51d3f1f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Jul 2022 10:05:05 +0200 Subject: [PATCH 11/12] doc(bindings/crypto-js): Fix typos. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index 98599dd36..ac0196c24 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -75,12 +75,13 @@ mod inner { /// Install the tracing layer. /// - /// `Tracing` is a singleton. Once it is installed, consecutive - /// calls to `install` will construct a new `Tracing` object but - /// with the exact same inner state. Calling `install` with a new - /// `min_level` will just update the `min_level` parameter; in - /// that regard, it is similar to calling the `min_level` method - /// on an existing `Tracing` object. + /// `Tracing` is a singleton. Once it is installed, + /// consecutive calls to the constructor will construct a new + /// `Tracing` object but with the exact same inner + /// state. Calling the constructor with a new `min_level` will + /// just update the `min_level` parameter; in that regard, it + /// is similar to calling the `min_level` method on an + /// existing `Tracing` object. #[wasm_bindgen(constructor)] pub fn new(min_level: LoggerLevel) -> Result { static mut INSTALL: Option = None; From c72ec36b3a63083b8b239f96f23339db197d5452 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Jul 2022 14:39:39 +0200 Subject: [PATCH 12/12] chore(style) Make `cargo fmt` happy. --- bindings/matrix-sdk-crypto-js/src/tracing.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-crypto-js/src/tracing.rs b/bindings/matrix-sdk-crypto-js/src/tracing.rs index ac0196c24..75a7801aa 100644 --- a/bindings/matrix-sdk-crypto-js/src/tracing.rs +++ b/bindings/matrix-sdk-crypto-js/src/tracing.rs @@ -39,7 +39,6 @@ mod inner { sync::{Arc, Once}, }; - use super::*; use tracing::{ field::{Field, Visit}, metadata::LevelFilter, @@ -51,6 +50,8 @@ mod inner { reload, Registry, }; + use super::*; + type TracingInner = Arc>; /// Type to install and to manipulate the tracing layer.