aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2022-12-06 00:15:18 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-12-06 00:15:18 +0000
commit202498803ab393be90f87cb796ffb0a9b2469e6a (patch)
treecb6537aeb69d8331bbaa98c3395bac3b10846d64
parent7b6b7aaff02948c2751e466975f099ebf27fb3ae (diff)
parent35dcd503b723fac020eea1d2e58957c3f51cb402 (diff)
downloadandroid_logger-202498803ab393be90f87cb796ffb0a9b2469e6a.tar.gz
Update to 0.11.1 am: 33fb794bf7 am: 469c0d1289 am: 35dcd503b7
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/android_logger/+/2327733 Change-Id: Ia18f725a80213666ed27a832e25c9c877bfafe57 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--Cargo.toml2
-rw-r--r--Cargo.toml.orig2
-rw-r--r--METADATA8
-rw-r--r--patches/0001-Support-selecting-target-log-buffer.patch49
-rw-r--r--patches/0002-Use-older-API-to-avoid-requiring-API-v30.patch16
-rw-r--r--src/lib.rs95
-rw-r--r--tests/config_log_level.rs2
-rw-r--r--tests/default_init.rs2
-rw-r--r--tests/multiple_init.rs2
9 files changed, 100 insertions, 78 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 02cf115..1638036 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,7 +11,7 @@
[package]
name = "android_logger"
-version = "0.11.0"
+version = "0.11.1"
authors = ["The android_logger Developers"]
description = """
A logging implementation for `log` which hooks to android log output.
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index f092ceb..37c6496 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
[package]
name = "android_logger"
-version = "0.11.0"
+version = "0.11.1"
authors = ["The android_logger Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
diff --git a/METADATA b/METADATA
index 7f7dc13..fba05dd 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/android_logger/android_logger-0.11.0.crate"
+ value: "https://static.crates.io/crates/android_logger/android_logger-0.11.1.crate"
}
- version: "0.11.0"
+ version: "0.11.1"
license_type: NOTICE
last_upgrade_date {
year: 2022
- month: 4
- day: 21
+ month: 12
+ day: 5
}
}
diff --git a/patches/0001-Support-selecting-target-log-buffer.patch b/patches/0001-Support-selecting-target-log-buffer.patch
index 88ab283..6731ea8 100644
--- a/patches/0001-Support-selecting-target-log-buffer.patch
+++ b/patches/0001-Support-selecting-target-log-buffer.patch
@@ -1,20 +1,21 @@
-From 2bc2650d0a7a11a74670a6583b16aa6714d7c993 Mon Sep 17 00:00:00 2001
-From: Matthew Maurer <mmaurer@google.com>
-Date: Thu, 17 Feb 2022 20:23:37 +0000
-Subject: [PATCH] Support selecting target log buffer
+From eaa9cff3c99ed9e172c9d9fb369b70f84587f297 Mon Sep 17 00:00:00 2001
+From: Jeff Vander Stoep <jeffv@google.com>
+Date: Mon, 5 Dec 2022 12:38:24 +0100
+Subject: [PATCH 1/2] Support selecting target log buffer
Android has several different log buffers. Previously, this library
would only support logging to the "Main" log. Now, it logs to the
default log (which is Main for most processes), with the option to
override which log buffer you send messages to in the config.
-Change-Id: I72779e62bd963586e3dfad431cd82c75daf04d92
+Test: atest
+Change-Id: Id13515bb2cb042d1c31d4095444ae87cdcb13291
---
- src/lib.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++----------
- 1 file changed, 58 insertions(+), 13 deletions(-)
+ src/lib.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++----------
+ 1 file changed, 57 insertions(+), 13 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
-index c50ce97..e7a62ef 100644
+index ef2027a..4bcce0c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -85,21 +85,49 @@ pub use env_logger::fmt::Formatter;
@@ -83,7 +84,7 @@ index c50ce97..e7a62ef 100644
// If a custom tag is used, add the module path to the message.
// Use PlatformLogWriter to output chunks if they exceed max size.
-@@ -205,6 +233,7 @@ impl AndroidLogger {
+@@ -203,6 +231,7 @@ impl AndroidLogger {
#[derive(Default)]
pub struct Config {
log_level: Option<Level>,
@@ -91,7 +92,7 @@ index c50ce97..e7a62ef 100644
filter: Option<env_logger::filter::Filter>,
tag: Option<CString>,
custom_format: Option<FormatFn>,
-@@ -220,6 +249,15 @@ impl Config {
+@@ -218,6 +247,15 @@ impl Config {
self
}
@@ -107,16 +108,16 @@ index c50ce97..e7a62ef 100644
fn filter_matches(&self, record: &Record) -> bool {
if let Some(ref filter) = self.filter {
filter.matches(record)
-@@ -259,6 +297,8 @@ impl Config {
- struct PlatformLogWriter<'a> {
- #[cfg(target_os = "android")] priority: LogPriority,
- #[cfg(not(target_os = "android"))] priority: Level,
+@@ -259,6 +297,8 @@ pub struct PlatformLogWriter<'a> {
+ priority: LogPriority,
+ #[cfg(not(target_os = "android"))]
+ priority: Level,
+ #[cfg(target_os = "android")] log_id: log_ffi::log_id_t,
+ #[cfg(not(target_os = "android"))] log_id: Option<LogId>,
len: usize,
last_newline_index: usize,
tag: &'a CStr,
-@@ -267,7 +307,7 @@ struct PlatformLogWriter<'a> {
+@@ -267,7 +307,7 @@ pub struct PlatformLogWriter<'a> {
impl<'a> PlatformLogWriter<'a> {
#[cfg(target_os = "android")]
@@ -146,16 +147,16 @@ index c50ce97..e7a62ef 100644
len: 0,
last_newline_index: 0,
tag,
-@@ -344,7 +386,7 @@ impl<'a> PlatformLogWriter<'a> {
+@@ -345,7 +387,7 @@ impl<'a> PlatformLogWriter<'a> {
});
- let msg: &CStr = unsafe { CStr::from_ptr(mem::transmute(self.buffer.as_ptr())) };
+ let msg: &CStr = unsafe { CStr::from_ptr(self.buffer.as_ptr().cast()) };
- android_log(self.priority, self.tag, msg);
+ android_log(self.log_id, self.priority, self.tag, msg);
- *unsafe { self.buffer.get_unchecked_mut(len) } = last_byte;
+ unsafe { *self.buffer.get_unchecked_mut(len) = last_byte };
}
-@@ -441,9 +483,11 @@ mod tests {
+@@ -450,9 +492,11 @@ mod tests {
// Filter is checked in config_filter_match below.
let config = Config::default()
.with_min_level(Level::Trace)
@@ -167,7 +168,7 @@ index c50ce97..e7a62ef 100644
assert_eq!(config.tag, Some(CString::new("my_app").unwrap()));
}
-@@ -514,7 +558,7 @@ mod tests {
+@@ -523,7 +567,7 @@ mod tests {
fn platform_log_writer_init_values() {
let tag = CStr::from_bytes_with_nul(b"tag\0").unwrap();
@@ -176,13 +177,15 @@ index c50ce97..e7a62ef 100644
assert_eq!(writer.tag, tag);
// Android uses LogPriority instead, which doesn't implement equality checks
-@@ -613,6 +657,6 @@ mod tests {
+@@ -628,7 +672,7 @@ mod tests {
}
fn get_tag_writer() -> PlatformLogWriter<'static> {
- PlatformLogWriter::new(Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
+ PlatformLogWriter::new(None, Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
}
- }
+
+ unsafe fn assume_init_slice<T>(slice: &[MaybeUninit<T>]) -> &[T] {
--
-2.35.1.265.g69c8d7142f-goog
+2.39.0.rc0.267.gcb52ba06e7-goog
+
diff --git a/patches/0002-Use-older-API-to-avoid-requiring-API-v30.patch b/patches/0002-Use-older-API-to-avoid-requiring-API-v30.patch
index 721e9eb..44f08fb 100644
--- a/patches/0002-Use-older-API-to-avoid-requiring-API-v30.patch
+++ b/patches/0002-Use-older-API-to-avoid-requiring-API-v30.patch
@@ -1,20 +1,20 @@
-From ec84856e0f0bc5a307529122bfed3d94d2ef4011 Mon Sep 17 00:00:00 2001
-From: Matthew Maurer <mmaurer@google.com>
-Date: Thu, 24 Feb 2022 14:07:03 -0800
-Subject: [PATCH] Use older API to avoid requiring API v30
+From b5ee33076e1868c5946345b2cfb15a519b8c2577 Mon Sep 17 00:00:00 2001
+From: Jeff Vander Stoep <jeffv@google.com>
+Date: Mon, 5 Dec 2022 12:42:22 +0100
+Subject: [PATCH 2/2] Use older API to avoid requiring API v30
Test: Check that keystore still outputs logs to system
Bug: 221185310
-Change-Id: I25174f1617557e270db70cd432cec78c037c6b75
+Change-Id: If81d66cb145cbb41b4338fd64ac024d77243482e
---
src/lib.rs | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
-index d21be3f..bc4fa61 100644
+index 4bcce0c..59f942b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
-@@ -113,17 +113,11 @@ impl LogId {
+@@ -111,17 +111,11 @@ impl LogId {
/// Output log to android system.
#[cfg(target_os = "android")]
fn android_log(log_id: log_ffi::log_id_t, prio: log_ffi::LogPriority, tag: &CStr, msg: &CStr) {
@@ -37,5 +37,5 @@ index d21be3f..bc4fa61 100644
}
--
-2.35.1.574.g5d30c73bfb-goog
+2.39.0.rc0.267.gcb52ba06e7-goog
diff --git a/src/lib.rs b/src/lib.rs
index 183559f..59f942b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -72,15 +72,15 @@ extern crate log;
extern crate env_logger;
+use log::{Level, Log, Metadata, Record};
#[cfg(target_os = "android")]
use log_ffi::LogPriority;
-use log::{Level, Log, Metadata, Record};
use std::ffi::{CStr, CString};
-use std::mem;
use std::fmt;
+use std::mem::{self, MaybeUninit};
use std::ptr;
-pub use env_logger::filter::{Filter, Builder as FilterBuilder};
+pub use env_logger::filter::{Builder as FilterBuilder, Filter};
pub use env_logger::fmt::Formatter;
pub(crate) type FormatFn = Box<dyn Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send>;
@@ -137,7 +137,6 @@ impl AndroidLogger {
}
}
-
static ANDROID_LOGGER: OnceCell<AndroidLogger> = OnceCell::new();
const LOGGING_TAG_MAX_LEN: usize = 23;
@@ -158,22 +157,23 @@ impl Log for AndroidLogger {
}
fn log(&self, record: &Record) {
- let config = self.config
- .get_or_init(Config::default);
+ let config = self.config.get_or_init(Config::default);
if !config.filter_matches(record) {
return;
}
// tag must not exceed LOGGING_TAG_MAX_LEN
- #[allow(deprecated)] // created an issue #35 for this
- let mut tag_bytes: [u8; LOGGING_TAG_MAX_LEN + 1] = unsafe { mem::uninitialized() };
+ let mut tag_bytes: [MaybeUninit<u8>; LOGGING_TAG_MAX_LEN + 1] = uninit_array();
let module_path = record.module_path().unwrap_or_default().to_owned();
// If no tag was specified, use module name
let custom_tag = &config.tag;
- let tag = custom_tag.as_ref().map(|s| s.as_bytes()).unwrap_or_else(|| module_path.as_bytes());
+ let tag = custom_tag
+ .as_ref()
+ .map(|s| s.as_bytes())
+ .unwrap_or_else(|| module_path.as_bytes());
// truncate the tag here to fit into LOGGING_TAG_MAX_LEN
self.fill_tag_bytes(&mut tag_bytes, tag);
@@ -203,21 +203,19 @@ impl Log for AndroidLogger {
}
impl AndroidLogger {
- fn fill_tag_bytes(&self, array: &mut [u8], tag: &[u8]) {
+ fn fill_tag_bytes(&self, array: &mut [MaybeUninit<u8>], tag: &[u8]) {
if tag.len() > LOGGING_TAG_MAX_LEN {
- for (input, output) in tag.iter()
+ for (input, output) in tag
+ .iter()
.take(LOGGING_TAG_MAX_LEN - 2)
.chain(b"..\0".iter())
.zip(array.iter_mut())
{
- *output = *input;
+ output.write(*input);
}
} else {
- for (input, output) in tag.iter()
- .chain(b"\0".iter())
- .zip(array.iter_mut())
- {
- *output = *input;
+ for (input, output) in tag.iter().chain(b"\0".iter()).zip(array.iter_mut()) {
+ output.write(*input);
}
}
}
@@ -288,15 +286,17 @@ impl Config {
}
}
-struct PlatformLogWriter<'a> {
- #[cfg(target_os = "android")] priority: LogPriority,
- #[cfg(not(target_os = "android"))] priority: Level,
+pub struct PlatformLogWriter<'a> {
+ #[cfg(target_os = "android")]
+ priority: LogPriority,
+ #[cfg(not(target_os = "android"))]
+ priority: Level,
#[cfg(target_os = "android")] log_id: log_ffi::log_id_t,
#[cfg(not(target_os = "android"))] log_id: Option<LogId>,
len: usize,
last_newline_index: usize,
tag: &'a CStr,
- buffer: [u8; LOGGING_MSG_MAX_LEN + 1],
+ buffer: [MaybeUninit<u8>; LOGGING_MSG_MAX_LEN + 1],
}
impl<'a> PlatformLogWriter<'a> {
@@ -315,7 +315,7 @@ impl<'a> PlatformLogWriter<'a> {
len: 0,
last_newline_index: 0,
tag,
- buffer: unsafe { mem::uninitialized() },
+ buffer: uninit_array(),
}
}
@@ -328,7 +328,7 @@ impl<'a> PlatformLogWriter<'a> {
len: 0,
last_newline_index: 0,
tag,
- buffer: unsafe { mem::uninitialized() },
+ buffer: uninit_array(),
}
}
@@ -360,7 +360,7 @@ impl<'a> PlatformLogWriter<'a> {
}
/// Flush everything remaining to android logger.
- fn flush(&mut self) {
+ pub fn flush(&mut self) {
let total_len = self.len;
if total_len == 0 {
@@ -374,21 +374,22 @@ impl<'a> PlatformLogWriter<'a> {
/// Output buffer up until the \0 which will be placed at `len` position.
fn output_specified_len(&mut self, len: usize) {
- let mut last_byte: u8 = b'\0';
+ let mut last_byte = MaybeUninit::new(b'\0');
+
mem::swap(&mut last_byte, unsafe {
self.buffer.get_unchecked_mut(len)
});
- let msg: &CStr = unsafe { CStr::from_ptr(mem::transmute(self.buffer.as_ptr())) };
+ let msg: &CStr = unsafe { CStr::from_ptr(self.buffer.as_ptr().cast()) };
android_log(self.log_id, self.priority, self.tag, msg);
- *unsafe { self.buffer.get_unchecked_mut(len) } = last_byte;
+ unsafe { *self.buffer.get_unchecked_mut(len) = last_byte };
}
/// Copy `len` bytes from `index` position to starting position.
fn copy_bytes_to_start(&mut self, index: usize, len: usize) {
- let src = unsafe { self.buffer.as_ptr().add(index) };
let dst = self.buffer.as_mut_ptr();
+ let src = unsafe { self.buffer.as_ptr().add(index) };
unsafe { ptr::copy(src, dst, len) };
}
}
@@ -407,7 +408,7 @@ impl<'a> fmt::Write for PlatformLogWriter<'a> {
.zip(incomming_bytes)
.enumerate()
.fold(None, |acc, (i, (output, input))| {
- *output = *input;
+ output.write(*input);
if *input == b'\n' {
Some(i)
} else {
@@ -445,7 +446,9 @@ impl<'a> fmt::Write for PlatformLogWriter<'a> {
/// This action does not require initialization. However, without initialization it
/// will use the default filter, which allows all logs.
pub fn log(record: &Record) {
- ANDROID_LOGGER.get_or_init(AndroidLogger::default).log(record)
+ ANDROID_LOGGER
+ .get_or_init(AndroidLogger::default)
+ .log(record)
}
/// Initializes the global logger with an android logger.
@@ -466,6 +469,12 @@ pub fn init_once(config: Config) {
}
}
+// FIXME: When `maybe_uninit_uninit_array ` is stabilized, use it instead of this helper
+fn uninit_array<const N: usize, T>() -> [MaybeUninit<T>; N] {
+ // SAFETY: Array contains MaybeUninit, which is fine to be uninit
+ unsafe { MaybeUninit::uninit().assume_init() }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -527,12 +536,12 @@ mod tests {
let logger = AndroidLogger::new(Config::default());
let too_long_tag: [u8; LOGGING_TAG_MAX_LEN + 20] = [b'a'; LOGGING_TAG_MAX_LEN + 20];
- let mut result: [u8; LOGGING_TAG_MAX_LEN + 1] = Default::default();
+ let mut result: [MaybeUninit<u8>; LOGGING_TAG_MAX_LEN + 1] = uninit_array();
logger.fill_tag_bytes(&mut result, &too_long_tag);
let mut expected_result = [b'a'; LOGGING_TAG_MAX_LEN - 2].to_vec();
expected_result.extend("..\0".as_bytes());
- assert_eq!(result.to_vec(), expected_result);
+ assert_eq!(unsafe { assume_init_slice(&result) }, expected_result);
}
#[test]
@@ -540,12 +549,12 @@ mod tests {
let logger = AndroidLogger::new(Config::default());
let short_tag: [u8; 3] = [b'a'; 3];
- let mut result: [u8; LOGGING_TAG_MAX_LEN + 1] = Default::default();
+ let mut result: [MaybeUninit<u8>; LOGGING_TAG_MAX_LEN + 1] = uninit_array();
logger.fill_tag_bytes(&mut result, &short_tag);
let mut expected_result = short_tag.to_vec();
expected_result.push(0);
- assert_eq!(result.to_vec()[..4], expected_result);
+ assert_eq!(unsafe { assume_init_slice(&result[..4]) }, expected_result);
}
#[test]
@@ -573,7 +582,10 @@ mod tests {
// Should have flushed up until the last newline.
assert_eq!(writer.len, 3);
assert_eq!(writer.last_newline_index, 0);
- assert_eq!(&writer.buffer.to_vec()[..writer.len], "\n90".as_bytes());
+ assert_eq!(
+ unsafe { assume_init_slice(&writer.buffer[..writer.len]) },
+ "\n90".as_bytes()
+ );
writer.temporal_flush();
// Should have flushed all remaining bytes.
@@ -616,7 +628,7 @@ mod tests {
writer.output_specified_len(5);
assert_eq!(
- writer.buffer[..log_string.len()].to_vec(),
+ unsafe { assume_init_slice(&writer.buffer[..log_string.len()]) },
log_string.as_bytes()
);
}
@@ -630,7 +642,10 @@ mod tests {
writer.copy_bytes_to_start(3, 2);
- assert_eq!(writer.buffer[..10].to_vec(), "3423456789".as_bytes());
+ assert_eq!(
+ unsafe { assume_init_slice(&writer.buffer[..10]) },
+ "3423456789".as_bytes()
+ );
}
#[test]
@@ -645,7 +660,7 @@ mod tests {
writer.copy_bytes_to_start(10, 0);
assert_eq!(
- writer.buffer[..test_string.len()].to_vec(),
+ unsafe { assume_init_slice(&writer.buffer[..test_string.len()]) },
test_string.as_bytes()
);
}
@@ -653,4 +668,8 @@ mod tests {
fn get_tag_writer() -> PlatformLogWriter<'static> {
PlatformLogWriter::new(None, Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
}
+
+ unsafe fn assume_init_slice<T>(slice: &[MaybeUninit<T>]) -> &[T] {
+ &*(slice as *const [MaybeUninit<T>] as *const [T])
+ }
}
diff --git a/tests/config_log_level.rs b/tests/config_log_level.rs
index ca6ad59..7904371 100644
--- a/tests/config_log_level.rs
+++ b/tests/config_log_level.rs
@@ -6,4 +6,4 @@ fn config_log_level() {
android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Trace));
assert_eq!(log::max_level(), log::LevelFilter::Trace);
-} \ No newline at end of file
+}
diff --git a/tests/default_init.rs b/tests/default_init.rs
index e7ca9e2..7b04c24 100644
--- a/tests/default_init.rs
+++ b/tests/default_init.rs
@@ -7,4 +7,4 @@ fn default_init() {
// android_logger has default log level "off"
assert_eq!(log::max_level(), log::LevelFilter::Off);
-} \ No newline at end of file
+}
diff --git a/tests/multiple_init.rs b/tests/multiple_init.rs
index 99b58c8..b46f48e 100644
--- a/tests/multiple_init.rs
+++ b/tests/multiple_init.rs
@@ -9,4 +9,4 @@ fn multiple_init() {
android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Error));
assert_eq!(log::max_level(), log::LevelFilter::Trace);
-} \ No newline at end of file
+}