日志记录
你应该使用 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.
3. use log::{debug, error, info};
5. /// Logs a greeting.
6. fn main() {
7. logger::init(
8. logger::Config::default()
9. .with_tag_on_device("rust")
10. .with_min_level(log::Level::Trace),
11. );
12. debug!("Starting program.");
13. info!("Things are going fine.");
14. error!("Something went wrong!");
15. }
在你的设备上构建,推送,并运行二进制文件 :
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!
