@@ -31,6 +31,56 @@ impl fmt::Debug for Config {
3131 }
3232}
3333
34+ #[ cfg( all( target_os = "android" , feature = "android-api-30" ) ) ]
35+ fn android_log_priority_from_level ( level : Level ) -> android_log_sys:: LogPriority {
36+ match level {
37+ Level :: Warn => android_log_sys:: LogPriority :: WARN ,
38+ Level :: Info => android_log_sys:: LogPriority :: INFO ,
39+ Level :: Debug => android_log_sys:: LogPriority :: DEBUG ,
40+ Level :: Error => android_log_sys:: LogPriority :: ERROR ,
41+ Level :: Trace => android_log_sys:: LogPriority :: VERBOSE ,
42+ }
43+ }
44+
45+ /// Asks Android liblog if a message with given `tag` and `priority` should be logged, using
46+ /// `default_prio` as the level filter in case no system- or process-wide overrides are set.
47+ #[ cfg( all( target_os = "android" , feature = "android-api-30" ) ) ]
48+ fn android_is_loggable_len (
49+ prio : log_ffi:: LogPriority ,
50+ tag : & str ,
51+ default_prio : log_ffi:: LogPriority ,
52+ ) -> bool {
53+ // SAFETY: tag points to a valid string tag.len() bytes long.
54+ unsafe {
55+ log_ffi:: __android_log_is_loggable_len (
56+ prio as log_ffi:: c_int ,
57+ tag. as_ptr ( ) as * const log_ffi:: c_char ,
58+ tag. len ( ) as log_ffi:: c_size_t ,
59+ default_prio as log_ffi:: c_int ,
60+ ) != 0
61+ }
62+ }
63+
64+ #[ cfg( not( all( target_os = "android" , feature = "android-api-30" ) ) ) ]
65+ fn default_is_loggable ( _tag : & str , record_level : Level , config_level : Option < LevelFilter > ) -> bool {
66+ record_level <= config_level. unwrap_or_else ( log:: max_level)
67+ }
68+
69+ #[ cfg( all( target_os = "android" , feature = "android-api-30" ) ) ]
70+ fn android_is_loggable ( tag : & str , record_level : Level , config_level : Option < LevelFilter > ) -> bool {
71+ let prio = android_log_priority_from_level ( record_level) ;
72+ // Priority to use in case no system-wide or process-wide overrides are set.
73+ let default_prio = match config_level {
74+ Some ( level_filter) => match level_filter. to_level ( ) {
75+ Some ( level) => android_log_priority_from_level ( level) ,
76+ // LevelFilter::to_level() returns None only for LevelFilter::Off
77+ None => android_log_sys:: LogPriority :: SILENT ,
78+ } ,
79+ None => android_log_sys:: LogPriority :: INFO ,
80+ } ;
81+ android_is_loggable_len ( prio, tag, default_prio)
82+ }
83+
3484impl Config {
3585 /// Changes the maximum log level.
3686 ///
@@ -64,9 +114,13 @@ impl Config {
64114 }
65115 }
66116
67- pub ( crate ) fn is_loggable ( & self , level : Level ) -> bool {
68- // todo: consider __android_log_is_loggable.
69- level <= self . log_level . unwrap_or_else ( log:: max_level)
117+ pub ( crate ) fn is_loggable ( & self , tag : & str , level : Level ) -> bool {
118+ #[ cfg( all( target_os = "android" , feature = "android-api-30" ) ) ]
119+ use android_is_loggable as is_loggable;
120+ #[ cfg( not( all( target_os = "android" , feature = "android-api-30" ) ) ) ]
121+ use default_is_loggable as is_loggable;
122+
123+ is_loggable ( tag, level, self . log_level )
70124 }
71125
72126 pub fn with_filter ( mut self , filter : env_filter:: Filter ) -> Self {
0 commit comments