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
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.junit.http;
45
46 import java.text.MessageFormat;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
50
51 import org.eclipse.jetty.util.log.Logger;
52
53
54
55
56 public class RecordingLogger implements Logger {
57 private static List<Warning> warnings = new ArrayList<>();
58
59
60
61
62
63 public static void clear() {
64 synchronized (warnings) {
65 warnings.clear();
66 }
67 }
68
69
70
71
72
73
74 public static List<Warning> getWarnings() {
75 synchronized (warnings) {
76 ArrayList<Warning> copy = new ArrayList<>(warnings);
77 return Collections.unmodifiableList(copy);
78 }
79 }
80
81 @SuppressWarnings("serial")
82 public static class Warning extends Exception {
83 public Warning(String msg) {
84 super(msg);
85 }
86
87 public Warning(String msg, Throwable cause) {
88 super(msg, cause);
89 }
90
91 public Warning(Throwable thrown) {
92 super(thrown);
93 }
94 }
95
96 private final String name;
97
98
99
100
101 public RecordingLogger() {
102 this("");
103 }
104
105
106
107
108
109
110 public RecordingLogger(String name) {
111 this.name = name;
112 }
113
114
115 @Override
116 public Logger getLogger(@SuppressWarnings("hiding") String name) {
117 return new RecordingLogger(name);
118 }
119
120
121 @Override
122 public String getName() {
123 return name;
124 }
125
126
127
128
129
130
131
132
133 public void warn(String msg, Object arg0, Object arg1) {
134 synchronized (warnings) {
135 warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
136 }
137 }
138
139
140 @Override
141 public void warn(String msg, Throwable th) {
142 synchronized (warnings) {
143 warnings.add(new Warning(msg, th));
144 }
145 }
146
147
148
149
150
151
152
153 public void warn(String msg) {
154 synchronized (warnings) {
155 warnings.add(new Warning(msg));
156 }
157 }
158
159
160
161
162
163
164
165
166 public void debug(String msg, Object arg0, Object arg1) {
167
168 }
169
170
171 @Override
172 public void debug(String msg, Throwable th) {
173
174 }
175
176
177
178
179
180
181
182 public void debug(String msg) {
183
184 }
185
186
187
188
189
190
191
192
193 public void info(String msg, Object arg0, Object arg1) {
194
195 }
196
197
198
199
200
201
202 public void info(String msg) {
203
204 }
205
206
207 @Override
208 public boolean isDebugEnabled() {
209 return false;
210 }
211
212
213 @Override
214 public void setDebugEnabled(boolean enabled) {
215
216 }
217
218
219 @Override
220 public void warn(String msg, Object... args) {
221 synchronized (warnings) {
222 int i = 0;
223 int index = msg.indexOf("{}");
224 while (index >= 0) {
225 msg = msg.replaceFirst("\\{\\}", "{" + i++ + "}");
226 index = msg.indexOf("{}");
227 }
228 warnings.add(new Warning(MessageFormat.format(msg, args)));
229 }
230 }
231
232
233 @Override
234 public void warn(Throwable thrown) {
235 synchronized (warnings) {
236 warnings.add(new Warning(thrown));
237 }
238 }
239
240
241 @Override
242 public void info(String msg, Object... args) {
243
244 }
245
246
247 @Override
248 public void info(Throwable thrown) {
249
250 }
251
252
253 @Override
254 public void info(String msg, Throwable thrown) {
255
256 }
257
258
259 @Override
260 public void debug(String msg, Object... args) {
261
262 }
263
264
265 @Override
266 public void debug(Throwable thrown) {
267
268 }
269
270
271 @Override
272 public void ignore(Throwable arg0) {
273
274 }
275
276
277 @Override
278 public void debug(String msg, long value) {
279
280 }
281 }