日志记录

你应该使用 log crate 来自动记录日志到 logcat (设备上)或 stdout(主机上):

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_min_level(log::Level::Trace),
  9. );
  10. debug!("Starting program.");
  11. info!("Things are going fine.");
  12. error!("Something went wrong!");
  13. }

在你的设备上构建,推送,并运行二进制文件 :

  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

日志将会在 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!