diff options
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 8 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | src/lib.rs | 45 |
6 files changed, 52 insertions, 15 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 4919394..cbd863c 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "84a4307b2659b5a6aa8e86a778dbd794a2525ed0" + "sha1": "91a6943f7814d457bf001c9a1185e15e80382659" } } @@ -37,13 +37,13 @@ rust_library { } // dependent_library ["feature_list"] -// aho-corasick-0.7.13 "default,std" +// aho-corasick-0.7.15 "default,std" // android_log-sys-0.2.0 // cfg-if-0.1.10 // env_logger-0.7.1 "regex" // lazy_static-1.4.0 // log-0.4.11 "std" -// memchr-2.3.3 "default,std,use_std" -// regex-1.3.9 "aho-corasick,default,memchr,perf,perf-cache,perf-dfa,perf-inline,perf-literal,std,thread_local,unicode,unicode-age,unicode-bool,unicode-case,unicode-gencat,unicode-perl,unicode-script,unicode-segment" -// regex-syntax-0.6.18 "default,unicode,unicode-age,unicode-bool,unicode-case,unicode-gencat,unicode-perl,unicode-script,unicode-segment" +// memchr-2.3.4 "default,std,use_std" +// regex-1.4.2 "aho-corasick,default,memchr,perf,perf-cache,perf-dfa,perf-inline,perf-literal,std,thread_local,unicode,unicode-age,unicode-bool,unicode-case,unicode-gencat,unicode-perl,unicode-script,unicode-segment" +// regex-syntax-0.6.21 "default,unicode,unicode-age,unicode-bool,unicode-case,unicode-gencat,unicode-perl,unicode-script,unicode-segment" // thread_local-1.0.1 @@ -12,7 +12,7 @@ [package] name = "android_logger" -version = "0.9.0" +version = "0.9.2" authors = ["The android_logger Developers"] description = "A logging implementation for `log` which hooks to android log output.\n" readme = "README.md" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index bd8d088..46785d6 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "android_logger" -version = "0.9.0" +version = "0.9.2" authors = ["The android_logger Developers"] license = "MIT OR Apache-2.0" readme = "README.md" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/android_logger/android_logger-0.9.0.crate" + value: "https://static.crates.io/crates/android_logger/android_logger-0.9.2.crate" } - version: "0.9.0" + version: "0.9.2" license_type: NOTICE last_upgrade_date { year: 2020 - month: 7 - day: 22 + month: 12 + day: 7 } } @@ -50,6 +50,18 @@ //! // .. //! } //! ``` +//! +//! ## Example with a custom log formatter +//! +//! ``` +//! use android_logger::Config; +//! +//! android_logger::init_once( +//! Config::default() +//! .with_min_level(log::Level::Trace) +//! .format(|f, record| write!(f, "my_app: {}", record.args())) +//! ) +//! ``` #[cfg(target_os = "android")] extern crate android_log_sys as log_ffi; @@ -71,6 +83,9 @@ use std::fmt; use std::ptr; pub use env_logger::filter::{Filter, Builder as FilterBuilder}; +pub use env_logger::fmt::Formatter; + +pub(crate) type FormatFn = Box<dyn Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send>; /// Output log to android system. #[cfg(target_os = "android")] @@ -153,10 +168,13 @@ impl Log for AndroidLogger { // If a custom tag is used, add the module path to the message. // Use PlatformLogWriter to output chunks if they exceed max size. - let _ = if custom_tag.is_some() { - fmt::write(&mut writer, format_args!("{}: {}", module_path, *record.args())) - } else { - fmt::write(&mut writer, *record.args()) + let _ = match (custom_tag, &config.custom_format) { + (_, Some(format)) => format(&mut writer, record), + (Some(_), _) => fmt::write( + &mut writer, + format_args!("{}: {}", module_path, *record.args()), + ), + _ => fmt::write(&mut writer, *record.args()), }; // output the remaining message (this would usually be the most common case) @@ -192,6 +210,7 @@ pub struct Config { log_level: Option<Level>, filter: Option<env_logger::filter::Filter>, tag: Option<CString>, + custom_format: Option<FormatFn>, } impl Default for Config { @@ -200,6 +219,7 @@ impl Default for Config { log_level: None, filter: None, tag: None, + custom_format: None, } } } @@ -231,6 +251,23 @@ impl Config { self.tag = Some(CString::new(tag).expect("Can't convert tag to CString")); self } + + /// Sets the format function for formatting the log output. + /// ``` + /// # use android_logger::Config; + /// android_logger::init_once( + /// Config::default() + /// .with_min_level(log::Level::Trace) + /// .format(|f, record| write!(f, "my_app: {}", record.args())) + /// ) + /// ``` + pub fn format<F>(mut self, format: F) -> Self + where + F: Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send + 'static, + { + self.custom_format = Some(Box::new(format)); + self + } } struct PlatformLogWriter<'a> { |