|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.debug; |
|
36 |
| |
|
37 |
| import java.io.IOException; |
|
38 |
| import java.util.HashSet; |
|
39 |
| import java.util.Set; |
|
40 |
| |
|
41 |
| import edu.rice.cs.plt.lambda.LambdaUtil; |
|
42 |
| import edu.rice.cs.plt.lambda.Predicate; |
|
43 |
| import edu.rice.cs.plt.text.TextUtil; |
|
44 |
| import edu.rice.cs.plt.tuple.Option; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| public class FilteredLogSink implements LogSink { |
|
55 |
| |
|
56 |
| private final LogSink _delegate; |
|
57 |
| private final Predicate<? super Message> _pred; |
|
58 |
| |
|
59 |
9
| public FilteredLogSink(LogSink delegate, Predicate<? super Message> pred) {
|
|
60 |
9
| _delegate = delegate;
|
|
61 |
9
| _pred = pred;
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| public void close() throws IOException { _delegate.close(); }
|
|
65 |
| |
|
66 |
0
| public void log(StandardMessage m) {
|
|
67 |
0
| if (_pred.contains(m)) { _delegate.log(m); }
|
|
68 |
| } |
|
69 |
| |
|
70 |
0
| public void logEnd(EndMessage m) {
|
|
71 |
0
| if (_pred.contains(m)) { _delegate.logEnd(m); }
|
|
72 |
| } |
|
73 |
| |
|
74 |
0
| public void logError(ErrorMessage m) {
|
|
75 |
0
| if (_pred.contains(m)) { _delegate.logError(m); }
|
|
76 |
| } |
|
77 |
| |
|
78 |
0
| public void logStack(StackMessage m) {
|
|
79 |
0
| if (_pred.contains(m)) { _delegate.logStack(m); }
|
|
80 |
| } |
|
81 |
| |
|
82 |
0
| public void logStart(StartMessage m) {
|
|
83 |
0
| if (_pred.contains(m)) { _delegate.logStart(m); }
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
5
| public static FilteredLogSink byLocationWhiteList(LogSink delegate, final String... prefixes) {
|
|
88 |
5
| return new FilteredLogSink(delegate, locationWhiteListPredicate(prefixes));
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
2
| public static FilteredLogSink byLocationBlackList(LogSink delegate, final String... prefixes) {
|
|
93 |
2
| return new FilteredLogSink(delegate, locationBlackListPredicate(prefixes));
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
0
| public static FilteredLogSink byLocation(LogSink delegate, final Predicate<? super String> pred) {
|
|
98 |
0
| return new FilteredLogSink(delegate, locationPredicate(pred));
|
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public static FilteredLogSink byStackDepth(LogSink delegate, int maxDepth) {
|
|
103 |
0
| return new FilteredLogSink(delegate, stackDepthPredicate(maxDepth));
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
0
| public static FilteredLogSink byThreadWhiteList(LogSink delegate, Thread... threads) {
|
|
108 |
0
| return new FilteredLogSink(delegate, threadWhiteListPredicate(threads));
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
1
| public static FilteredLogSink byThreadWhiteList(LogSink delegate, String... nameParts) {
|
|
113 |
1
| return new FilteredLogSink(delegate, threadWhiteListPredicate(nameParts));
|
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
0
| public static FilteredLogSink byThreadBlackList(LogSink delegate, Thread... threads) {
|
|
118 |
0
| return new FilteredLogSink(delegate, threadBlackListPredicate(threads));
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
1
| public static FilteredLogSink byThreadBlackList(LogSink delegate, String... nameParts) {
|
|
123 |
1
| return new FilteredLogSink(delegate, threadBlackListPredicate(nameParts));
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
0
| public static FilteredLogSink byThread(LogSink delegate, Predicate<? super ThreadSnapshot> pred) {
|
|
128 |
0
| return new FilteredLogSink(delegate, threadPredicate(pred));
|
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
7
| public static Predicate<Message> locationWhiteListPredicate(final String... prefixes) {
|
|
138 |
7
| return locationPredicate(new Predicate<String>() {
|
|
139 |
0
| public boolean contains(String s) { return TextUtil.startsWithAny(s, prefixes); }
|
|
140 |
| }); |
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
2
| public static Predicate<Message> locationBlackListPredicate(final String... prefixes) {
|
|
150 |
2
| return LambdaUtil.negate(locationWhiteListPredicate(prefixes));
|
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
7
| public static Predicate<Message> locationPredicate(final Predicate<? super String> pred) {
|
|
160 |
7
| return new Predicate<Message>() {
|
|
161 |
0
| public boolean contains(Message m) {
|
|
162 |
0
| Option<StackTraceElement> locOpt = m.caller();
|
|
163 |
0
| if (locOpt.isSome()) {
|
|
164 |
0
| StackTraceElement loc = locOpt.unwrap();
|
|
165 |
0
| return pred.contains(loc.getClassName() + "." + loc.getMethodName());
|
|
166 |
| } |
|
167 |
0
| else { return pred.contains(""); }
|
|
168 |
| } |
|
169 |
| }; |
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
0
| public static Predicate<Message> stackDepthPredicate(final int maxDepth) {
|
|
174 |
0
| return new Predicate<Message>() {
|
|
175 |
0
| public boolean contains(Message m) { return m.thread().getStackTrace().size() <= maxDepth; }
|
|
176 |
| }; |
|
177 |
| } |
|
178 |
| |
|
179 |
| |
|
180 |
0
| public static Predicate<Message> threadWhiteListPredicate(Thread... threads) {
|
|
181 |
0
| final Set<Long> ids = new HashSet<Long>();
|
|
182 |
0
| for (Thread t : threads) { ids.add(t.getId()); }
|
|
183 |
0
| threads = null;
|
|
184 |
0
| return threadPredicate(new Predicate<ThreadSnapshot>() {
|
|
185 |
0
| public boolean contains(ThreadSnapshot t) { return ids.contains(t.getId()); }
|
|
186 |
| }); |
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
2
| public static Predicate<Message> threadWhiteListPredicate(final String... nameParts) {
|
|
194 |
2
| return threadPredicate(new Predicate<ThreadSnapshot>() {
|
|
195 |
0
| public boolean contains(ThreadSnapshot t) { return TextUtil.containsAny(t.getName(), nameParts); }
|
|
196 |
| }); |
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
0
| public static Predicate<Message> threadBlackListPredicate(Thread... threads) {
|
|
201 |
0
| return LambdaUtil.negate(threadWhiteListPredicate(threads));
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
1
| public static Predicate<Message> threadBlackListPredicate(String... nameParts) {
|
|
209 |
1
| return LambdaUtil.negate(threadWhiteListPredicate(nameParts));
|
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
2
| public static Predicate<Message> threadPredicate(final Predicate<? super ThreadSnapshot> pred) {
|
|
214 |
2
| return new Predicate<Message>() {
|
|
215 |
0
| public boolean contains(Message m) { return pred.contains(m.thread()); }
|
|
216 |
| }; |
|
217 |
| } |
|
218 |
| |
|
219 |
| } |