Location>code7788 >text

How to output rust logs to android terminal

Popularity:684 ℃/2024-10-24 09:34:44

All posts on this blog, unless otherwise stated, use theCC BY-NC-SA 4.0License Agreement. Reprinted with permission fromonly you

contexts

In Rust, when printing logs using println! the output is actually sent to standard output (stdout), whereas Android Logcat is specialized in processing and displaying application log messages, and the standard output implementation is redefined for this environment. This means that Rust log output does not appear in Logcat.

android_loggerIntegrates directly with Android's logging system to ensure that log messages appear in Logcat as expected.

configure

android_logger is used as follows

Note that the version used here is android_logger0.11.0, if you use the latest version, you may need to make relevant api adjustments.

Add the following dependency to

[dependencies]
log = "0.4"

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.11.0"

The reason for introducing logs at the same time:

  • log is an abstract library for logging, providing a set of unified interfaces for logging messages (such as info!, warn!, error!, etc.). android_logger is a concrete implementation of log for the android platform.
  • While android_logger itself relies on log, and you might think that you don't need to refer to log, android_logger actually relies on the log library for things like log::LevelFilter and log::Level::Debug.

initialization

#[cfg(target_os = "android")]
fn init_logging() {
    android_logger::init_once(
        android_logger::Config::default()
            .with_min_level(log::Level::Debug)
            .with_tag("flutter"),
    );
}

Note that here with_min_level, if it is debug, then the most used level for printing must be debug and above, with info, it is basically impossible to print in logcat.

utilization

The rust file is used as follows


extern crate android_logger;

Add Printing

impl PlatformTextureWithProvider for BoxedPixelData {
    fn create_texture(
        engine_handle: i64,
        payload_provider: Arc<dyn PayloadProvider<Self>>,
    ) -> Result<PlatformTexture<BoxedPixelData>> {
        //usage example
        log::debug!("2222222223 create_texture");
        log::debug!("this is a debug {}", "raynor");

        PlatformTexture::new(engine_handle, Some(payload_provider))
    }
}

After connecting the android device and running flutter run, the logcat log output is as follows:

√ Built build\app\outputs\flutter-apk\
Installing build\app\outputs\flutter-apk\...           6.3s
W/FlutterAnimationAdvance( 3237): FlutterAnimationAdvance getInstance()
D/flutter ( 3237): irondash_texture::platform::platform_impl: 2222222223 create_texture
D/flutter ( 3237): irondash_texture::platform::platform_impl: this is a debug raynor

take note of

Because the library is recompiled every time you run it, there's no need to run cargo clean every time you make a change to the logs in rust. You can run the commands that are supported by your platform (such as flutter run) to see the logs take effect, and you'll see the output.