Upgrade rust/crates/android_logger to 0.11.0

Test: make
Change-Id: I2fc61ce97edfd8a09eb67d559bb71b0fd9016081
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 00435b7..c1be5ef 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,6 @@
 {
   "git": {
-    "sha1": "c20ec25acacf0731583140b230e6ce94e59b054d"
-  }
-}
+    "sha1": "596b92d5907f0054884412d2f7137704edae4208"
+  },
+  "path_in_vcs": ""
+}
\ No newline at end of file
diff --git a/Android.bp b/Android.bp
index 056c310..63736fe 100644
--- a/Android.bp
+++ b/Android.bp
@@ -43,7 +43,7 @@
     name: "android_logger_test_src_lib",
     crate_name: "android_logger",
     cargo_env_compat: true,
-    cargo_pkg_version: "0.10.1",
+    cargo_pkg_version: "0.11.0",
     srcs: ["src/lib.rs"],
     test_suites: ["general-tests"],
     auto_gen_config: true,
@@ -55,8 +55,8 @@
     rustlibs: [
         "libandroid_log_sys",
         "libenv_logger",
-        "liblazy_static",
         "liblog_rust",
+        "libonce_cell",
     ],
 }
 
@@ -64,7 +64,7 @@
     name: "android_logger_test_defaults",
     crate_name: "android_logger",
     cargo_env_compat: true,
-    cargo_pkg_version: "0.10.1",
+    cargo_pkg_version: "0.11.0",
     test_suites: ["general-tests"],
     auto_gen_config: true,
     edition: "2015",
@@ -76,8 +76,8 @@
         "libandroid_log_sys",
         "libandroid_logger",
         "libenv_logger",
-        "liblazy_static",
         "liblog_rust",
+        "libonce_cell",
     ],
 }
 
@@ -103,7 +103,7 @@
     name: "libandroid_logger",
     crate_name: "android_logger",
     cargo_env_compat: true,
-    cargo_pkg_version: "0.10.1",
+    cargo_pkg_version: "0.11.0",
     srcs: ["src/lib.rs"],
     edition: "2015",
     features: [
@@ -113,8 +113,8 @@
     rustlibs: [
         "libandroid_log_sys",
         "libenv_logger",
-        "liblazy_static",
         "liblog_rust",
+        "libonce_cell",
     ],
     apex_available: [
         "//apex_available:platform",
diff --git a/Cargo.toml b/Cargo.toml
index dec0113..02cf115 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,38 +3,46 @@
 # When uploading crates to the registry Cargo will automatically
 # "normalize" Cargo.toml files for maximal compatibility
 # with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies
+# to registry (e.g., crates.io) dependencies.
 #
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
 
 [package]
 name = "android_logger"
-version = "0.10.1"
+version = "0.11.0"
 authors = ["The android_logger Developers"]
-description = "A logging implementation for `log` which hooks to android log output.\n"
+description = """
+A logging implementation for `log` which hooks to android log output.
+"""
 readme = "README.md"
-keywords = ["android", "bindings", "log", "logger"]
+keywords = [
+    "android",
+    "bindings",
+    "log",
+    "logger",
+]
 categories = ["api-bindings"]
 license = "MIT OR Apache-2.0"
 repository = "https://github.com/Nercury/android_logger-rs"
+
 [dependencies.android_log-sys]
 version = "0.2"
 
 [dependencies.env_logger]
-version = "0.8"
+version = "0.9"
 default-features = false
 
-[dependencies.lazy_static]
-version = "1.4"
-
 [dependencies.log]
 version = "0.4"
 
+[dependencies.once_cell]
+version = "1.9"
+
 [features]
 default = ["regex"]
 regex = ["env_logger/regex"]
+
 [badges.travis-ci]
 repository = "Nercury/android_logger-rs"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index a9ad77f..f092ceb 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "android_logger"
-version = "0.10.1"
+version = "0.11.0"
 authors = ["The android_logger Developers"]
 license = "MIT OR Apache-2.0"
 readme = "README.md"
@@ -16,7 +16,7 @@
 regex = ["env_logger/regex"]
 
 [dependencies]
-lazy_static = "1.4"
+once_cell = "1.9"
 
 [dependencies.log]
 version = "0.4"
@@ -25,7 +25,7 @@
 version = "0.2"
 
 [dependencies.env_logger]
-version = "0.8"
+version = "0.9"
 default-features = false
 
 [badges]
diff --git a/METADATA b/METADATA
index 27e229f..7f7dc13 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@
   }
   url {
     type: ARCHIVE
-    value: "https://static.crates.io/crates/android_logger/android_logger-0.10.1.crate"
+    value: "https://static.crates.io/crates/android_logger/android_logger-0.11.0.crate"
   }
-  version: "0.10.1"
+  version: "0.11.0"
   license_type: NOTICE
   last_upgrade_date {
-    year: 2021
+    year: 2022
     month: 4
-    day: 19
+    day: 21
   }
 }
