From 42a81e8695330a57ea7a51de69e71a893e5d8db9 Mon Sep 17 00:00:00 2001 From: neuronull <9162534+neuronull@users.noreply.github.com> Date: Wed, 19 Nov 2025 10:31:05 -0700 Subject: [PATCH] Replace log with tracing for ios and android uniffi --- Cargo.lock | 70 +++++++---------- crates/bitwarden-uniffi/Cargo.toml | 8 +- .../bitwarden-uniffi/src/android_support.rs | 15 ++-- crates/bitwarden-uniffi/src/lib.rs | 75 +++++++++++++++---- 4 files changed, 100 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2dd888421..f60ef21d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,20 +97,9 @@ checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" [[package]] name = "android_log-sys" -version = "0.3.2" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84521a3cf562bc62942e294181d9eef17eb38ceb8c68677bc49f144e4c3d4f8d" - -[[package]] -name = "android_logger" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f39be698127218cca460cb624878c9aa4e2b47dba3b277963d2bf00bad263b" -dependencies = [ - "android_log-sys", - "env_filter", - "log", -] +checksum = "85965b6739a430150bdd138e2374a98af0c3ee0d030b3bb7fc3bddff58d0102e" [[package]] name = "android_system_properties" @@ -893,7 +882,6 @@ dependencies = [ name = "bitwarden-uniffi" version = "0.1.0" dependencies = [ - "android_logger", "async-trait", "bitwarden-collections", "bitwarden-core", @@ -909,13 +897,14 @@ dependencies = [ "bitwarden-uniffi-error", "bitwarden-vault", "chrono", - "env_logger", "jni", "libloading", - "log", - "oslog", "rustls-platform-verifier", "thiserror 2.0.12", + "tracing", + "tracing-android", + "tracing-oslog", + "tracing-subscriber", "uniffi", "uuid", ] @@ -1762,19 +1751,6 @@ dependencies = [ "syn", ] -[[package]] -name = "dashmap" -version = "5.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" -dependencies = [ - "cfg-if", - "hashbrown 0.14.5", - "lock_api", - "once_cell", - "parking_lot_core", -] - [[package]] name = "data-encoding" version = "2.9.0" @@ -3418,17 +3394,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" -[[package]] -name = "oslog" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80d2043d1f61d77cb2f4b1f7b7b2295f40507f5f8e9d1c8bf10a1ca5f97a3969" -dependencies = [ - "cc", - "dashmap", - "log", -] - [[package]] name = "owo-colors" version = "4.2.1" @@ -5313,6 +5278,17 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-android" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12612be8f868a09c0ceae7113ff26afe79d81a24473a393cb9120ece162e86c0" +dependencies = [ + "android_log-sys", + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.30" @@ -5355,6 +5331,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-oslog" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76902d2a8d5f9f55a81155c08971734071968c90f2d9bfe645fe700579b2950" +dependencies = [ + "cc", + "cfg-if", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "tracing-subscriber" version = "0.3.20" diff --git a/crates/bitwarden-uniffi/Cargo.toml b/crates/bitwarden-uniffi/Cargo.toml index 82aa7a7bb..935fac93b 100644 --- a/crates/bitwarden-uniffi/Cargo.toml +++ b/crates/bitwarden-uniffi/Cargo.toml @@ -34,14 +34,14 @@ bitwarden-state = { workspace = true, features = ["uniffi"] } bitwarden-uniffi-error = { workspace = true } bitwarden-vault = { workspace = true, features = ["uniffi"] } chrono = { workspace = true, features = ["std"] } -env_logger = "0.11.1" -log = { workspace = true } thiserror = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } uniffi = { workspace = true } uuid = { workspace = true } [target.'cfg(target_os = "android")'.dependencies] -android_logger = "0.15" +tracing-android = "0.2.0" # The use of rustls-platform-verifier requires some extra support to communicate with the Android platform jni = ">=0.21, <0.22" @@ -49,7 +49,7 @@ libloading = ">=0.8.1, <0.9" rustls-platform-verifier = "0.6.0" [target.'cfg(target_os = "ios")'.dependencies] -oslog = "0.2.0" +tracing-oslog = "0.3.0" [build-dependencies] uniffi = { workspace = true, features = ["build"] } diff --git a/crates/bitwarden-uniffi/src/android_support.rs b/crates/bitwarden-uniffi/src/android_support.rs index 12ebc38df..b65df94c2 100644 --- a/crates/bitwarden-uniffi/src/android_support.rs +++ b/crates/bitwarden-uniffi/src/android_support.rs @@ -1,6 +1,7 @@ use std::{error::Error, sync::OnceLock}; use jni::sys::{JavaVM, jint, jsize}; +use tracing::{error, info}; pub static JAVA_VM: OnceLock = OnceLock::new(); @@ -9,7 +10,7 @@ pub static JAVA_VM: OnceLock = OnceLock::new(); #[allow(non_snake_case)] #[unsafe(no_mangle)] pub extern "system" fn JNI_OnLoad(vm_ptr: jni::JavaVM, _reserved: *mut std::ffi::c_void) -> jint { - log::info!("JNI_OnLoad initializing"); + info!("JNI_OnLoad initializing"); JAVA_VM.get_or_init(|| vm_ptr); jni::sys::JNI_VERSION_1_6 } @@ -18,25 +19,25 @@ pub fn init() { fn init_inner() -> Result<(), Box> { let jvm = match JAVA_VM.get() { Some(jvm) => { - log::info!("JavaVM already initialized"); + info!("JavaVM already initialized"); jvm } None => { - log::info!("JavaVM not initialized, initializing now"); + info!("JavaVM not initialized, initializing now"); let jvm = java_vm()?; JAVA_VM.get_or_init(|| jvm) } }; let mut env = jvm.attach_current_thread_permanently()?; - log::info!("Initializing Android verifier"); + info!("Initializing Android verifier"); init_verifier(&mut env)?; - log::info!("SDK Android support initialized"); + info!("SDK Android support initialized"); Ok(()) } - if let Err(e) = init_inner() { - log::error!("Failed to initialize Android support: {:#?}", e); + if let Err(error) = init_inner() { + error!(%error, "Failed to initialize Android support"); } } diff --git a/crates/bitwarden-uniffi/src/lib.rs b/crates/bitwarden-uniffi/src/lib.rs index 9e2244dd8..f16325763 100644 --- a/crates/bitwarden-uniffi/src/lib.rs +++ b/crates/bitwarden-uniffi/src/lib.rs @@ -2,7 +2,7 @@ uniffi::setup_scaffolding!(); -use std::sync::Arc; +use std::sync::{Arc, Once}; use auth::AuthClient; use bitwarden_core::{ClientSettings, client::internal::ClientManagedTokens}; @@ -111,22 +111,65 @@ impl Client { } } +static INIT: Once = Once::new(); + fn init_logger() { - #[cfg(not(any(target_os = "android", target_os = "ios")))] - let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) - .try_init(); - - #[cfg(target_os = "ios")] - let _ = oslog::OsLogger::new("com.8bit.bitwarden") - .level_filter(log::LevelFilter::Info) - .init(); - - #[cfg(target_os = "android")] - android_logger::init_once( - android_logger::Config::default() - .with_tag("com.bitwarden.sdk") - .with_max_level(log::LevelFilter::Info), - ); + use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _}; + + INIT.call_once(|| { + // the log level prioritization is determined by: + // 1. if RUST_LOG is detected at runtime + // 2. if RUST_LOG is provided at compile time + // 3. default to INFO + let filter = EnvFilter::builder() + .with_default_directive( + option_env!("RUST_LOG") + .unwrap_or("info") + .parse() + .expect("should provide valid log level at compile time."), + ) + .from_env_lossy(); + + let fmtlayer = tracing_subscriber::fmt::layer() + .with_ansi(true) + .with_file(true) + .with_line_number(true) + .with_target(true) + .pretty(); + + #[cfg(target_os = "ios")] + { + const TAG: &str = "com.8bit.bitwarden"; + + tracing_subscriber::registry() + .with(fmtlayer) + .with(filter) + .with(tracing_oslog::OsLogger::new(TAG, "default")) + .init(); + } + + #[cfg(target_os = "android")] + { + const TAG: &str = "com.bitwarden.sdk"; + + tracing_subscriber::registry() + .with(fmtlayer) + .with(filter) + .with( + tracing_android::layer(TAG) + .expect("initialization of android logcat tracing layer"), + ) + .init(); + } + + #[cfg(not(any(target_os = "android", target_os = "ios")))] + { + tracing_subscriber::registry() + .with(fmtlayer) + .with(filter) + .init(); + } + }); } /// Setup the error converter to ensure conversion errors don't cause panics