@@ -71,6 +71,9 @@ use std::fmt;
71
71
use std:: ptr;
72
72
73
73
pub use env_logger:: filter:: { Filter , Builder as FilterBuilder } ;
74
+ pub use env_logger:: fmt:: Formatter ;
75
+
76
+ pub ( crate ) type FormatFn = Box < dyn Fn ( & mut dyn fmt:: Write , & Record ) -> fmt:: Result + Sync + Send > ;
74
77
75
78
/// Output log to android system.
76
79
#[ cfg( target_os = "android" ) ]
@@ -153,10 +156,13 @@ impl Log for AndroidLogger {
153
156
154
157
// If a custom tag is used, add the module path to the message.
155
158
// Use PlatformLogWriter to output chunks if they exceed max size.
156
- let _ = if custom_tag. is_some ( ) {
157
- fmt:: write ( & mut writer, format_args ! ( "{}: {}" , module_path, * record. args( ) ) )
158
- } else {
159
- fmt:: write ( & mut writer, * record. args ( ) )
159
+ let _ = match ( custom_tag, & config. custom_format ) {
160
+ ( _, Some ( format) ) => format ( & mut writer, record) ,
161
+ ( Some ( _) , _) => fmt:: write (
162
+ & mut writer,
163
+ format_args ! ( "{}: {}" , module_path, * record. args( ) ) ,
164
+ ) ,
165
+ _ => fmt:: write ( & mut writer, * record. args ( ) ) ,
160
166
} ;
161
167
162
168
// output the remaining message (this would usually be the most common case)
@@ -192,6 +198,7 @@ pub struct Config {
192
198
log_level : Option < Level > ,
193
199
filter : Option < env_logger:: filter:: Filter > ,
194
200
tag : Option < CString > ,
201
+ custom_format : Option < FormatFn > ,
195
202
}
196
203
197
204
impl Default for Config {
@@ -200,6 +207,7 @@ impl Default for Config {
200
207
log_level : None ,
201
208
filter : None ,
202
209
tag : None ,
210
+ custom_format : None ,
203
211
}
204
212
}
205
213
}
@@ -231,6 +239,14 @@ impl Config {
231
239
self . tag = Some ( CString :: new ( tag) . expect ( "Can't convert tag to CString" ) ) ;
232
240
self
233
241
}
242
+
243
+ pub fn format < F > ( mut self , format : F ) -> Self
244
+ where
245
+ F : Fn ( & mut dyn fmt:: Write , & Record ) -> fmt:: Result + Sync + Send + ' static ,
246
+ {
247
+ self . custom_format = Some ( Box :: new ( format) ) ;
248
+ self
249
+ }
234
250
}
235
251
236
252
struct PlatformLogWriter < ' a > {
0 commit comments