Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Keep FRB logging attached across isolates

Manav Rathid10a89ce5e59
diff --git a/rust/bindings/frb/ente-rust/src/api/log.rs b/rust/bindings/frb/ente-rust/src/api/log.rs
index dacff00484..7953c91844 100644
--- a/rust/bindings/frb/ente-rust/src/api/log.rs
+++ b/rust/bindings/frb/ente-rust/src/api/log.rs
@@ -1,11 +1,8 @@
-// TODO: On FRB 2.13, replace this module and the Dart attachment with
-// `enable_frb_rust_to_dart_logging!(setup_dart_logging_output = false)`.
-// `init_app` must then call `setup_backtrace`, not `setup_default_user_utils`.
-
-use std::sync::{Once, RwLock};
+use std::sync::{Mutex, Once};
use crate::frb_generated::StreamSink;
+#[derive(Clone, Copy)]
pub enum LogLevel {
Error,
Warn,
@@ -18,7 +15,7 @@ pub struct LogEntry {
pub message: String,
}
-static SINK: RwLock<Option<StreamSink<LogEntry>>> = RwLock::new(None);
+static SINKS: Mutex<Vec<StreamSink<LogEntry>>> = Mutex::new(Vec::new());
static LOGGER: StreamLogger = StreamLogger;
static LOGGER_INIT: Once = Once::new();
@@ -39,12 +36,19 @@ impl log::Log for StreamLogger {
log::Level::Info => LogLevel::Info,
log::Level::Debug | log::Level::Trace => return,
};
- if let Some(sink) = SINK.read().unwrap().as_ref() {
- let _ = sink.add(LogEntry {
- level,
- target: record.target().to_string(),
- message: record.args().to_string(),
- });
+ let mut sinks = SINKS.lock().unwrap();
+ while let Some(sink) = sinks.first() {
+ if sink
+ .add(LogEntry {
+ level,
+ target: record.target().to_string(),
+ message: record.args().to_string(),
+ })
+ .is_ok()
+ {
+ break;
+ }
+ sinks.remove(0);
}
}
@@ -59,7 +63,6 @@ pub(crate) fn install() {
}
pub fn attach_log_stream(sink: StreamSink<LogEntry>) {
- *SINK.write().unwrap() = Some(sink);
- // FRB resets the process-wide maximum after this logger is installed.
+ 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.rs
index dacff00484..7953c91844 100644
--- a/rust/bindings/frb/photos/src/api/log.rs
+++ b/rust/bindings/frb/photos/src/api/log.rs
@@ -1,11 +1,8 @@
-// TODO: On FRB 2.13, replace this module and the Dart attachment with
-// `enable_frb_rust_to_dart_logging!(setup_dart_logging_output = false)`.
-// `init_app` must then call `setup_backtrace`, not `setup_default_user_utils`.
-
-use std::sync::{Once, RwLock};
+use std::sync::{Mutex, Once};
use crate::frb_generated::StreamSink;
+#[derive(Clone, Copy)]
pub enum LogLevel {
Error,
Warn,
@@ -18,7 +15,7 @@ pub struct LogEntry {
pub message: String,
}
-static SINK: RwLock<Option<StreamSink<LogEntry>>> = RwLock::new(None);
+static SINKS: Mutex<Vec<StreamSink<LogEntry>>> = Mutex::new(Vec::new());
static LOGGER: StreamLogger = StreamLogger;
static LOGGER_INIT: Once = Once::new();
@@ -39,12 +36,19 @@ impl log::Log for StreamLogger {
log::Level::Info => LogLevel::Info,
log::Level::Debug | log::Level::Trace => return,
};
- if let Some(sink) = SINK.read().unwrap().as_ref() {
- let _ = sink.add(LogEntry {
- level,
- target: record.target().to_string(),
- message: record.args().to_string(),
- });
+ let mut sinks = SINKS.lock().unwrap();
+ while let Some(sink) = sinks.first() {
+ if sink
+ .add(LogEntry {
+ level,
+ target: record.target().to_string(),
+ message: record.args().to_string(),
+ })
+ .is_ok()
+ {
+ break;
+ }
+ sinks.remove(0);
}
}
@@ -59,7 +63,6 @@ pub(crate) fn install() {
}
pub fn attach_log_stream(sink: StreamSink<LogEntry>) {
- *SINK.write().unwrap() = Some(sink);
- // FRB resets the process-wide maximum after this logger is installed.
+ SINKS.lock().unwrap().push(sink);
log::set_max_level(log::LevelFilter::Info);
}