logging.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //! FFI Logging configuration
  2. //!
  3. //! Provides functions to initialize tracing subscriber for stdout logging.
  4. use std::sync::Once;
  5. static INIT: Once = std::sync::Once::new();
  6. /// Initialize the tracing subscriber for stdout logging.
  7. ///
  8. /// This function sets up a tracing subscriber that outputs logs to stdout,
  9. /// making them visible when using the FFI from other languages.
  10. ///
  11. /// Call this function once at application startup, before creating
  12. /// any wallets. Subsequent calls are safe but have no effect.
  13. ///
  14. /// # Arguments
  15. ///
  16. /// * `level` - Log level filter (e.g., "debug", "info", "warn", "error", "trace")
  17. ///
  18. /// # Example (from Flutter/Dart)
  19. ///
  20. /// ```dart
  21. /// await CdkFfi.initLogging("debug");
  22. /// // Now all logs will be visible in stdout
  23. /// final wallet = await MultiMintWallet.create(...);
  24. /// ```
  25. #[uniffi::export]
  26. pub fn init_logging(level: String) {
  27. INIT.call_once(|| {
  28. #[cfg(target_os = "android")]
  29. {
  30. use android_logger::{Config, FilterBuilder};
  31. use log::LevelFilter;
  32. android_logger::init_once(
  33. Config::default()
  34. .with_max_level(LevelFilter::Trace)
  35. .with_tag("cdk")
  36. .with_format(|f, record| write!(f, "{}", record.args()))
  37. .with_filter(FilterBuilder::new().parse(&level).build()),
  38. );
  39. }
  40. #[cfg(not(target_os = "android"))]
  41. {
  42. use tracing_subscriber::{fmt, EnvFilter};
  43. let filter = EnvFilter::try_new(&level).unwrap_or_else(|_| EnvFilter::new("info"));
  44. fmt()
  45. .with_env_filter(filter)
  46. .with_target(true)
  47. .with_ansi(false)
  48. .init();
  49. }
  50. });
  51. }
  52. /// Initialize logging with default "info" level
  53. #[uniffi::export]
  54. pub fn init_default_logging() {
  55. init_logging("info".to_string());
  56. }