59
59
//! [`Filter::matches`]: struct.Filter.html#method.matches
60
60
61
61
use log:: { Level , LevelFilter , Metadata , Record } ;
62
- use std:: collections:: HashMap ;
63
62
use std:: env;
64
63
use std:: fmt;
65
64
use std:: mem;
@@ -108,7 +107,7 @@ pub struct Filter {
108
107
///
109
108
/// [`Filter`]: struct.Filter.html
110
109
pub struct Builder {
111
- directives : HashMap < Option < String > , LevelFilter > ,
110
+ directives : Vec < Directive > ,
112
111
filter : Option < inner:: Filter > ,
113
112
built : bool ,
114
113
}
@@ -172,7 +171,7 @@ impl Builder {
172
171
/// Initializes the filter builder with defaults.
173
172
pub fn new ( ) -> Builder {
174
173
Builder {
175
- directives : HashMap :: new ( ) ,
174
+ directives : Vec :: new ( ) ,
176
175
filter : None ,
177
176
built : false ,
178
177
}
@@ -189,6 +188,19 @@ impl Builder {
189
188
builder
190
189
}
191
190
191
+ /// Insert the directive replacing any directive with the same name.
192
+ fn insert_directive ( & mut self , mut directive : Directive ) {
193
+ if let Some ( pos) = self
194
+ . directives
195
+ . iter ( )
196
+ . position ( |d| d. name == directive. name )
197
+ {
198
+ mem:: swap ( & mut self . directives [ pos] , & mut directive) ;
199
+ } else {
200
+ self . directives . push ( directive) ;
201
+ }
202
+ }
203
+
192
204
/// Adds a directive to the filter for a specific module.
193
205
pub fn filter_module ( & mut self , module : & str , level : LevelFilter ) -> & mut Self {
194
206
self . filter ( Some ( module) , level)
@@ -204,7 +216,10 @@ impl Builder {
204
216
/// The given module (if any) will log at most the specified level provided.
205
217
/// If no module is provided then the filter will apply to all log messages.
206
218
pub fn filter ( & mut self , module : Option < & str > , level : LevelFilter ) -> & mut Self {
207
- self . directives . insert ( module. map ( |s| s. to_string ( ) ) , level) ;
219
+ self . insert_directive ( Directive {
220
+ name : module. map ( |s| s. to_string ( ) ) ,
221
+ level,
222
+ } ) ;
208
223
self
209
224
}
210
225
@@ -219,7 +234,7 @@ impl Builder {
219
234
self . filter = filter;
220
235
221
236
for directive in directives {
222
- self . directives . insert ( directive. name , directive . level ) ;
237
+ self . insert_directive ( directive) ;
223
238
}
224
239
self
225
240
}
@@ -237,12 +252,8 @@ impl Builder {
237
252
level : LevelFilter :: Error ,
238
253
} ) ;
239
254
} else {
240
- // Consume map of directives.
241
- let directives_map = mem:: take ( & mut self . directives ) ;
242
- directives = directives_map
243
- . into_iter ( )
244
- . map ( |( name, level) | Directive { name, level } )
245
- . collect ( ) ;
255
+ // Consume directives.
256
+ directives = mem:: take ( & mut self . directives ) ;
246
257
// Sort the directives by length of their name, this allows a
247
258
// little more efficient lookup at runtime.
248
259
directives. sort_by ( |a, b| {
0 commit comments