1 package org.catacomb.report;
2
3 import java.util.HashSet;
4
5 public class E {
6
7 public static long time0 = 0;
8
9 static String lastShortSource;
10 static String cachedAction;
11 static String lastErr;
12 static int nrep;
13
14 static String possErrText;
15
16 static HashSet<String> sentMessages = new HashSet<String>();
17
18
19 static Reporter reporter;
20
21
22
23
24 static final int INFO = 1;
25 static final int WARNING = 2;
26 static final int ERROR = 3;
27 static final int MESSAGE = 4;
28 static final int FATAL = 5;
29 static final int DEPRECATED = 6;
30 static final int MISSING = 7;
31
32
33 static String[] levels;
34
35 private static void initLevels() {
36 levels = new String[10];
37 levels[INFO] = "INFO";
38 levels[WARNING] = "WARNING";
39 levels[ERROR] = "ERROR";
40 levels[MESSAGE] = "MESSAGE";
41 levels[FATAL] = "FATAL";
42 levels[DEPRECATED] = "DEPRECATED";
43 levels[MISSING] = "MISSING";
44 }
45
46
47
48 public static void setReporter(Reporter r) {
49 reporter = r;
50 }
51
52
53 private static long getTime() {
54 return System.currentTimeMillis();
55 }
56
57
58 public static void zeroTime() {
59 time0 = getTime();
60 }
61
62 public static String getStringTime() {
63 if (time0 == 0) {
64 zeroTime();
65 }
66 long dt = (getTime() - time0);
67 return "" + dt;
68 }
69
70 public static void info(String s) {
71 report(INFO, s + getShortSource());
72 }
73
74 public static void infoTime(String s) {
75 report(INFO, s + " at " + getStringTime());
76 }
77
78
79 public static void longInfo(String s) {
80 report(INFO, s);
81 showSource(16);
82 }
83
84
85 public static void message(String s) {
86 report(MESSAGE, s);
87 }
88
89 public static void oneLineWarning(String s) {
90 report(WARNING, s + getShortSource());
91 }
92
93
94 public static void shortWarning(String s) {
95 report(WARNING, s + getMediumSource());
96 }
97
98 public static void shortError(String s) {
99 report(ERROR, s + getShortSource());
100 }
101
102 public static void medWarning(String s) {
103 report(WARNING, s);
104 showSource(4);
105 }
106
107 public static void warning(String s) {
108 report(WARNING, s);
109 showSource(12);
110 }
111
112 public static void linkToWarning(String s, Object obj) {
113 report(WARNING, s);
114 String fcn = obj.getClass().getName();
115 String scn = fcn.substring(fcn.lastIndexOf(".") + 1, fcn.length());
116 System.out.println(" at " + fcn + ".nomethod(" + scn + ".java:1) " + getShortSource());
117
118 }
119
120 public static void error(String s) {
121 error(s, -1);
122 }
123
124 public static void longError(String s) {
125 error(s, 20);
126 }
127
128
129 public static void error(String s, int n) {
130 if (lastErr != null && lastErr.equals(s)) {
131 nrep += 1;
132 if (nrep == 3 || nrep == 10 || nrep ==30 || nrep == 100) {
133 report(ERROR, " ....... last error repeated " + nrep + " times");
134 }
135 } else {
136 if (nrep > 0) {
137 report(ERROR, "total repeats of last error " + nrep);
138 }
139 nrep = 0;
140 lastErr = s;
141 report(ERROR, s);
142 if (n < 0) {
143 showSource();
144 } else {
145 showSource(n);
146 }
147 }
148 }
149
150
151 public static void possibleError(String s) {
152 possErrText = "Possible Error " + s + "\n" + getMediumSource();
153 }
154
155
156 public static void debugError(String s) {
157 report(ERROR, s);
158 System.out.println("stack trace follows: ");
159 stackTrace();
160 }
161
162
163 public static void fatalError(String s) {
164 report(FATAL, s);
165 stackTrace();
166 System.exit(0);
167 }
168
169
170 public static void override(String s) {
171 report(MISSING, "method should be overridden: " + s);
172 showSource();
173 }
174
175 public static void override() {
176 report(MISSING, "method should be overridden: ");
177 showSource();
178 }
179
180
181 public static void deprecate(String s) {
182 report(DEPRECATED, "using deprecated class: " + s + " " +
183 getShortSource());
184 showShortSource();
185 }
186
187 public static void deprecate() {
188 report(DEPRECATED, "using deprecated method " + getShortSource());
189 showSource();
190 }
191
192 public static void missing(String s) {
193 report(MISSING, "missing code needed: " + s);
194 showSource();
195 }
196
197
198 public static void missing() {
199 report(MISSING, "missing code needed");
200 showSource();
201 }
202
203
204 public static void shortMissing(String s) {
205 report(MISSING, s + getShortSource());
206 }
207
208 public static void shortMissingOnce(String s) {
209 String msg = "MISSING - " + s + getShortSource();
210 if (sentMessages.contains(msg)) {
211
212 } else {
213 sentMessages.add(msg);
214 report(MISSING, s + getShortSource());
215 }
216 }
217
218 public static void stackTrace() {
219 (new Exception()).printStackTrace();
220 }
221
222
223
224
225 public static void showSource() {
226 showSource(18);
227 }
228
229 public static void showShortSource() {
230 showSource(2);
231 }
232
233 public static void showSource(int n) {
234 StackTraceElement[] stea = (new Exception()).getStackTrace();
235 for (int i = 2; i < 2 + n && i < stea.length; i++) {
236 System.out.println(" at " + stea[i].toString());
237
238
239
240
241
242 }
243 }
244
245
246
247 public static String getMediumSource() {
248 String ret = "";
249 int n = 2;
250 StackTraceElement[] stea = (new Exception()).getStackTrace();
251 for (int i = 2; i < 2 + n && i < stea.length; i++) {
252 ret += " at " + stea[i].toString() + "\n";
253 }
254 return ret;
255 }
256
257
258 public static String getShortSource() {
259 StackTraceElement[] stea = (new Exception()).getStackTrace();
260 String ss = (" at " + stea[2].toString());
261 if (ss.equals(lastShortSource)) {
262 ss = "";
263 } else {
264 lastShortSource = ss;
265 }
266 return ss;
267 }
268
269
270
271 public static void delay() {
272 pause(200);
273 }
274
275
276 public static void pause(int n) {
277 try {
278
279 Thread.sleep(n);
280 } catch (Exception ex) {
281 }
282 }
283
284
285 public static void newLine() {
286 System.out.println("...");
287 }
288
289
290 public static void cacheAction(String s) {
291 cachedAction = s;
292 }
293
294 public static void reportCached() {
295 report(INFO, "may relate to: " + cachedAction);
296 }
297
298
299 public static void dump(String[] labs) {
300 if (labs != null) {
301 for (int i = 0; i < labs.length; i++) {
302 E.info("element " + i + ": " + labs[i]);
303 }
304 }
305 }
306
307
308 public static void dump(String s, int[] ia) {
309 E.info("int[] array: " + s + " " + ia.length);
310 for (int i = 0; i < ia.length; i++) {
311 E.info(" elt " + i + " = " + ia[i]);
312 }
313 }
314
315
316 public static void dump(String s, double[] da) {
317 E.info("double[] array: " + s + " " + da.length);
318 for (int i = 0; i < da.length; i++) {
319 E.info(" elt " + i + " = " + da[i]);
320 }
321 }
322
323
324 public static String toString(double[] pts) {
325 StringBuffer sb = new StringBuffer();
326 if (pts == null) {
327 sb.append("(null)");
328 } else {
329 sb.append("[");
330 for (int i = 0; i < pts.length; i++) {
331 if (i > 0) {
332 sb.append(", ");
333 }
334 sb.append(String.format("%.3g", new Double(pts[i])));
335 }
336
337 sb.append("]");
338 }
339 return sb.toString();
340 }
341
342
343 public static void warnOnce(String msg) {
344 if (sentMessages.contains(msg)) {
345
346 } else {
347 sentMessages.add(msg);
348 shortWarning(msg);
349 }
350
351 }
352
353
354
355 public static void report(int level, String s) {
356 if (levels == null) {
357 initLevels();
358 }
359 System.out.println(levels[level] + " - " + s);
360
361 if (reporter != null) {
362 if (level == INFO) {
363 reporter.reportInfo(s);
364
365 } else if (level == WARNING) {
366 reporter.reportWarning(s);
367
368 } else if (level == ERROR) {
369 reporter.reportError(s);
370 } else {
371 reporter.report(levels[level] + "- " + s);
372 }
373
374 }
375 }
376
377
378
379 }