Logging

You should use the log crate to automatically log to logcat (on-device) or stdout (on-host):

hello_rust_logs/Android.bp:

  1. rust_binary {
  2. name: "hello_rust_logs",
  3. crate_name: "hello_rust_logs",
  4. srcs: ["src/main.rs"],
  5. rustlibs: [
  6. "liblog_rust",
  7. "liblogger",
  8. ],
  9. host_supported: true,
  10. }

hello_rust_logs/src/main.rs:

  1. //! Rust logging demo.
  2. use log::{debug, error, info};
  3. /// Logs a greeting.
  4. fn main() {
  5. logger::init(
  6. logger::Config::default()
  7. .with_tag_on_device("rust")
  8. .with_max_level(log::LevelFilter::Trace),
  9. );
  10. debug!("Starting program.");
  11. info!("Things are going fine.");
  12. error!("Something went wrong!");
  13. }

Build, push, and run the binary on your device:

  1. m hello_rust_logs
  2. adb push "$ANDROID_PRODUCT_OUT/system/bin/hello_rust_logs" /data/local/tmp
  3. adb shell /data/local/tmp/hello_rust_logs

The logs show up in adb logcat:

  1. adb logcat -s rust
  1. 09-08 08:38:32.454 2420 2420 D rust: hello_rust_logs: Starting program.
  2. 09-08 08:38:32.454 2420 2420 I rust: hello_rust_logs: Things are going fine.
  3. 09-08 08:38:32.454 2420 2420 E rust: hello_rust_logs: Something went wrong!
  • The logger implementation in liblogger is only needed in the final binary, if you’re logging from a library you only need the log facade crate.