π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Tie FRB log sinks to isolate lifetimes
Manav Rathi0c76e935f964
diff --git a/mobile/apps/locker/lib/main.dart b/mobile/apps/locker/lib/main.dartindex 589ebd61c6..05cfd65471 100644--- a/mobile/apps/locker/lib/main.dart+++ b/mobile/apps/locker/lib/main.dart@@ -46,6 +46,7 @@ import 'package:shared_preferences/shared_preferences.dart';final _logger = Logger("main");bool _isRustInitialized = false;Future<void>? _rustInitFuture;+late final LogSinkGuard _rustLogSinkGuard;void main() async {WidgetsFlutterBinding.ensureInitialized();@@ -128,7 +129,8 @@ Future<void> _ensureRustInitialized() async {void _attachRustLogStream() {final logger = Logger("rust");- attachLogStream().listen((entry) {+ _rustLogSinkGuard = LogSinkGuard();+ _rustLogSinkGuard.attachLogStream().listen((entry) {final message = "[${entry.target}] ${entry.message}";switch (entry.level) {case LogLevel.error:diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex 3bb7b84d11..dfafa9a424 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -83,6 +83,8 @@ bool _stopHearBeat = false;bool _isSyncInitialized = false;bool _isRustInitialized = false;Future<void>? _rustInitFuture;+late final LogSinkGuard _enteRustLogSinkGuard;+late final photos_rust_log.LogSinkGuard _photosRustLogSinkGuard;enum ForegroundStartupMode { normal, picker }@@ -567,10 +569,12 @@ Future<void> _ensureRustInitialized({required String via}) async {void _attachRustLogStream() {final logger = Logger("rust");- attachLogStream().listen((entry) {+ _enteRustLogSinkGuard = LogSinkGuard();+ _enteRustLogSinkGuard.attachLogStream().listen((entry) {_logRustEntry(logger, entry.level.name, entry.target, entry.message);});- photos_rust_log.attachLogStream().listen((entry) {+ _photosRustLogSinkGuard = photos_rust_log.LogSinkGuard();+ _photosRustLogSinkGuard.attachLogStream().listen((entry) {_logRustEntry(logger, entry.level.name, entry.target, entry.message);});}diff --git a/rust/bindings/frb/ente-rust/src/api/log.rs b/rust/bindings/frb/ente-rust/src/api/log.rsindex 7953c91844..c2affcd727 100644--- a/rust/bindings/frb/ente-rust/src/api/log.rs+++ b/rust/bindings/frb/ente-rust/src/api/log.rs@@ -1,5 +1,8 @@+use std::sync::atomic::{AtomicU64, Ordering};use std::sync::{Mutex, Once};+use flutter_rust_bridge::frb;+use crate::frb_generated::StreamSink;#[derive(Clone, Copy)]@@ -15,12 +18,44 @@ pub struct LogEntry {pub message: String,}-static SINKS: Mutex<Vec<StreamSink<LogEntry>>> = Mutex::new(Vec::new());+static NEXT_SINK_ID: AtomicU64 = AtomicU64::new(0);+static SINKS: Mutex<Vec<(u64, StreamSink<LogEntry>)>> = Mutex::new(Vec::new());static LOGGER: StreamLogger = StreamLogger;static LOGGER_INIT: Once = Once::new();struct StreamLogger;+#[frb(opaque)]+pub struct LogSinkGuard {+ id: u64,+}++impl LogSinkGuard {+ #[frb(sync)]+ pub fn new() -> Self {+ Self {+ id: NEXT_SINK_ID.fetch_add(1, Ordering::Relaxed),+ }+ }++ pub fn attach_log_stream(&self, sink: StreamSink<LogEntry>) {+ SINKS.lock().unwrap().push((self.id, sink));+ log::set_max_level(log::LevelFilter::Info);+ }+}++impl Drop for LogSinkGuard {+ fn drop(&mut self) {+ let _sink = {+ let mut sinks = SINKS.lock().unwrap();+ sinks+ .iter()+ .position(|(id, _)| *id == self.id)+ .map(|index| sinks.remove(index).1)+ };+ }+}+impl log::Log for StreamLogger {fn enabled(&self, metadata: &log::Metadata) -> bool {metadata.level() <= log::Level::Info@@ -37,7 +72,7 @@ impl log::Log for StreamLogger {log::Level::Debug | log::Level::Trace => return,};let mut sinks = SINKS.lock().unwrap();- while let Some(sink) = sinks.first() {+ while let Some((_, sink)) = sinks.first() {if sink.add(LogEntry {level,@@ -61,8 +96,3 @@ pub(crate) fn install() {log::set_max_level(log::LevelFilter::Info);});}--pub fn attach_log_stream(sink: StreamSink<LogEntry>) {- SINKS.lock().unwrap().push(sink);- log::set_max_level(log::LevelFilter::Info);-}diff --git a/rust/bindings/frb/photos/src/api/log.rs b/rust/bindings/frb/photos/src/api/log.rsindex 7953c91844..c2affcd727 100644--- a/rust/bindings/frb/photos/src/api/log.rs+++ b/rust/bindings/frb/photos/src/api/log.rs@@ -1,5 +1,8 @@+use std::sync::atomic::{AtomicU64, Ordering};use std::sync::{Mutex, Once};+use flutter_rust_bridge::frb;+use crate::frb_generated::StreamSink;#[derive(Clone, Copy)]@@ -15,12 +18,44 @@ pub struct LogEntry {pub message: String,}-static SINKS: Mutex<Vec<StreamSink<LogEntry>>> = Mutex::new(Vec::new());+static NEXT_SINK_ID: AtomicU64 = AtomicU64::new(0);+static SINKS: Mutex<Vec<(u64, StreamSink<LogEntry>)>> = Mutex::new(Vec::new());static LOGGER: StreamLogger = StreamLogger;static LOGGER_INIT: Once = Once::new();struct StreamLogger;+#[frb(opaque)]+pub struct LogSinkGuard {+ id: u64,+}++impl LogSinkGuard {+ #[frb(sync)]+ pub fn new() -> Self {+ Self {+ id: NEXT_SINK_ID.fetch_add(1, Ordering::Relaxed),+ }+ }++ pub fn attach_log_stream(&self, sink: StreamSink<LogEntry>) {+ SINKS.lock().unwrap().push((self.id, sink));+ log::set_max_level(log::LevelFilter::Info);+ }+}++impl Drop for LogSinkGuard {+ fn drop(&mut self) {+ let _sink = {+ let mut sinks = SINKS.lock().unwrap();+ sinks+ .iter()+ .position(|(id, _)| *id == self.id)+ .map(|index| sinks.remove(index).1)+ };+ }+}+impl log::Log for StreamLogger {fn enabled(&self, metadata: &log::Metadata) -> bool {metadata.level() <= log::Level::Info@@ -37,7 +72,7 @@ impl log::Log for StreamLogger {log::Level::Debug | log::Level::Trace => return,};let mut sinks = SINKS.lock().unwrap();- while let Some(sink) = sinks.first() {+ while let Some((_, sink)) = sinks.first() {if sink.add(LogEntry {level,@@ -61,8 +96,3 @@ pub(crate) fn install() {log::set_max_level(log::LevelFilter::Info);});}--pub fn attach_log_stream(sink: StreamSink<LogEntry>) {- SINKS.lock().unwrap().push(sink);- log::set_max_level(log::LevelFilter::Info);-}