diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 60 |
1 files changed, 21 insertions, 39 deletions
@@ -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 @@ fn android_log(_log_id: Option<LogId>, _priority: Level, _tag: &CStr, _msg: &CSt /// 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 @@ impl Default for AndroidLogger { /// 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 @@ impl Log for AndroidLogger { 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 @@ impl Log for AndroidLogger { // 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 @@ impl AndroidLogger { } /// Filter for android logger. +#[derive(Default)] pub struct Config { log_level: Option<Level>, log_id: Option<LogId>, @@ -236,18 +233,6 @@ pub struct Config { 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 @@ impl Config { 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 @@ impl<'a> PlatformLogWriter<'a> { /// 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 @@ impl<'a> fmt::Write for PlatformLogWriter<'a> { /// 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 @@ pub fn log(record: &Record) { /// 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 @@ mod tests { 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 @@ mod tests { } 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()) } } |