Skip to content

Commit

Permalink
Add support for android_logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Dec 5, 2020
1 parent 6e2ef7d commit 74b2b2f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 42 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ http = [ "warp", "tokio" ]
webhooks = [ "reqwest" ]
track-spends = []
ffi = []
android = [ "android_logger" ]

[lib]
crate-type = [ "lib", "cdylib" ]
Expand Down Expand Up @@ -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 }
Expand Down
108 changes: 66 additions & 42 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()),
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 74b2b2f

Please sign in to comment.