59
59
//! [`Filter::matches`]: struct.Filter.html#method.matches
60
60
61
61
use log:: { Level , LevelFilter , Metadata , Record } ;
62
+ use std:: collections:: HashMap ;
62
63
use std:: env;
63
64
use std:: fmt;
64
65
use std:: mem;
@@ -107,7 +108,7 @@ pub struct Filter {
107
108
///
108
109
/// [`Filter`]: struct.Filter.html
109
110
pub struct Builder {
110
- directives : Vec < Directive > ,
111
+ directives : HashMap < Option < String > , LevelFilter > ,
111
112
filter : Option < inner:: Filter > ,
112
113
built : bool ,
113
114
}
@@ -171,7 +172,7 @@ impl Builder {
171
172
/// Initializes the filter builder with defaults.
172
173
pub fn new ( ) -> Builder {
173
174
Builder {
174
- directives : Vec :: new ( ) ,
175
+ directives : HashMap :: new ( ) ,
175
176
filter : None ,
176
177
built : false ,
177
178
}
@@ -203,10 +204,7 @@ impl Builder {
203
204
/// The given module (if any) will log at most the specified level provided.
204
205
/// If no module is provided then the filter will apply to all log messages.
205
206
pub fn filter ( & mut self , module : Option < & str > , level : LevelFilter ) -> & mut Self {
206
- self . directives . push ( Directive {
207
- name : module. map ( |s| s. to_string ( ) ) ,
208
- level,
209
- } ) ;
207
+ self . directives . insert ( module. map ( |s| s. to_string ( ) ) , level) ;
210
208
self
211
209
}
212
210
@@ -221,7 +219,7 @@ impl Builder {
221
219
self . filter = filter;
222
220
223
221
for directive in directives {
224
- self . directives . push ( directive) ;
222
+ self . directives . insert ( directive. name , directive . level ) ;
225
223
}
226
224
self
227
225
}
@@ -231,24 +229,31 @@ impl Builder {
231
229
assert ! ( !self . built, "attempt to re-use consumed builder" ) ;
232
230
self . built = true ;
233
231
232
+ let mut directives = Vec :: new ( ) ;
234
233
if self . directives . is_empty ( ) {
235
234
// Adds the default filter if none exist
236
- self . directives . push ( Directive {
235
+ directives. push ( Directive {
237
236
name : None ,
238
237
level : LevelFilter :: Error ,
239
238
} ) ;
240
239
} else {
240
+ // Consume map of directives.
241
+ let directives_map = mem:: replace ( & mut self . directives , HashMap :: new ( ) ) ;
242
+ directives = directives_map
243
+ . into_iter ( )
244
+ . map ( |( name, level) | Directive { name, level } )
245
+ . collect ( ) ;
241
246
// Sort the directives by length of their name, this allows a
242
247
// little more efficient lookup at runtime.
243
- self . directives . sort_by ( |a, b| {
248
+ directives. sort_by ( |a, b| {
244
249
let alen = a. name . as_ref ( ) . map ( |a| a. len ( ) ) . unwrap_or ( 0 ) ;
245
250
let blen = b. name . as_ref ( ) . map ( |b| b. len ( ) ) . unwrap_or ( 0 ) ;
246
251
alen. cmp ( & blen)
247
252
} ) ;
248
253
}
249
254
250
255
Filter {
251
- directives : mem:: replace ( & mut self . directives , Vec :: new ( ) ) ,
256
+ directives : mem:: replace ( & mut directives, Vec :: new ( ) ) ,
252
257
filter : mem:: replace ( & mut self . filter , None ) ,
253
258
}
254
259
}
0 commit comments