• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From cd85e49c438e3aa9dbe2989f91e5ad7d3f4816de Mon Sep 17 00:00:00 2001
2From: Marcin Radomski <dextero@google.com>
3Date: Thu, 17 Aug 2023 16:11:56 +0000
4Subject: [PATCH] Enable default-initializing liblog_rust to write to logcat on
5 Android
6
7Add default_log_impl cfg that, when enabled, makes `liblog_rust` use
8`android_logger` instead of `NopLogger` by default.
9
10This makes it possible to embed android_logger as mod inside liblog_rust
11crate, so that AndroidLogger can be used as default logger instead of a
12NopLogger.
13
14Changing that default prevents dropping logs when the logger is
15uninitialized. This can happen by accident when an application doesn't
16intialize the logger in all linker namespaces it pulls libraries from.
17See discussion at b/294216366#comment7.
18
19Bug: 275290559
20Test: compile test app from aosp/2717614
21Test: run it on a Cuttlefish device
22Test: observe logcat logs on all level from FFI call
23Test: observe all logs on non-FFI call without initializing the logger
24Test: observe set log filter applying only to non-FFI call
25Change-Id: I04dd334c66e5a2be8cfb19e87be3afb9146e5aa6
26---
27 Android.bp            | 12 ++++++++++++
28 src/android_logger.rs |  1 +
29 src/lib.rs            | 23 +++++++++++++++++++++++
30 3 files changed, 36 insertions(+)
31 create mode 120000 src/android_logger.rs
32
33diff --git a/Android.bp b/Android.bp
34index e6ff3cf..81c9175 100644
35--- a/Android.bp
36+++ b/Android.bp
37@@ -60,6 +60,18 @@ rust_library {
38     product_available: true,
39     vendor_available: true,
40     min_sdk_version: "29",
41+    target: {
42+      android: {
43+        cfgs: ["default_log_impl"],
44+        rustlibs: [
45+          "libandroid_log_sys",
46+          "libonce_cell",
47+        ],
48+        shared_libs: [
49+          "liblog",
50+        ],
51+      }
52+    }
53 }
54
55 rust_library_rlib {
56diff --git a/src/android_logger.rs b/src/android_logger.rs
57new file mode 120000
58index 0000000..84b8625
59--- /dev/null
60+++ b/src/android_logger.rs
61@@ -0,0 +1 @@
62+../../android_logger/src/lib.rs
63\ No newline at end of file
64diff --git a/src/lib.rs b/src/lib.rs
65index 4ead826..8eb1c50 100644
66--- a/src/lib.rs
67+++ b/src/lib.rs
68@@ -344,6 +344,11 @@ mod serde;
69 #[cfg(feature = "kv_unstable")]
70 pub mod kv;
71
72+#[cfg(default_log_impl)]
73+extern crate once_cell;
74+#[cfg(default_log_impl)]
75+mod android_logger;
76+
77 #[cfg(has_atomics)]
78 use std::sync::atomic::{AtomicUsize, Ordering};
79
80@@ -405,7 +410,10 @@ const UNINITIALIZED: usize = 0;
81 const INITIALIZING: usize = 1;
82 const INITIALIZED: usize = 2;
83
84+#[cfg(not(default_log_impl))]
85 static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
86+#[cfg(default_log_impl)]
87+static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
88
89 static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
90
91@@ -1572,6 +1580,21 @@ impl error::Error for ParseLevelError {}
92 /// If a logger has not been set, a no-op implementation is returned.
93 pub fn logger() -> &'static dyn Log {
94     if STATE.load(Ordering::SeqCst) != INITIALIZED {
95+        #[cfg(default_log_impl)]
96+        {
97+            // On Android, default to logging to logcat if not explicitly initialized. This
98+            // prevents logs from being dropped by default, which may happen unexpectedly in case
99+            // of using libraries from multiple linker namespaces and failing to initialize the
100+            // logger in each namespace. See b/294216366#comment7.
101+            use android_logger::{AndroidLogger, Config};
102+            use std::sync::OnceLock;
103+            static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
104+            return
105+                ANDROID_LOGGER.get_or_init(|| {
106+                    // Pass all logs down to liblog - it does its own filtering.
107+                    AndroidLogger::new(Config::default().with_max_level(LevelFilter::Trace))
108+                });
109+        }
110         static NOP: NopLogger = NopLogger;
111         &NOP
112     } else {
113--
1142.42.0.rc1.204.g551eb34607-goog
115
116