-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use __android_log_is_loggable in AndroidLogger::enabled #84
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,56 @@ impl fmt::Debug for Config { | |
} | ||
} | ||
|
||
#[cfg(all(target_os = "android", feature = "android-api-30"))] | ||
fn android_log_priority_from_level(level: Level) -> android_log_sys::LogPriority { | ||
match level { | ||
Level::Warn => android_log_sys::LogPriority::WARN, | ||
Level::Info => android_log_sys::LogPriority::INFO, | ||
Level::Debug => android_log_sys::LogPriority::DEBUG, | ||
Level::Error => android_log_sys::LogPriority::ERROR, | ||
Level::Trace => android_log_sys::LogPriority::VERBOSE, | ||
} | ||
} | ||
|
||
/// Asks Android liblog if a message with given `tag` and `priority` should be logged, using | ||
/// `default_prio` as the level filter in case no system- or process-wide overrides are set. | ||
#[cfg(all(target_os = "android", feature = "android-api-30"))] | ||
fn android_is_loggable_len( | ||
prio: log_ffi::LogPriority, | ||
tag: &str, | ||
default_prio: log_ffi::LogPriority, | ||
) -> bool { | ||
// SAFETY: tag points to a valid string tag.len() bytes long. | ||
unsafe { | ||
log_ffi::__android_log_is_loggable_len( | ||
prio as log_ffi::c_int, | ||
tag.as_ptr() as *const log_ffi::c_char, | ||
tag.len() as log_ffi::c_size_t, | ||
default_prio as log_ffi::c_int, | ||
) != 0 | ||
} | ||
} | ||
|
||
#[cfg(not(all(target_os = "android", feature = "android-api-30")))] | ||
fn default_is_loggable(_tag: &str, record_level: Level, config_level: Option<LevelFilter>) -> bool { | ||
record_level <= config_level.unwrap_or_else(log::max_level) | ||
} | ||
|
||
#[cfg(all(target_os = "android", feature = "android-api-30"))] | ||
fn android_is_loggable(tag: &str, record_level: Level, config_level: Option<LevelFilter>) -> bool { | ||
let prio = android_log_priority_from_level(record_level); | ||
// Priority to use in case no system-wide or process-wide overrides are set. | ||
let default_prio = match config_level { | ||
Some(level_filter) => match level_filter.to_level() { | ||
Some(level) => android_log_priority_from_level(level), | ||
// LevelFilter::to_level() returns None only for LevelFilter::Off | ||
None => android_log_sys::LogPriority::SILENT, | ||
}, | ||
None => android_log_sys::LogPriority::INFO, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log level set in the Using And the only way I can see how that could be achieved is to:
In your opinion, what behavior would make the most sense? Anyway, multiple layers of filters are extremely confusing. If only the |
||
}; | ||
android_is_loggable_len(prio, tag, default_prio) | ||
} | ||
|
||
impl Config { | ||
/// Changes the maximum log level. | ||
/// | ||
|
@@ -64,9 +114,13 @@ impl Config { | |
} | ||
} | ||
|
||
pub(crate) fn is_loggable(&self, level: Level) -> bool { | ||
// todo: consider __android_log_is_loggable. | ||
level <= self.log_level.unwrap_or_else(log::max_level) | ||
pub(crate) fn is_loggable(&self, tag: &str, level: Level) -> bool { | ||
#[cfg(all(target_os = "android", feature = "android-api-30"))] | ||
use android_is_loggable as is_loggable; | ||
#[cfg(not(all(target_os = "android", feature = "android-api-30")))] | ||
use default_is_loggable as is_loggable; | ||
|
||
is_loggable(tag, level, self.log_level) | ||
} | ||
|
||
pub fn with_filter(mut self, filter: env_filter::Filter) -> Self { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we link
Rust log facilities
to (the documentation for)log::set_max_level()
once again?