diff --git a/Cargo.toml b/Cargo.toml index 6c749ad..70a1d80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ http = [ "warp", "tokio" ] webhooks = [ "reqwest" ] track-spends = [] ffi = [] +android = [ "android_logger" ] [lib] crate-type = [ "lib", "cdylib" ] @@ -49,6 +50,9 @@ warp = { version = "0.2.5", optional = true } # webhooks deps reqwest = { version = "0.10.9", optional = true, features = ["json", "blocking"] } +# android deps +android_logger = { version = "0.9.1", optional = true } + # cli/extra deps structopt = { version = "0.3.20", optional = true } dotenv = { version = "0.15.0", optional = true } diff --git a/src/config.rs b/src/config.rs index b180b21..6973162 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,8 +10,10 @@ use crate::util::descriptor::ExtendedDescriptor; use crate::util::xpub::XyzPubKey; use crate::util::BoolThen; +#[cfg(any(feature = "pretty_env_logger", feature = "android_logger"))] +use log::Level; #[cfg(feature = "pretty_env_logger")] -use {log::Level, pretty_env_logger::env_logger::Builder as LogBuilder}; +use pretty_env_logger::env_logger::Builder as LogBuilder; #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] @@ -459,52 +461,74 @@ impl Config { }) } + #[cfg(all(not(feature = "pretty_env_logger"), not(feature = "android_logger")))] + pub fn setup_logger(&self) {} + + #[cfg(any(feature = "pretty_env_logger", feature = "android_logger"))] pub fn setup_logger(&self) { #[cfg(feature = "pretty_env_logger")] - apply_log_env(if self.timestamp { + let mut builder = apply_log_env(if self.timestamp { pretty_env_logger::formatted_timed_builder() } else { pretty_env_logger::formatted_builder() - }) - .filter_module( - "bwt", - match self.verbose { - 0 => Level::Info, - 1 => Level::Debug, - _ => Level::Trace, - } - .to_level_filter(), - ) - .filter_module( - "bitcoincore_rpc", - match self.verbose { - 0 | 1 => Level::Warn, - 2 => Level::Debug, - _ => Level::Trace, - } - .to_level_filter(), - ) - .filter_module( - "warp", - match self.verbose { - 0 | 1 => Level::Warn, - 2 => Level::Info, - 3 => Level::Debug, - _ => Level::Trace, - } - .to_level_filter(), - ) - .filter_module("hyper", Level::Warn.to_level_filter()) - .filter_level( - match self.verbose { - 0 | 1 => Level::Warn, - 2 | 3 => Level::Info, - 4 => Level::Debug, - _ => Level::Trace, - } - .to_level_filter(), - ) - .init(); + }); + + #[cfg(feature = "android_logger")] + let mut builder = android_logger::FilterBuilder::from_env("RUST_LOG"); + + builder + .filter_module( + "bwt", + match self.verbose { + 0 => Level::Info, + 1 => Level::Debug, + _ => Level::Trace, + } + .to_level_filter(), + ) + .filter_module( + "bitcoincore_rpc", + match self.verbose { + 0 | 1 => Level::Warn, + 2 => Level::Debug, + _ => Level::Trace, + } + .to_level_filter(), + ) + .filter_module( + "warp", + match self.verbose { + 0 | 1 => Level::Warn, + 2 => Level::Info, + 3 => Level::Debug, + _ => Level::Trace, + } + .to_level_filter(), + ) + .filter_module("hyper", Level::Warn.to_level_filter()) + .filter_level( + match self.verbose { + 0 | 1 => Level::Warn, + 2 | 3 => Level::Info, + 4 => Level::Debug, + _ => Level::Trace, + } + .to_level_filter(), + ); + + #[cfg(feature = "pretty_env_logger")] + builder.init(); + + #[cfg(feature = "android_logger")] + android_logger::init_once( + android_logger::Config::default() + .with_min_level(match self.verbose { + 0 => Level::Info, + 1 => Level::Debug, + _ => Level::Trace, + }) + .with_filter(builder.build()), + ); } } diff --git a/src/lib.rs b/src/lib.rs index bde8fa0..569fa62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,9 @@ extern crate bitcoin_hashes; #[macro_use] extern crate serde; +#[cfg(all(feature = "pretty_env_logger", feature = "android_logger"))] +compile_error!("the pretty_env_logger and android_logger features are mutually exclusive"); + #[macro_use] pub mod util;