Skip to content

Commit 96708db

Browse files
authored
Merge pull request #89 from dextero/cleanup
Add examples/system_log_level_overrides.rs
2 parents 634c027 + 032cfa2 commit 96708db

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ will consider the process-wide global state (set via
105105
[`__android_log_set_minimum_priority`](https://cs.android.com/android/platform/superproject/main/+/main:prebuilts/runtime/mainline/runtime/sdk/common_os/include/system/logging/liblog/include/android/log.h;l=364;drc=4cf460634134d51dba174f8af60dffb10f703f51))
106106
and Android system properties when deciding if a message should be logged or
107107
not. In this case, the effective log level is the _least verbose_ of the levels
108-
set between those and Rust log facilities.
108+
set between those and [Rust log
109+
facilities](https://docs.rs/log/latest/log/fn.set_max_level.html).
109110

110111
## License
111112

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//! An utility for testing the behavior of `android_logger` crate.
2+
//!
3+
//! ## Build
4+
//!
5+
//! 1. Setup [`cargo-ndk`](https://github.com/bbqsrc/cargo-ndk)
6+
//!
7+
//! ```
8+
//! cargo install cargo-ndk
9+
//! rustup target add x86_64-linux-android
10+
//! ```
11+
//!
12+
//! 2. Build with `cargo ndk`:
13+
//!
14+
//! ```
15+
//! ANDROID_NDK_HOME=/usr/lib/android-sdk/ndk/27.1.12297006 \
16+
//! cargo ndk -t x86_64 build --release --features android-api-30 \
17+
//! --example system_log_level_overrides
18+
//! ```
19+
//!
20+
//! ## Run on emulator
21+
//!
22+
//! 1. Grab a [Cuttlefish](https://source.android.com/docs/devices/cuttlefish)
23+
//! virtual device + Android build from [Android
24+
//! CI](https://ci.android.com/builds/branches/aosp-main/grid?legacy=1). Select
25+
//! the last green `aosp_cf_x86_64_phone` `trunk_staging-userdebug` build and
26+
//! open "Artifacts" link, download:
27+
//!
28+
//! - `aosp_cf_x86_64_phone-img-BUILDNUMBER.zip`
29+
//! - `cvd-host_package.tar.gz`
30+
//!
31+
//! 2. Unpack both archives & start the emulator.
32+
//!
33+
//! ```
34+
//! cd $(mktemp -d)
35+
//! unzip ~/Downloads/aosp_cf_x86_64_phone-img-*.zip
36+
//! tar xf ~/Downloads/cvd-host_package.tar.gz
37+
//! HOME=$PWD bin/launch_cvd
38+
//! ```
39+
//!
40+
//! Once emulator launches, `adb` should detect it on `0.0.0.0:6520`
41+
//! automatically. Shut down the `launch_cvd` command to exit the emulator.
42+
//!
43+
//! 3. Upload & run:
44+
//!
45+
//! ```
46+
//! adb push ./target/x86_64-linux-android/release/examples/system_log_level_overrides /data/local/tmp/
47+
//! adb shell /data/local/tmp/system_log_level_overrides
48+
//! ```
49+
//!
50+
//! ## Test interaction with Android system properties
51+
//!
52+
//! See [`logd`
53+
//! README](https://cs.android.com/android/platform/superproject/main/+/main:system/logging/logd/README.property)
54+
//! in AOSP for details.
55+
//!
56+
//! ```
57+
//! # default: should print info+ logs in `adb logcat -s log_test`
58+
//! # hint: use `adb logcat -v color` is awesome too
59+
//! adb shell /data/local/tmp/system_log_level_overrides
60+
//!
61+
//! # should print trace+ logs in `adb logcat -s log_test`
62+
//! adb shell setprop log.tag V
63+
//! adb shell /data/local/tmp/system_log_level_overrides
64+
//!
65+
//! # should print warn+ logs in `adb logcat -s log_test`
66+
//! adb shell setprop log.tag.log_test W
67+
//! adb shell /data/local/tmp/system_log_level_overrides
68+
//! ```
69+
70+
fn main() {
71+
android_logger::init_once(
72+
android_logger::Config::default()
73+
.with_tag("log_test")
74+
// If set, this is the highest level to log unless overriddeby by the system.
75+
// Note the verbosity can be *increased* through system properties.
76+
.with_max_level(log::LevelFilter::Info),
77+
);
78+
// The log crate applies its filtering before we even get to android_logger.
79+
// Pass everything down so that Android's liblog can determine the log level instead.
80+
log::set_max_level(log::LevelFilter::Trace);
81+
82+
log::trace!("trace");
83+
log::debug!("debug");
84+
log::info!("info");
85+
log::warn!("warn");
86+
log::error!("error");
87+
}

0 commit comments

Comments
 (0)