Consistent Rust logger initialization for host and device

Implement a universal logger interface that allows logging both
on-device (using android_logger) and on-host (using env_logger).
Uses a configuration struct similar to the one used by android_logger.

Allows using the same logging initialization code for on-device
code and host-side tests.

Bug: 162454083
Test: atest system/logging/rust
 + For device tests: confirm logging happens in logcat
 + For host tests: confirm logging happens to stdout (for failing test)
Change-Id: If80685033d2b39c660fab881405456f629fb1f8b
diff --git a/rust/tests/config_log_level.rs b/rust/tests/config_log_level.rs
new file mode 100644
index 0000000..7a9a241
--- /dev/null
+++ b/rust/tests/config_log_level.rs
@@ -0,0 +1,17 @@
+use std::env;
+
+#[test]
+fn config_log_level() {
+    // Environment variables should be overwritten by config values.
+    env::set_var("RUST_LOG", "debug");
+
+    let init_result = logger::init(
+        logger::Config::default()
+            .with_min_level(log::Level::Trace));
+
+    assert_eq!(init_result, true);
+    // Setting the level through the Config struct should impact both host and device
+    assert_eq!(log::max_level(), log::LevelFilter::Trace);
+
+    env::remove_var("RUST_LOG");
+}
\ No newline at end of file
diff --git a/rust/tests/default_init.rs b/rust/tests/default_init.rs
new file mode 100644
index 0000000..413e8c8
--- /dev/null
+++ b/rust/tests/default_init.rs
@@ -0,0 +1,12 @@
+#[test]
+fn default_init() {
+    assert_eq!(logger::init(Default::default()), true);
+
+    if cfg!(target_os = "android") {
+        // android_logger has default log level "off"
+        assert_eq!(log::max_level(), log::LevelFilter::Off);
+    } else {
+        // env_logger has default log level "error"
+        assert_eq!(log::max_level(), log::LevelFilter::Error);
+    }
+}
\ No newline at end of file
diff --git a/rust/tests/env_log_level.rs b/rust/tests/env_log_level.rs
new file mode 100644
index 0000000..eb86d97
--- /dev/null
+++ b/rust/tests/env_log_level.rs
@@ -0,0 +1,16 @@
+use std::env;
+
+#[test]
+fn env_log_level() {
+    env::set_var("RUST_LOG", "debug");
+    assert_eq!(logger::init(Default::default()), true);
+
+    if cfg!(target_os = "android") {
+        // android_logger does not read from environment variables
+        assert_eq!(log::max_level(), log::LevelFilter::Off);
+    } else {
+        // env_logger reads its log level from the "RUST_LOG" environment variable
+        assert_eq!(log::max_level(), log::LevelFilter::Debug);
+    }
+    env::remove_var("RUST_LOG");
+}
\ No newline at end of file
diff --git a/rust/tests/multiple_init.rs b/rust/tests/multiple_init.rs
new file mode 100644
index 0000000..0ac8b2f
--- /dev/null
+++ b/rust/tests/multiple_init.rs
@@ -0,0 +1,8 @@
+#[test]
+fn multiple_init() {
+    let first_init = logger::init(Default::default());
+    let second_init = logger::init(Default::default());
+
+    assert_eq!(first_init, true);
+    assert_eq!(second_init, false);
+}
\ No newline at end of file