Procházet zdrojové kódy

feat(ffi): init_logging (#1529)

asmo před 1 týdnem
rodič
revize
0c02c577a7

+ 2 - 0
Cargo.lock

@@ -1346,6 +1346,8 @@ dependencies = [
  "serde_json",
  "thiserror 2.0.17",
  "tokio",
+ "tracing",
+ "tracing-subscriber",
  "uniffi",
  "url",
  "uuid",

+ 2 - 0
crates/cdk-ffi/Cargo.toml

@@ -30,6 +30,8 @@ uniffi = { version = "0.29", features = ["cli", "tokio"] }
 url = { workspace = true }
 uuid = { workspace = true, features = ["v4"] }
 cdk-common.workspace = true
+tracing.workspace = true
+tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
 
 
 [features]

+ 2 - 0
crates/cdk-ffi/src/lib.rs

@@ -7,6 +7,7 @@
 
 pub mod database;
 pub mod error;
+pub mod logging;
 pub mod multi_mint_wallet;
 #[cfg(feature = "postgres")]
 pub mod postgres;
@@ -17,6 +18,7 @@ pub mod wallet;
 
 pub use database::*;
 pub use error::*;
+pub use logging::*;
 pub use multi_mint_wallet::*;
 pub use types::*;
 pub use wallet::*;

+ 43 - 0
crates/cdk-ffi/src/logging.rs

@@ -0,0 +1,43 @@
+//! FFI Logging configuration
+//!
+//! Provides functions to initialize tracing subscriber for stdout logging.
+
+use std::sync::Once;
+
+static INIT: Once = std::sync::Once::new();
+
+/// Initialize the tracing subscriber for stdout logging.
+///
+/// This function sets up a tracing subscriber that outputs logs to stdout,
+/// making them visible when using the FFI from other languages.
+///
+/// Call this function once at application startup, before creating
+/// any wallets. Subsequent calls are safe but have no effect.
+///
+/// # Arguments
+///
+/// * `level` - Log level filter (e.g., "debug", "info", "warn", "error", "trace")
+///
+/// # Example (from Flutter/Dart)
+///
+/// ```dart
+/// await CdkFfi.initLogging("debug");
+/// // Now all logs will be visible in stdout
+/// final wallet = await MultiMintWallet.create(...);
+/// ```
+#[uniffi::export]
+pub fn init_logging(level: String) {
+    INIT.call_once(|| {
+        use tracing_subscriber::{fmt, EnvFilter};
+
+        let filter = EnvFilter::try_new(&level).unwrap_or_else(|_| EnvFilter::new("info"));
+
+        fmt().with_env_filter(filter).with_target(true).init();
+    });
+}
+
+/// Initialize logging with default "info" level
+#[uniffi::export]
+pub fn init_default_logging() {
+    init_logging("info".to_string());
+}

+ 1 - 1
crates/cdk-ffi/src/wallet.rs

@@ -39,7 +39,7 @@ impl Wallet {
         let m = Mnemonic::parse(&mnemonic)
             .map_err(|e| FfiError::InvalidMnemonic { msg: e.to_string() })?;
         let seed = m.to_seed_normalized("");
-
+        tracing::info!("creating ffi wallet");
         // Convert the FFI database trait to a CDK database implementation
         let localstore = crate::database::create_cdk_database_from_ffi(db);