diff --git a/README.md b/README.md
index c3b88bf..cc408d6 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@
 
 ```toml
 [target.'cfg(target_os = "android")'.dependencies]
-android_logger = "0.10"
+android_logger = "0.11"
 ```
 
 Example of initialization on activity creation, with log configuration:
diff --git a/patches/0001-Support-selecting-target-log-buffer.patch b/patches/0001-Support-selecting-target-log-buffer.patch
index e5fc33b..88ab283 100644
--- a/patches/0001-Support-selecting-target-log-buffer.patch
+++ b/patches/0001-Support-selecting-target-log-buffer.patch
@@ -14,10 +14,10 @@
  1 file changed, 58 insertions(+), 13 deletions(-)
 
 diff --git a/src/lib.rs b/src/lib.rs
-index 11a127e..d21be3f 100644
+index c50ce97..e7a62ef 100644
 --- a/src/lib.rs
 +++ b/src/lib.rs
-@@ -87,21 +87,49 @@ pub use env_logger::fmt::Formatter;
+@@ -85,21 +85,49 @@ pub use env_logger::fmt::Formatter;
  
  pub(crate) type FormatFn = Box<dyn Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send>;
  
@@ -74,7 +74,7 @@
  
  /// Underlying android logger backend
  pub struct AndroidLogger {
-@@ -164,7 +192,7 @@ impl Log for AndroidLogger {
+@@ -160,7 +188,7 @@ impl Log for AndroidLogger {
  
          // message must not exceed LOGGING_MSG_MAX_LEN
          // therefore split log message into multiple log calls
@@ -83,23 +83,15 @@
  
          // If a custom tag is used, add the module path to the message.
          // Use PlatformLogWriter to output chunks if they exceed max size.
-@@ -208,6 +236,7 @@ impl AndroidLogger {
- /// Filter for android logger.
+@@ -205,6 +233,7 @@ impl AndroidLogger {
+ #[derive(Default)]
  pub struct Config {
      log_level: Option<Level>,
 +    log_id: Option<LogId>,
      filter: Option<env_logger::filter::Filter>,
      tag: Option<CString>,
      custom_format: Option<FormatFn>,
-@@ -217,6 +246,7 @@ impl Default for Config {
-     fn default() -> Self {
-         Config {
-             log_level: None,
-+            log_id: None,
-             filter: None,
-             tag: None,
-             custom_format: None,
-@@ -234,6 +264,15 @@ impl Config {
+@@ -220,6 +249,15 @@ impl Config {
          self
      }
  
@@ -114,8 +106,8 @@
 +
      fn filter_matches(&self, record: &Record) -> bool {
          if let Some(ref filter) = self.filter {
-             filter.matches(&record)
-@@ -273,6 +312,8 @@ impl Config {
+             filter.matches(record)
+@@ -259,6 +297,8 @@ impl Config {
  struct PlatformLogWriter<'a> {
      #[cfg(target_os = "android")] priority: LogPriority,
      #[cfg(not(target_os = "android"))] priority: Level,
@@ -124,7 +116,7 @@
      len: usize,
      last_newline_index: usize,
      tag: &'a CStr,
-@@ -281,7 +322,7 @@ struct PlatformLogWriter<'a> {
+@@ -267,7 +307,7 @@ struct PlatformLogWriter<'a> {
  
  impl<'a> PlatformLogWriter<'a> {
      #[cfg(target_os = "android")]
@@ -133,7 +125,7 @@
          #[allow(deprecated)] // created an issue #35 for this
          PlatformLogWriter {
              priority: match level {
-@@ -291,6 +332,7 @@ impl<'a> PlatformLogWriter<'a> {
+@@ -277,6 +317,7 @@ impl<'a> PlatformLogWriter<'a> {
                  Level::Error => LogPriority::ERROR,
                  Level::Trace => LogPriority::VERBOSE,
              },
@@ -141,7 +133,7 @@
              len: 0,
              last_newline_index: 0,
              tag,
-@@ -299,10 +341,11 @@ impl<'a> PlatformLogWriter<'a> {
+@@ -285,10 +326,11 @@ impl<'a> PlatformLogWriter<'a> {
      }
  
      #[cfg(not(target_os = "android"))]
@@ -154,7 +146,7 @@
              len: 0,
              last_newline_index: 0,
              tag,
-@@ -358,7 +401,7 @@ impl<'a> PlatformLogWriter<'a> {
+@@ -344,7 +386,7 @@ impl<'a> PlatformLogWriter<'a> {
          });
  
          let msg: &CStr = unsafe { CStr::from_ptr(mem::transmute(self.buffer.as_ptr())) };
@@ -163,7 +155,7 @@
  
          *unsafe { self.buffer.get_unchecked_mut(len) } = last_byte;
      }
-@@ -458,9 +501,11 @@ mod tests {
+@@ -441,9 +483,11 @@ mod tests {
          // Filter is checked in config_filter_match below.
          let config = Config::default()
              .with_min_level(Level::Trace)
@@ -175,23 +167,22 @@
          assert_eq!(config.tag, Some(CString::new("my_app").unwrap()));
      }
  
-@@ -531,7 +576,7 @@ mod tests {
+@@ -514,7 +558,7 @@ mod tests {
      fn platform_log_writer_init_values() {
          let tag = CStr::from_bytes_with_nul(b"tag\0").unwrap();
  
--        let writer = PlatformLogWriter::new(Level::Warn, &tag);
-+        let writer = PlatformLogWriter::new(None, Level::Warn, &tag);
+-        let writer = PlatformLogWriter::new(Level::Warn, tag);
++        let writer = PlatformLogWriter::new(None, Level::Warn, tag);
  
          assert_eq!(writer.tag, tag);
          // Android uses LogPriority instead, which doesn't implement equality checks
-@@ -630,6 +675,6 @@ mod tests {
+@@ -613,6 +657,6 @@ mod tests {
      }
  
      fn get_tag_writer() -> PlatformLogWriter<'static> {
--        PlatformLogWriter::new(Level::Warn, &CStr::from_bytes_with_nul(b"tag\0").unwrap())
-+        PlatformLogWriter::new(None, Level::Warn, &CStr::from_bytes_with_nul(b"tag\0").unwrap())
+-        PlatformLogWriter::new(Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
++        PlatformLogWriter::new(None, Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
      }
  }
 -- 
 2.35.1.265.g69c8d7142f-goog
-
diff --git a/src/lib.rs b/src/lib.rs
index bc4fa61..183559f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -65,15 +65,13 @@
 
 #[cfg(target_os = "android")]
 extern crate android_log_sys as log_ffi;
-#[macro_use]
-extern crate lazy_static;
+extern crate once_cell;
+use once_cell::sync::OnceCell;
 #[macro_use]
 extern crate log;
 
 extern crate env_logger;
 
-use std::sync::RwLock;
-
 #[cfg(target_os = "android")]
 use log_ffi::LogPriority;
 use log::{Level, Log, Metadata, Record};
@@ -127,21 +125,20 @@
 
 /// Underlying android logger backend
 pub struct AndroidLogger {
-    config: RwLock<Config>,
+    config: OnceCell<Config>,
 }
 
 impl AndroidLogger {
     /// Create new logger instance from config
     pub fn new(config: Config) -> AndroidLogger {
         AndroidLogger {
-            config: RwLock::new(config),
+            config: OnceCell::from(config),
         }
     }
 }
 
-lazy_static! {
-   static ref ANDROID_LOGGER: AndroidLogger = AndroidLogger::default();
-}
+
+static ANDROID_LOGGER: OnceCell<AndroidLogger> = OnceCell::new();
 
 const LOGGING_TAG_MAX_LEN: usize = 23;
 const LOGGING_MSG_MAX_LEN: usize = 4000;
@@ -150,7 +147,7 @@
     /// Create a new logger with default config
     fn default() -> AndroidLogger {
         AndroidLogger {
-            config: RwLock::new(Config::default()),
+            config: OnceCell::from(Config::default()),
         }
     }
 }
@@ -162,8 +159,7 @@
 
     fn log(&self, record: &Record) {
         let config = self.config
-            .read()
-            .expect("failed to acquire android_log filter lock for read");
+            .get_or_init(Config::default);
 
         if !config.filter_matches(record) {
             return;
@@ -177,7 +173,7 @@
 
         // If no tag was specified, use module name
         let custom_tag = &config.tag;
-        let tag = custom_tag.as_ref().map(|s| s.as_bytes()).unwrap_or(module_path.as_bytes());
+        let tag = custom_tag.as_ref().map(|s| s.as_bytes()).unwrap_or_else(|| module_path.as_bytes());
 
         // truncate the tag here to fit into LOGGING_TAG_MAX_LEN
         self.fill_tag_bytes(&mut tag_bytes, tag);
@@ -228,6 +224,7 @@
 }
 
 /// Filter for android logger.
+#[derive(Default)]
 pub struct Config {
     log_level: Option<Level>,
     log_id: Option<LogId>,
@@ -236,18 +233,6 @@
     custom_format: Option<FormatFn>,
 }
 
-impl Default for Config {
-    fn default() -> Self {
-        Config {
-            log_level: None,
-            log_id: None,
-            filter: None,
-            tag: None,
-            custom_format: None,
-        }
-    }
-}
-
 impl Config {
     /// Change the minimum log level.
     ///
@@ -269,7 +254,7 @@
 
     fn filter_matches(&self, record: &Record) -> bool {
         if let Some(ref filter) = self.filter {
-            filter.matches(&record)
+            filter.matches(record)
         } else {
             true
         }
@@ -402,7 +387,7 @@
 
     /// Copy `len` bytes from `index` position to starting position.
     fn copy_bytes_to_start(&mut self, index: usize, len: usize) {
-        let src = unsafe { self.buffer.as_ptr().offset(index as isize) };
+        let src = unsafe { self.buffer.as_ptr().add(index) };
         let dst = self.buffer.as_mut_ptr();
         unsafe { ptr::copy(src, dst, len) };
     }
@@ -460,7 +445,7 @@
 /// This action does not require initialization. However, without initialization it
 /// will use the default filter, which allows all logs.
 pub fn log(record: &Record) {
-    ANDROID_LOGGER.log(record)
+    ANDROID_LOGGER.get_or_init(AndroidLogger::default).log(record)
 }
 
 /// Initializes the global logger with an android logger.
@@ -471,16 +456,13 @@
 /// It is ok to call this at the activity creation, and it will be
 /// repeatedly called on every lifecycle restart (i.e. screen rotation).
 pub fn init_once(config: Config) {
-    if let Err(err) = log::set_logger(&*ANDROID_LOGGER) {
+    let log_level = config.log_level;
+    let logger = ANDROID_LOGGER.get_or_init(|| AndroidLogger::new(config));
+
+    if let Err(err) = log::set_logger(logger) {
         debug!("android_logger: log::set_logger failed: {}", err);
-    } else {
-        if let Some(level) = config.log_level {
-            log::set_max_level(level.to_level_filter());
-        }
-        *ANDROID_LOGGER
-            .config
-            .write()
-            .expect("failed to acquire android_log filter lock for write") = config;
+    } else if let Some(level) = log_level {
+        log::set_max_level(level.to_level_filter());
     }
 }
 
@@ -570,7 +552,7 @@
     fn platform_log_writer_init_values() {
         let tag = CStr::from_bytes_with_nul(b"tag\0").unwrap();
 
-        let writer = PlatformLogWriter::new(None, Level::Warn, &tag);
+        let writer = PlatformLogWriter::new(None, Level::Warn, tag);
 
         assert_eq!(writer.tag, tag);
         // Android uses LogPriority instead, which doesn't implement equality checks
@@ -669,6 +651,6 @@
     }
 
     fn get_tag_writer() -> PlatformLogWriter<'static> {
-        PlatformLogWriter::new(None, Level::Warn, &CStr::from_bytes_with_nul(b"tag\0").unwrap())
+        PlatformLogWriter::new(None, Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
     }
 }