|
| 1 | +use crate::{FormatFn, LogId}; |
| 2 | +use log::{Level, LevelFilter, Record}; |
| 3 | +use std::ffi::CString; |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +/// Filter for android logger. |
| 7 | +#[derive(Default)] |
| 8 | +pub struct Config { |
| 9 | + pub(crate) log_level: Option<LevelFilter>, |
| 10 | + pub(crate) buf_id: Option<LogId>, |
| 11 | + filter: Option<env_filter::Filter>, |
| 12 | + pub(crate) tag: Option<CString>, |
| 13 | + pub(crate) custom_format: Option<FormatFn>, |
| 14 | +} |
| 15 | + |
| 16 | +impl fmt::Debug for Config { |
| 17 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 18 | + f.debug_struct("Config") |
| 19 | + .field("log_level", &self.log_level) |
| 20 | + .field("buf_id", &self.buf_id) |
| 21 | + .field("filter", &self.filter) |
| 22 | + .field("tag", &self.tag) |
| 23 | + .field("custom_format", match &self.custom_format { |
| 24 | + Some(_) => &"Some(_)", |
| 25 | + None => &"None", |
| 26 | + }) |
| 27 | + .finish() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Config { |
| 32 | + /// Changes the maximum log level. |
| 33 | + /// |
| 34 | + /// Note, that `Trace` is the maximum level, because it provides the |
| 35 | + /// maximum amount of detail in the emitted logs. |
| 36 | + /// |
| 37 | + /// If `Off` level is provided, then nothing is logged at all. |
| 38 | + /// |
| 39 | + /// [`log::max_level()`] is considered as the default level. |
| 40 | + pub fn with_max_level(mut self, level: LevelFilter) -> Self { |
| 41 | + self.log_level = Some(level); |
| 42 | + self |
| 43 | + } |
| 44 | + |
| 45 | + /// Changes the Android logging system buffer to be used. |
| 46 | + /// |
| 47 | + /// By default, logs are sent to the [`Main`] log. Other logging buffers may |
| 48 | + /// only be accessible to certain processes. |
| 49 | + /// |
| 50 | + /// [`Main`]: LogId::Main |
| 51 | + pub fn with_log_buffer(mut self, buf_id: LogId) -> Self { |
| 52 | + self.buf_id = Some(buf_id); |
| 53 | + self |
| 54 | + } |
| 55 | + |
| 56 | + pub(crate) fn filter_matches(&self, record: &Record) -> bool { |
| 57 | + if let Some(ref filter) = self.filter { |
| 58 | + filter.matches(record) |
| 59 | + } else { |
| 60 | + true |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + pub(crate) fn is_loggable(&self, level: Level) -> bool { |
| 65 | + // todo: consider __android_log_is_loggable. |
| 66 | + level <= self.log_level.unwrap_or_else(log::max_level) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn with_filter(mut self, filter: env_filter::Filter) -> Self { |
| 70 | + self.filter = Some(filter); |
| 71 | + self |
| 72 | + } |
| 73 | + |
| 74 | + pub fn with_tag<S: Into<Vec<u8>>>(mut self, tag: S) -> Self { |
| 75 | + self.tag = Some(CString::new(tag).expect("Can't convert tag to CString")); |
| 76 | + self |
| 77 | + } |
| 78 | + |
| 79 | + /// Sets the format function for formatting the log output. |
| 80 | + /// ``` |
| 81 | + /// # use android_logger::Config; |
| 82 | + /// android_logger::init_once( |
| 83 | + /// Config::default() |
| 84 | + /// .with_max_level(log::LevelFilter::Trace) |
| 85 | + /// .format(|f, record| write!(f, "my_app: {}", record.args())) |
| 86 | + /// ) |
| 87 | + /// ``` |
| 88 | + pub fn format<F>(mut self, format: F) -> Self |
| 89 | + where |
| 90 | + F: Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send + 'static, |
| 91 | + { |
| 92 | + self.custom_format = Some(Box::new(format)); |
| 93 | + self |
| 94 | + } |
| 95 | +} |
0 commit comments