π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add log forwarding to the frb and wasm bindings
Manav Rathic303016cc9c7
diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex 47de9a5580..e90ce7a83b 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2565,6 +2565,7 @@ version = "0.0.0"dependencies = ["ente-core","ente-space",+ "ente-wasm-log","serde","serde-wasm-bindgen","wasm-bindgen",@@ -2588,6 +2589,7 @@ dependencies = ["ente-accounts","ente-contacts","ente-core",+ "ente-wasm-log","js-sys","md-5 0.11.0","serde",@@ -2601,9 +2603,20 @@ name = "ente-wasm-core"version = "0.0.0"dependencies = ["ente-core",+ "ente-wasm-log","wasm-bindgen",]+[[package]]+name = "ente-wasm-log"+version = "0.0.0"+dependencies = [+ "js-sys",+ "log",+ "wasm-bindgen",+ "web-sys",+]+[[package]]name = "ente_photos_napi"version = "0.0.0"@@ -2633,6 +2646,7 @@ dependencies = ["ente-contacts","ente-core","flutter_rust_bridge",+ "log",][[package]]diff --git a/rust/Cargo.toml b/rust/Cargo.tomlindex 5d8e749b0f..397ec04147 100644--- a/rust/Cargo.toml+++ b/rust/Cargo.toml@@ -11,6 +11,7 @@ members = ["bindings/uniffi/ensu","bindings/wasm/ente-wasm","bindings/wasm/core",+ "bindings/wasm/log","bindings/wasm/space","crates/accounts","crates/assets",@@ -35,8 +36,10 @@ ente-paste = { path = "crates/paste" }ente-photos = { path = "crates/photos" }ente-space = { path = "crates/space" }ente-test-support = { path = "crates/test-support" }+ente-wasm-log = { path = "bindings/wasm/log" }flutter_rust_bridge = "=2.12.0"flutter_rust_bridge_codegen = "=2.12.0"+log = "0.4.29"md-5 = "0.11.0"mockito = "1.7.2"ort = { version = "=2.0.0-rc.13", default-features = false }diff --git a/rust/apps/cli/Cargo.toml b/rust/apps/cli/Cargo.tomlindex f5a50cc8f1..149bddc5c9 100644--- a/rust/apps/cli/Cargo.toml+++ b/rust/apps/cli/Cargo.toml@@ -21,7 +21,7 @@ ente-paste.workspace = trueenv_logger = "0.11"futures = "0.3"indicatif = "0.18"-log = "0.4"+log.workspace = trueopen = "5.0"rusqlite = { version = "0.38", features = ["bundled", "serde_json"] }serde.workspace = truediff --git a/rust/bindings/frb/ente-rust/Cargo.toml b/rust/bindings/frb/ente-rust/Cargo.tomlindex 9d9ae6c663..7ef262d009 100644--- a/rust/bindings/frb/ente-rust/Cargo.toml+++ b/rust/bindings/frb/ente-rust/Cargo.toml@@ -10,6 +10,7 @@ ente-accounts.workspace = trueente-contacts.workspace = trueente-core.workspace = trueflutter_rust_bridge.workspace = true+log.workspace = true[features]default = []diff --git a/rust/bindings/frb/ente-rust/src/api/logs.rs b/rust/bindings/frb/ente-rust/src/api/logs.rsnew file mode 100644index 0000000000..fa94a667d7--- /dev/null+++ b/rust/bindings/frb/ente-rust/src/api/logs.rs@@ -0,0 +1,73 @@+use std::sync::RwLock;++use crate::frb_generated::StreamSink;++pub enum LogLevel {+ Error,+ Warn,+ Info,+ Debug,+ Trace,+}++impl From<log::Level> for LogLevel {+ fn from(level: log::Level) -> Self {+ match level {+ log::Level::Error => LogLevel::Error,+ log::Level::Warn => LogLevel::Warn,+ log::Level::Info => LogLevel::Info,+ log::Level::Debug => LogLevel::Debug,+ log::Level::Trace => LogLevel::Trace,+ }+ }+}++impl From<LogLevel> for log::LevelFilter {+ fn from(level: LogLevel) -> Self {+ match level {+ LogLevel::Error => log::LevelFilter::Error,+ LogLevel::Warn => log::LevelFilter::Warn,+ LogLevel::Info => log::LevelFilter::Info,+ LogLevel::Debug => log::LevelFilter::Debug,+ LogLevel::Trace => log::LevelFilter::Trace,+ }+ }+}++pub struct LogEntry {+ pub level: LogLevel,+ pub target: String,+ pub message: String,+}++static SINK: RwLock<Option<StreamSink<LogEntry>>> = RwLock::new(None);+static LOGGER: StreamLogger = StreamLogger;++struct StreamLogger;++impl log::Log for StreamLogger {+ fn enabled(&self, metadata: &log::Metadata) -> bool {+ metadata.level() <= log::max_level()+ }++ fn log(&self, record: &log::Record) {+ if !self.enabled(record.metadata()) {+ return;+ }+ if let Some(sink) = SINK.read().unwrap().as_ref() {+ let _ = sink.add(LogEntry {+ level: record.level().into(),+ target: record.target().to_string(),+ message: record.args().to_string(),+ });+ }+ }++ fn flush(&self) {}+}++pub fn attach_log_stream(max_level: LogLevel, sink: StreamSink<LogEntry>) {+ *SINK.write().unwrap() = Some(sink);+ log::set_max_level(max_level.into());+ let _ = log::set_logger(&LOGGER);+}diff --git a/rust/bindings/frb/ente-rust/src/api/mod.rs b/rust/bindings/frb/ente-rust/src/api/mod.rsindex 826a58a579..ed3c126945 100644--- a/rust/bindings/frb/ente-rust/src/api/mod.rs+++ b/rust/bindings/frb/ente-rust/src/api/mod.rs@@ -2,4 +2,6 @@pub mod contacts;pub mod init;+#[cfg(any(feature = "flutter", frb_expand))]+pub mod logs;pub mod urls;diff --git a/rust/bindings/wasm/core/Cargo.toml b/rust/bindings/wasm/core/Cargo.tomlindex 830849bf05..21ed1fdacb 100644--- a/rust/bindings/wasm/core/Cargo.toml+++ b/rust/bindings/wasm/core/Cargo.toml@@ -7,4 +7,5 @@ crate-type = ["cdylib"][dependencies]ente-core = { workspace = true }+ente-wasm-log.workspace = truewasm-bindgen.workspace = truediff --git a/rust/bindings/wasm/core/src/lib.rs b/rust/bindings/wasm/core/src/lib.rsindex 44b051002f..09db41fcfb 100644--- a/rust/bindings/wasm/core/src/lib.rs+++ b/rust/bindings/wasm/core/src/lib.rs@@ -1,3 +1,5 @@//! Wasm bindings for the `ente-core` crate.+use ente_wasm_log as _;+mod crypto;diff --git a/rust/bindings/wasm/ente-wasm/Cargo.toml b/rust/bindings/wasm/ente-wasm/Cargo.tomlindex 26ea41e780..f0f35e93c7 100644--- a/rust/bindings/wasm/ente-wasm/Cargo.toml+++ b/rust/bindings/wasm/ente-wasm/Cargo.toml@@ -9,6 +9,7 @@ crate-type = ["cdylib"]ente-accounts.workspace = trueente-contacts.workspace = trueente-core.workspace = true+ente-wasm-log.workspace = truejs-sys = "0.3"md-5.workspace = trueserde.workspace = truediff --git a/rust/bindings/wasm/ente-wasm/src/lib.rs b/rust/bindings/wasm/ente-wasm/src/lib.rsindex 2e76249441..be7626a5ce 100644--- a/rust/bindings/wasm/ente-wasm/src/lib.rs+++ b/rust/bindings/wasm/ente-wasm/src/lib.rs@@ -1,5 +1,7 @@//! WASM bindings for Ente web apps.+use ente_wasm_log as _;+mod auth;mod contacts;mod crypto;diff --git a/rust/bindings/wasm/log/Cargo.toml b/rust/bindings/wasm/log/Cargo.tomlnew file mode 100644index 0000000000..2f28bfe2ef--- /dev/null+++ b/rust/bindings/wasm/log/Cargo.toml@@ -0,0 +1,9 @@+[package]+name = "ente-wasm-log"+edition = "2024"++[dependencies]+js-sys = "0.3"+log.workspace = true+wasm-bindgen.workspace = true+web-sys = { version = "0.3.102", features = ["console"] }diff --git a/rust/bindings/wasm/log/src/lib.rs b/rust/bindings/wasm/log/src/lib.rsnew file mode 100644index 0000000000..5c61abec46--- /dev/null+++ b/rust/bindings/wasm/log/src/lib.rs@@ -0,0 +1,54 @@+use wasm_bindgen::JsCast;+use wasm_bindgen::prelude::*;++const GLOBAL_HOOK: &str = "enteRustLog";++static LOGGER: WasmLogger = WasmLogger;++struct WasmLogger;++impl log::Log for WasmLogger {+ fn enabled(&self, metadata: &log::Metadata) -> bool {+ metadata.level() <= log::max_level()+ }++ fn log(&self, record: &log::Record) {+ if !self.enabled(record.metadata()) {+ return;+ }+ let level = record.level().to_string();+ let target = record.target().to_string();+ let message = record.args().to_string();++ let handled = js_sys::Reflect::get(&js_sys::global(), &GLOBAL_HOOK.into())+ .ok()+ .and_then(|hook| hook.dyn_into::<js_sys::Function>().ok())+ .is_some_and(|hook| {+ hook.call3(+ &JsValue::NULL,+ &level.as_str().into(),+ &target.as_str().into(),+ &message.as_str().into(),+ )+ .is_ok()+ });++ if !handled {+ let line = JsValue::from(format!("[{target}] {message}"));+ match record.level() {+ log::Level::Error => web_sys::console::error_1(&line),+ log::Level::Warn => web_sys::console::warn_1(&line),+ log::Level::Info => web_sys::console::info_1(&line),+ log::Level::Debug | log::Level::Trace => web_sys::console::debug_1(&line),+ }+ }+ }++ fn flush(&self) {}+}++#[wasm_bindgen(start)]+fn start() {+ let _ = log::set_logger(&LOGGER);+ log::set_max_level(log::LevelFilter::Info);+}diff --git a/rust/bindings/wasm/space/Cargo.toml b/rust/bindings/wasm/space/Cargo.tomlindex 10bfeadda5..d7dbf1e43b 100644--- a/rust/bindings/wasm/space/Cargo.toml+++ b/rust/bindings/wasm/space/Cargo.toml@@ -8,6 +8,7 @@ crate-type = ["cdylib"][dependencies]ente-core.workspace = trueente-space.workspace = true+ente-wasm-log.workspace = trueserde.workspace = trueserde-wasm-bindgen = "0.6"wasm-bindgen.workspace = truediff --git a/rust/bindings/wasm/space/src/lib.rs b/rust/bindings/wasm/space/src/lib.rsindex 4db6aed245..f31c1d7701 100644--- a/rust/bindings/wasm/space/src/lib.rs+++ b/rust/bindings/wasm/space/src/lib.rs@@ -1,3 +1,5 @@//! WASM bindings for Ente Space.+use ente_wasm_log as _;+mod space;diff --git a/rust/crates/core/Cargo.toml b/rust/crates/core/Cargo.tomlindex 54f22432b7..5676f88b5f 100644--- a/rust/crates/core/Cargo.toml+++ b/rust/crates/core/Cargo.toml@@ -13,7 +13,7 @@ crypto_secretstream = "=0.2.0"# `wasm32-unknown-unknown` (it uses the browser's `crypto.getRandomValues`).# This also enables `rand_core::OsRng` on WASM.getrandom = { version = "0.2", features = ["js"] }-log = "0.4"+log.workspace = truemd-5.workspace = truerand_core = { version = "0.6", features = ["getrandom"] }reqwest = { workspace = true, features = ["http2", "charset", "system-proxy", "json", "rustls-tls-native-roots", "rustls-tls-webpki-roots"] }