system_log_level_overrides/
system_log_level_overrides.rs

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
70fn 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}