OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package logging | |
6 | |
7 import ( | |
8 "flag" | |
9 "strings" | |
10 ) | |
11 | |
12 const ( | |
13 // FilterOnAll is a Filter value that, when included in a Filter, causes
it to | |
14 // display all log messages. | |
15 FilterOnAll = "all" | |
16 | |
17 // FilterOnKey is a log field key whose value specifies the context's fi
lter. | |
18 FilterOnKey = "_filterOn" | |
19 ) | |
20 | |
21 // Filter is a configurable list of strings to filter on, when the 'filterOn' | |
22 // property is supplied. | |
23 type Filter []string | |
24 | |
25 var _ flag.Value = (*Filter)(nil) | |
26 | |
27 // Set implements flag.Value. | |
28 func (f *Filter) Set(v string) error { | |
29 if len(v) > 0 { | |
30 *f = append(*f, v) | |
31 } | |
32 return nil | |
33 } | |
34 | |
35 // String implements flag.Value. | |
36 func (f *Filter) String() string { | |
37 return strings.Join([]string(*f), ",") | |
38 } | |
39 | |
40 // Get returns a filter function that returns true if the Filter passes the | |
41 // supplied log flags. | |
42 func (f Filter) Get() func(interface{}) bool { | |
43 filterMap := make(map[string]bool) | |
44 for _, filter := range f { | |
45 if filter == FilterOnAll { | |
46 filterMap = nil | |
47 break | |
48 } | |
49 filterMap[filter] = true | |
50 } | |
51 | |
52 return func(filterOn interface{}) bool { | |
53 if filterMap == nil || filterOn == nil { | |
54 return true | |
55 } | |
56 | |
57 switch t := filterOn.(type) { | |
58 case string: | |
59 _, ok := filterMap[t] | |
60 return ok | |
61 | |
62 default: | |
63 return true | |
64 } | |
65 } | |
66 } | |
OLD | NEW |