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