@@ -20,14 +20,67 @@ impl fmt::Debug for Config {
20
20
. field ( "buf_id" , & self . buf_id )
21
21
. field ( "filter" , & self . filter )
22
22
. field ( "tag" , & self . tag )
23
- . field ( "custom_format" , match & self . custom_format {
24
- Some ( _) => & "Some(_)" ,
25
- None => & "None" ,
26
- } )
23
+ . field (
24
+ "custom_format" ,
25
+ match & self . custom_format {
26
+ Some ( _) => & "Some(_)" ,
27
+ None => & "None" ,
28
+ } ,
29
+ )
27
30
. finish ( )
28
31
}
29
32
}
30
33
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
+
31
84
impl Config {
32
85
/// Changes the maximum log level.
33
86
///
@@ -61,9 +114,13 @@ impl Config {
61
114
}
62
115
}
63
116
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)
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 )
67
124
}
68
125
69
126
pub fn with_filter ( mut self , filter : env_filter:: Filter ) -> Self {
0 commit comments