Skip to content

armgit / ente

Public Imported from GitHub
0

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

Share the UniFFI log adapter

Manav Rathibbdcd2dfb294
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 11032a84ae..4ab691a439 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2360,7 +2360,7 @@ name = "ente-cast-uniffi"
version = "0.0.0"
dependencies = [
"ente-core",
- "log",
+ "ente-uniffi-log",
"thiserror 2.0.18",
"uniffi",
]
@@ -2447,7 +2447,7 @@ version = "0.0.0"
dependencies = [
"ente-assets",
"ente-ensu",
- "log",
+ "ente-uniffi-log",
"thiserror 2.0.18",
"uniffi",
"uuid",
@@ -2585,6 +2585,13 @@ dependencies = [
"uuid",
]
+[[package]]
+name = "ente-uniffi-log"
+version = "0.0.0"
+dependencies = [
+ "log",
+]
+
[[package]]
name = "ente-wasm"
version = "0.0.0"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 397ec04147..3d2aa3a858 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -9,6 +9,7 @@ members = [
"bindings/napi/photos",
"bindings/uniffi/cast",
"bindings/uniffi/ensu",
+ "bindings/uniffi/log",
"bindings/wasm/ente-wasm",
"bindings/wasm/core",
"bindings/wasm/log",
@@ -36,6 +37,7 @@ ente-paste = { path = "crates/paste" }
ente-photos = { path = "crates/photos" }
ente-space = { path = "crates/space" }
ente-test-support = { path = "crates/test-support" }
+ente-uniffi-log = { path = "bindings/uniffi/log" }
ente-wasm-log = { path = "bindings/wasm/log" }
flutter_rust_bridge = "=2.12.0"
flutter_rust_bridge_codegen = "=2.12.0"
diff --git a/rust/bindings/uniffi/cast/Cargo.toml b/rust/bindings/uniffi/cast/Cargo.toml
index 7c2efe461d..3b7ccda7eb 100644
--- a/rust/bindings/uniffi/cast/Cargo.toml
+++ b/rust/bindings/uniffi/cast/Cargo.toml
@@ -8,6 +8,6 @@ crate-type = ["staticlib", "cdylib"]
[dependencies]
ente-core.workspace = true
-log.workspace = true
+ente-uniffi-log.workspace = true
thiserror.workspace = true
uniffi.workspace = true
diff --git a/rust/bindings/uniffi/cast/src/log.rs b/rust/bindings/uniffi/cast/src/log.rs
index cd14ff267d..d982f97e49 100644
--- a/rust/bindings/uniffi/cast/src/log.rs
+++ b/rust/bindings/uniffi/cast/src/log.rs
@@ -1,47 +1 @@
-#[derive(uniffi::Enum)]
-pub enum RustLogLevel {
- Error,
- Warn,
- Info,
-}
-
-#[uniffi::export(callback_interface)]
-pub trait RustLogSink: Send + Sync {
- fn log(&self, level: RustLogLevel, target: String, message: String);
-}
-
-struct UniffiLogger {
- sink: Box<dyn RustLogSink>,
-}
-
-impl log::Log for UniffiLogger {
- fn enabled(&self, metadata: &log::Metadata) -> bool {
- metadata.level() <= log::Level::Info
- }
-
- fn log(&self, record: &log::Record) {
- if !self.enabled(record.metadata()) {
- return;
- }
- let level = match record.level() {
- log::Level::Error => RustLogLevel::Error,
- log::Level::Warn => RustLogLevel::Warn,
- log::Level::Info => RustLogLevel::Info,
- log::Level::Debug | log::Level::Trace => return,
- };
- self.sink.log(
- level,
- record.target().to_string(),
- record.args().to_string(),
- );
- }
-
- fn flush(&self) {}
-}
-
-#[uniffi::export]
-pub fn init_rust_logging(sink: Box<dyn RustLogSink>) {
- log::set_boxed_logger(Box::new(UniffiLogger { sink }))
- .expect("Rust logger already initialized");
- log::set_max_level(log::LevelFilter::Info);
-}
+ente_uniffi_log::setup!();
diff --git a/rust/bindings/uniffi/ensu/Cargo.toml b/rust/bindings/uniffi/ensu/Cargo.toml
index 75d3e192cf..d4e718e951 100644
--- a/rust/bindings/uniffi/ensu/Cargo.toml
+++ b/rust/bindings/uniffi/ensu/Cargo.toml
@@ -9,7 +9,7 @@ crate-type = ["staticlib", "cdylib"]
[dependencies]
ente-assets.workspace = true
ente-ensu = { workspace = true, features = ["transcription"] }
-log.workspace = true
+ente-uniffi-log.workspace = true
thiserror.workspace = true
uniffi = { workspace = true, features = ["tokio"] }
uuid = { version = "1" }
diff --git a/rust/bindings/uniffi/ensu/src/log.rs b/rust/bindings/uniffi/ensu/src/log.rs
index cd14ff267d..d982f97e49 100644
--- a/rust/bindings/uniffi/ensu/src/log.rs
+++ b/rust/bindings/uniffi/ensu/src/log.rs
@@ -1,47 +1 @@
-#[derive(uniffi::Enum)]
-pub enum RustLogLevel {
- Error,
- Warn,
- Info,
-}
-
-#[uniffi::export(callback_interface)]
-pub trait RustLogSink: Send + Sync {
- fn log(&self, level: RustLogLevel, target: String, message: String);
-}
-
-struct UniffiLogger {
- sink: Box<dyn RustLogSink>,
-}
-
-impl log::Log for UniffiLogger {
- fn enabled(&self, metadata: &log::Metadata) -> bool {
- metadata.level() <= log::Level::Info
- }
-
- fn log(&self, record: &log::Record) {
- if !self.enabled(record.metadata()) {
- return;
- }
- let level = match record.level() {
- log::Level::Error => RustLogLevel::Error,
- log::Level::Warn => RustLogLevel::Warn,
- log::Level::Info => RustLogLevel::Info,
- log::Level::Debug | log::Level::Trace => return,
- };
- self.sink.log(
- level,
- record.target().to_string(),
- record.args().to_string(),
- );
- }
-
- fn flush(&self) {}
-}
-
-#[uniffi::export]
-pub fn init_rust_logging(sink: Box<dyn RustLogSink>) {
- log::set_boxed_logger(Box::new(UniffiLogger { sink }))
- .expect("Rust logger already initialized");
- log::set_max_level(log::LevelFilter::Info);
-}
+ente_uniffi_log::setup!();
diff --git a/rust/bindings/uniffi/log/Cargo.toml b/rust/bindings/uniffi/log/Cargo.toml
new file mode 100644
index 0000000000..bf656b8cde
--- /dev/null
+++ b/rust/bindings/uniffi/log/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "ente-uniffi-log"
+edition = "2024"
+
+[dependencies]
+log = { workspace = true, features = ["std"] }
diff --git a/rust/bindings/uniffi/log/src/lib.rs b/rust/bindings/uniffi/log/src/lib.rs
new file mode 100644
index 0000000000..4313ac670a
--- /dev/null
+++ b/rust/bindings/uniffi/log/src/lib.rs
@@ -0,0 +1,55 @@
+#[macro_export]
+macro_rules! setup {
+ () => {
+ #[derive(uniffi::Enum)]
+ pub enum RustLogLevel {
+ Error,
+ Warn,
+ Info,
+ }
+
+ #[uniffi::export(callback_interface)]
+ pub trait RustLogSink: Send + Sync {
+ fn log(&self, level: RustLogLevel, target: String, message: String);
+ }
+
+ #[uniffi::export]
+ pub fn init_rust_logging(sink: Box<dyn RustLogSink>) {
+ $crate::__log::set_boxed_logger(Box::new(UniffiLogger { sink }))
+ .expect("Rust logger already initialized");
+ $crate::__log::set_max_level($crate::__log::LevelFilter::Info);
+ }
+
+ struct UniffiLogger {
+ sink: Box<dyn RustLogSink>,
+ }
+
+ impl $crate::__log::Log for UniffiLogger {
+ fn enabled(&self, metadata: &$crate::__log::Metadata) -> bool {
+ metadata.level() <= $crate::__log::Level::Info
+ }
+
+ fn log(&self, record: &$crate::__log::Record) {
+ if !self.enabled(record.metadata()) {
+ return;
+ }
+ let level = match record.level() {
+ $crate::__log::Level::Error => RustLogLevel::Error,
+ $crate::__log::Level::Warn => RustLogLevel::Warn,
+ $crate::__log::Level::Info => RustLogLevel::Info,
+ $crate::__log::Level::Debug | $crate::__log::Level::Trace => return,
+ };
+ self.sink.log(
+ level,
+ record.target().to_string(),
+ record.args().to_string(),
+ );
+ }
+
+ fn flush(&self) {}
+ }
+ };
+}
+
+#[doc(hidden)]
+pub use log as __log;