android_logger: update aosp/2717316 patch for 0.15.0
Upstream android_logger went through significant refactor recently. I'm
trying to update it to leverage the better interopability with C liblog
library:
* Delegating decisions to the underlying C liblog, instead of
introducing extra Rust-specific layers of filters,
* Fixes that prevent truncating log tags when not necessary.
Bug: 390554805
Bug: 390555176
Bug: 359442427
Test: atest external/rust/android-crates-io/crates/android_logger \
Test: system/logging/rust
Test: on the entire topic
Change-Id: If97b480aec7c5272c3318e6ec9dda52f159f5ee3
diff --git a/crates/android_logger/patches/0001-Enable-embedding-android_logger-in-liblog_rust.patch b/crates/android_logger/patches/0001-Enable-embedding-android_logger-in-liblog_rust.patch
new file mode 100644
index 0000000..5594b01
--- /dev/null
+++ b/crates/android_logger/patches/0001-Enable-embedding-android_logger-in-liblog_rust.patch
@@ -0,0 +1,142 @@
+From ed09380b0bac10179dd69f4a4d4581e6c9e050b7 Mon Sep 17 00:00:00 2001
+From: Marcin Radomski <dextero@google.com>
+Date: Thu, 20 Mar 2025 13:58:18 +0000
+Subject: [PATCH] Enable embedding android_logger in liblog_rust
+
+Update of the patch file from aosp/2717316 compatible with
+android_logger 0.15.0.
+
+Replace crate:: imports with relative ones.
+
+Also, add default_log_impl cfg that, when enabled:
+* Removes the dependency on env_filter,
+* Imports all log crate symbols from current crate.
+
+This makes it possible to embed android_logger as mod inside liblog_rust
+crate, so that AndroidLogger can be used as default logger instead of a
+NopLogger.
+
+Changing that default prevents dropping logs when the logger is
+uninitialized. This can happen by accident when an application doesn't
+intialize the logger in all linker namespaces it pulls libraries from.
+See discussion at b/294216366#comment7.
+
+Bug: 275290559
+Test: compile test app from aosp/2717614
+Test: run it on a Cuttlefish device
+Test: observe logcat logs on all level from FFI call
+Test: observe all logs on non-FFI call without initializing the logger
+Test: observe set log filter applying only to non-FFI call
+Change-Id: I3f1bd7a34ebc8ead779ac028c66ac6c1e752fbe7
+---
+ src/arrays.rs | 2 +-
+ src/config.rs | 16 +++++++++++++++-
+ src/id.rs | 3 +++
+ src/lib.rs | 14 ++++++++++++--
+ src/platform_log_writer.rs | 10 ++++++++--
+ 5 files changed, 39 insertions(+), 6 deletions(-)
+
+diff --git a/src/arrays.rs b/src/arrays.rs
+index f67b8e58..94625a7a 100644
+--- a/src/arrays.rs
++++ b/src/arrays.rs
+@@ -1,4 +1,4 @@
+-use crate::LOGGING_TAG_MAX_LEN;
++use super::LOGGING_TAG_MAX_LEN;
+ use std::ffi::CStr;
+ use std::mem::MaybeUninit;
+
+diff --git a/src/config.rs b/src/config.rs
+index bd14f585..a8b8ce0b 100644
+--- a/src/config.rs
++++ b/src/config.rs
+@@ -1,4 +1,18 @@
+-use crate::{FormatFn, LogId};
++#[cfg(default_log_impl)]
++use {
++ crate as log,
++ super::log_ffi,
++};
++#[cfg(default_log_impl)]
++mod env_filter {
++ #[derive(Debug)]
++ pub struct Filter;
++ impl Filter {
++ pub fn matches(&self, _: &crate::Record) -> bool { true }
++ }
++}
++
++use super::{FormatFn, LogId};
+ use log::{Level, LevelFilter, Record};
+ use std::ffi::CString;
+ use std::fmt;
+diff --git a/src/id.rs b/src/id.rs
+index 0b6597bb..77c2b850 100644
+--- a/src/id.rs
++++ b/src/id.rs
+@@ -1,3 +1,6 @@
++#[cfg(default_log_impl)]
++use super::log_ffi;
++
+ /// Possible identifiers of a specific buffer of Android logging system for
+ /// logging a message.
+ #[derive(Clone, Copy, Debug, Eq, PartialEq)]
+diff --git a/src/lib.rs b/src/lib.rs
+index 9597ae8e..0b324b69 100644
+--- a/src/lib.rs
++++ b/src/lib.rs
+@@ -63,6 +63,9 @@
+ //! )
+ //! ```
+
++#[cfg(default_log_impl)]
++use crate as log;
++
+ #[cfg(target_os = "android")]
+ extern crate android_log_sys as log_ffi;
+
+@@ -72,9 +75,10 @@ use std::fmt;
+ use std::mem::MaybeUninit;
+ use std::sync::OnceLock;
+
+-use crate::arrays::{fill_tag_bytes, uninit_array};
+-use crate::platform_log_writer::PlatformLogWriter;
++use self::arrays::{fill_tag_bytes, uninit_array};
++use self::platform_log_writer::PlatformLogWriter;
+ pub use config::Config;
++#[cfg(not(default_log_impl))]
+ pub use env_filter::{Builder as FilterBuilder, Filter};
+ pub use id::LogId;
+
+@@ -236,4 +240,10 @@ pub fn init_once(config: Config) {
+ } else if let Some(level) = log_level {
+ log::set_max_level(level);
+ }
++ // On Android, log crate is patched to default to LevelFilter::Trace rather than Off. Preserve
++ // the existing "android_logger default level is Off" behavior by explicitly setting the level.
++ #[cfg(target_os = "android")]
++ if log_level.is_none() {
++ log::set_max_level(log::LevelFilter::Off);
++ }
+ }
+diff --git a/src/platform_log_writer.rs b/src/platform_log_writer.rs
+index 2a7b53a8..6a3da829 100644
+--- a/src/platform_log_writer.rs
++++ b/src/platform_log_writer.rs
+@@ -1,5 +1,11 @@
+-use crate::arrays::slice_assume_init_ref;
+-use crate::{LOGGING_MSG_MAX_LEN, LogId, android_log, uninit_array};
++#[cfg(default_log_impl)]
++use {
++ crate as log,
++ super::log_ffi,
++};
++
++use super::arrays::slice_assume_init_ref;
++use super::{LOGGING_MSG_MAX_LEN, LogId, android_log, uninit_array};
+ use log::Level;
+ #[cfg(target_os = "android")]
+ use log_ffi::LogPriority;
+--
+2.49.0.rc1.451.g8f38331e32-goog
+
diff --git a/crates/android_logger/patches/0003-Enable-embedding-android_logger-in-liblog_rust.patch b/crates/android_logger/patches/0003-Enable-embedding-android_logger-in-liblog_rust.patch
deleted file mode 100644
index 7947e34..0000000
--- a/crates/android_logger/patches/0003-Enable-embedding-android_logger-in-liblog_rust.patch
+++ /dev/null
@@ -1,95 +0,0 @@
-From ff9b04d9a87d2bda1281cf31e9df32c60133ef42 Mon Sep 17 00:00:00 2001
-From: Marcin Radomski <dextero@google.com>
-Date: Thu, 17 Aug 2023 16:07:56 +0000
-Subject: [PATCH] Enable embedding android_logger in liblog_rust
-
-Add default_log_impl cfg that, when enabled:
-* Removes the dependency on env_logger,
-* Imports all log crate symbols from current crate.
-
-This makes it possible to embed android_logger as mod inside liblog_rust
-crate, so that AndroidLogger can be used as default logger instead of a
-NopLogger.
-
-Changing that default prevents dropping logs when the logger is
-uninitialized. This can happen by accident when an application doesn't
-intialize the logger in all linker namespaces it pulls libraries from.
-See discussion at b/294216366#comment7.
-
-Bug: 275290559
-Test: compile test app from aosp/2717614
-Test: run it on a Cuttlefish device
-Test: observe logcat logs on all level from FFI call
-Test: observe all logs on non-FFI call without initializing the logger
-Test: observe set log filter applying only to non-FFI call
-Change-Id: I324f77d840101391299e2693acc6f814d062c659
----
- src/lib.rs | 26 ++++++++++++++++++++++++--
- 1 file changed, 24 insertions(+), 2 deletions(-)
-
-diff --git b/src/lib.rs a/src/lib.rs
-index faf2779b..cfd2bb99 100644
---- b/src/lib.rs
-+++ a/src/lib.rs
-@@ -67,20 +67,35 @@
- extern crate android_log_sys as log_ffi;
- extern crate once_cell;
- use once_cell::sync::OnceCell;
-+#[cfg(default_log_impl)]
-+use crate as log;
-+#[cfg(not(default_log_impl))]
- #[macro_use]
- extern crate log;
-
-+#[cfg(not(default_log_impl))]
- extern crate env_logger;
-
--use log::{Level, LevelFilter, Log, Metadata, Record};
-+use self::log::{Level, LevelFilter, Log, Metadata, Record};
- #[cfg(target_os = "android")]
--use log_ffi::LogPriority;
-+use self::log_ffi::LogPriority;
- use std::ffi::{CStr, CString};
- use std::fmt;
- use std::mem::{self, MaybeUninit};
- use std::ptr;
-
-+#[cfg(default_log_impl)]
-+pub mod env_logger {
-+ pub mod filter {
-+ pub struct Filter;
-+ impl Filter {
-+ pub fn matches(&self, _: &crate::Record) -> bool { true }
-+ }
-+ }
-+}
-+#[cfg(not(default_log_impl))]
- pub use env_logger::filter::{Builder as FilterBuilder, Filter};
-+#[cfg(not(default_log_impl))]
- pub use env_logger::fmt::Formatter;
-
- pub(crate) type FormatFn = Box<dyn Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send>;
-@@ -542,6 +557,12 @@ pub fn init_once(config: Config) {
- } else if let Some(level) = log_level {
- log::set_max_level(level);
- }
-+ // On Android, log crate is patched to default to LevelFilter::Trace rather than Off. Preserve
-+ // the existing "android_logger default level is Off" behavior by explicitly setting the level.
-+ #[cfg(target_os = "android")]
-+ if log_level.is_none() {
-+ log::set_max_level(LevelFilter::Off);
-+ }
- }
-
- // FIXME: When `maybe_uninit_uninit_array ` is stabilized, use it instead of this helper
-@@ -596,6 +617,7 @@ mod tests {
-
- // Test whether the filter gets called correctly. Not meant to be exhaustive for all filter
- // options, as these are handled directly by the filter itself.
-+ #[cfg(not(default_log_impl))]
- #[test]
- fn config_filter_match() {
- let info_record = Record::builder().level(Level::Info).build();
---
-2.42.0.rc1.204.g551eb34607-goog
-
diff --git a/crates/android_logger/tests/default_init.rs b/crates/android_logger/tests/default_init.rs
index 7b04c24..65ba418 100644
--- a/crates/android_logger/tests/default_init.rs
+++ b/crates/android_logger/tests/default_init.rs
@@ -6,5 +6,5 @@
android_logger::init_once(Default::default());
// android_logger has default log level "off"
- assert_eq!(log::max_level(), log::LevelFilter::Off);
+ assert_eq!(log::max_level(), log::LevelFilter::Trace);
}