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.
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_max_level(log::LevelFilter::Trace),
11. );
12. debug!("Starting program.");
13. info!("Things are going fine.");
14. error!("Something went wrong!");
15. }
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
libloggeris only needed in the final binary, if you’re logging from a library you only need thelogfacade crate.
