Source code: com/gopas/rt/util/Message.java
1 /**********************************************************************
2 * GOPAS Software GmbH (c) 1999 Muenchen - All rights reserved
3 *
4 * $State: Exp $
5 * $Id: Message.java,v 1.1.1.1 2000/11/17 12:03:32 mathis Exp $
6 *
7 **********************************************************************/
8
9 package com.gopas.rt.util;
10
11 import java.awt.*;
12 import java.io.*;
13 import java.util.*;
14 import java.text.*;
15 import java.beans.*;
16
17 import javax.swing.*;
18
19 /**
20 * Message write several types of error messages and other messages
21 *
22 * some of them can be switched on/off and can throw Exceptions.
23 *
24 * Message is a Singleton. You can either use the static methods
25 * or a single Instance theMessage
26 *
27 * this version requires JDK1.2
28 *
29 * Usage: you create and register(add) one or more instances of a Trace
30 * subclass. These instances do the real output, some of them to a Stream
31 * and otheres pop up messages. Each Trace object can be given
32 * separate thresholds for error messages (the interface RT contains
33 * some useful constants).
34 *
35 * @see Trace
36 * @see DialogTrace
37 * @see LogTrace
38 *
39 **/
40 public class Message implements RT {
41
42 /**
43 * Convenience predicate class.
44 **/
45 public class Threshold implements Predicate {
46 public Threshold(int errorIsOn,
47 int warningIsOn,
48 int debugIsOn,
49 int verboseIsOn) {
50 this.errorIsOn = errorIsOn;
51 this.warningIsOn = warningIsOn;
52 this.debugIsOn = debugIsOn;
53 this.verboseIsOn = verboseIsOn;
54 }
55
56 /**
57 * <p>Teste, ob das Praedikat auf ein bestimmtes Objekt
58 * zutrifft. </p>
59 *
60 * @param obj das zu testende Objekt.
61 * @return true, falls das Praedikat zutrifft.
62 **/
63 public boolean test(Object obj) {
64 boolean retVal = false;
65 if (obj instanceof LogEntry) {
66 LogEntry entry = (LogEntry)obj;
67 switch(entry.getMsgType()) {
68 case ERROR_MSG:
69 retVal = (entry.getLevel() >= errorIsOn);
70 break;
71 case WARNING_MSG:
72 retVal = (entry.getLevel() >= warningIsOn);
73 break;
74 case DEBUG_MSG:
75 retVal = (entry.getLevel() >= debugIsOn);
76 break;
77 case VERBOSE_MSG:
78 retVal = (entry.getLevel() >= verboseIsOn);
79 break;
80 }
81 }
82 return retVal;
83 }
84
85 private int errorIsOn;
86 private int warningIsOn;
87 private int debugIsOn;
88 private int verboseIsOn;
89 }
90
91
92 /**
93 * no instances of this class
94 **/
95 protected Message() {}
96
97 /**
98 * Create a trace which writes to a Writer object.
99 *
100 * @param theWriter
101 * @param pred filtering predicate
102 */
103 public static LogTrace createLogTrace(PrintWriter theWriter, Predicate pred) {
104 LogTrace retVal = new LogTrace(theWriter, pred);
105 return retVal;
106 }
107
108 /**
109 * Create and register a trace which writes to a Writer object.
110 * @param theWriter stream where Messages are logged
111 * @param pred filtering predicate
112 */
113 public static void addLogTrace(PrintWriter theWriter, Predicate pred) {
114 add(createLogTrace(theWriter, pred));
115 }
116
117 /**
118 * Create a trace which opens a dialog window.
119 * @param baseComp root component for the dialog window.
120 * @param pred filtering predicate
121 **/
122 public static DialogTrace createDialogTrace(Component baseComp, Predicate pred) {
123 DialogTrace retVal = new DialogTrace(baseComp, pred);
124 return retVal;
125 }
126
127 /**
128 * Create and register a trace which opens a dialog window.
129 * @param baseComp root component for the dialog window.
130 * @param pred filtering predicate
131 */
132 public static DialogTrace addDialogTrace(
133 Component baseComp,
134 Predicate pred) {
135 DialogTrace retVal = createDialogTrace(baseComp, pred);
136 add(retVal);
137 return retVal;
138 }
139
140
141 /**
142 * Add/register trace to active trace set.
143 *
144 * @param tr trace to add
145 */
146 public static Trace add(Trace tr) {
147 traceSet.add(tr);
148 return tr;
149 }
150
151
152 /**
153 * Remove trace from the active trace set.
154 *
155 * @param tr trace to remove
156 */
157 public static void remove(Trace tr) {
158 traceSet.remove(tr);
159 }
160
161 /**
162 * Add error log entry.
163 *
164 * @param level
165 * @param msg
166 */
167 public static void error(Object host, String msg) {
168 error(host, msg, null, DEFAULT_ERROR_LEVEL, null);
169 }
170
171 /**
172 * Add error log entry.
173 *
174 * @param level
175 * @param msg
176 */
177 public static void errorNoRepeat(Object host, String msg) {
178 errorNoRepeat(host, msg, null, DEFAULT_ERROR_LEVEL, null);
179 }
180
181 /**
182 * Add error log entry.
183 *
184 * @param level
185 * @param msg
186 */
187 public static void error(Object host, Exception ex, String msg) {
188 error(host, msg, null, DEFAULT_ERROR_LEVEL, ex);
189 }
190
191 /**
192 * Add error log entry.
193 *
194 * @param level
195 * @param msg
196 */
197 public static void errorNoRepeat(Object host, Exception ex, String msg) {
198 errorNoRepeat(host, msg, null, DEFAULT_ERROR_LEVEL, ex);
199 }
200
201 /**
202 * Add error log entry.
203 *
204 * @param host
205 * @param msg
206 * @param msgParm
207 * @param level
208 * @param ex
209 */
210 public static void error(
211 Object host,
212 String msg,
213 Object[] msgParm,
214 int level,
215 Exception ex) {
216 if (msgParm != null) {
217 msg = MessageFormat.format( msg, msgParm );
218 }
219 LogEntry entry = new LogEntry(repHostFunctor, ERROR_MSG, host, msg, level, ex);
220 out(entry);
221 }
222
223
224 /**
225 * Add error log entry.
226 *
227 * @param host
228 * @param msg
229 * @param msgParm
230 * @param level
231 * @param ex
232 */
233 public static void errorNoRepeat(
234 Object host,
235 String msg,
236 Object[] msgParm,
237 int level,
238 Exception ex) {
239 if (msgParm != null) {
240 msg = MessageFormat.format( msg, msgParm );
241 }
242 if ( ! msgSet.contains(msg)) {
243 LogEntry entry =
244 new LogEntry(repHostFunctor, ERROR_MSG, host, msg, level, ex);
245 out(entry);
246 }
247 }
248
249
250 /**
251 *
252 *
253 * @param level
254 * @param msg
255 */
256 public static void warning(Object host, String msg) {
257 warning(host, msg, null, DEFAULT_WARNING_LEVEL, null);
258 }
259
260 /**
261 * Add warning log entry.
262 *
263 * @param host
264 * @param msg
265 * @param msgParm
266 * @param level
267 * @param ex
268 */
269 public static void warning(
270 Object host,
271 String msg,
272 Object[] msgParm,
273 int level,
274 Exception ex) {
275 if (msgParm != null) {
276 msg = MessageFormat.format( msg, msgParm );
277 }
278 LogEntry entry = new LogEntry(repHostFunctor, WARNING_MSG, host, msg, level, ex);
279 out(entry);
280 }
281 // public static boolean getResourceBoolean(String bundleName,
282 // String resName,
283 // boolean defaultVal,
284 // boolean quiet) {
285
286 /**
287 *
288 *
289 * @param level
290 * @param msg
291 */
292 public static void debug(Predicate pred, Object host, String msg) {
293 if (pred.test(host)) {
294 debug(host, msg, null, DEFAULT_DEBUG_LEVEL, null);
295 }
296 }
297
298 /**
299 *
300 *
301 * @param level
302 * @param msg
303 */
304 public static void debug(Object host, String msg) {
305 debug(host, msg, null, DEFAULT_DEBUG_LEVEL, null);
306 }
307
308 /**
309 * Add debug log entry.
310 *
311 * @param host
312 * @param msg
313 * @param msgParm
314 * @param level
315 * @param ex
316 */
317 public static void debug(
318 Predicate pred,
319 Object host,
320 String msg,
321 Object[] msgParm,
322 int level,
323 Exception ex) {
324 if (pred.test(host)) {
325 debug( host, msg, msgParm, level, ex);
326 }
327 }
328
329 /**
330 * Add debug log entry.
331 *
332 * @param host
333 * @param msg
334 * @param msgParm
335 * @param level
336 * @param ex
337 */
338 public static void debug(
339 Object host,
340 String msg,
341 Object[] msgParm,
342 int level,
343 Exception ex) {
344 if (msgParm != null) {
345 msg = MessageFormat.format( msg, msgParm );
346 }
347 boolean doDebug = doDebugAll;
348 if (host instanceof Class) doDebug = shouldDebug((Class)host);
349 else if (host != null) doDebug = shouldDebug(host.getClass());
350 if (doDebug) {
351 LogEntry entry =
352 new LogEntry(repHostFunctor, DEBUG_MSG, host, msg, level, ex);
353 out(entry);
354 }
355 }
356
357 /**
358 *
359 *
360 * @param level
361 * @param msg
362 */
363 public static void verbose(Object host, String msg) {
364 verbose(host, msg, null, DEFAULT_VERBOSE_LEVEL, null);
365 }
366
367 /**
368 * Add verbose log entry.
369 *
370 * @param host
371 * @param msg
372 * @param msgParm
373 * @param level
374 * @param ex
375 */
376 public static void verbose(
377 Object host,
378 String msg,
379 Object[] msgParm,
380 int level,
381 Exception ex) {
382 if (msgParm != null) {
383 msg = MessageFormat.format( msg, msgParm );
384 }
385 LogEntry entry = new LogEntry(repHostFunctor, VERBOSE_MSG, host, msg, level, ex);
386 out(entry);
387 }
388
389
390 /**
391 * Send log entry to all traces in the active trace set. If the trace set
392 * is empty send log entry to stdout.
393 *
394 * @param entry log entry to send
395 **/
396 private static void out(LogEntry entry) {
397 if (traceSet.size() == 0) {
398 defaultTrace.out(entry);
399 } else {
400 Iterator iter = traceSet.iterator();
401 while (iter.hasNext()) {
402 Trace tr = (Trace)iter.next();
403 tr.out(entry);
404 }
405 }
406 }
407
408 public static boolean shouldDebug(Class aClass) {
409 boolean retVal = doDebugAll;
410 retVal |= debugSet.contains(aClass);
411 return retVal;
412 }
413
414 public static void shouldDebug(Class aClass, boolean flag) {
415 if (flag) {
416 debugSet.add(aClass);
417 } else {
418 debugSet.remove(aClass);
419 }
420 }
421
422 private static Set debugSet = new HashSet();
423
424 /**
425 * Get the value of repHostFunctor.
426 * @return Value of repHostFunctor.
427 */
428 public static Functor getRepHostFunctor() {return repHostFunctor;}
429
430 /**
431 * Set the value of repHostFunctor.
432 * @param v Value to assign to repHostFunctor.
433 */
434 public static void setRepHostFunctor(Functor v) {repHostFunctor = v;}
435
436 /** the functor used to represent message hosts */
437 private static Functor repHostFunctor = Functors.Ident;
438
439 /** active trace set */
440 private static final Set traceSet = new HashSet();
441
442 /** set of messages already sent. **/
443 private static final Set msgSet = new HashSet();
444
445 /** default trace to stdout */
446 private static final Trace defaultTrace = new LogTrace(
447 new PrintWriter(System.out, true),
448 Predicates.TRUE);
449
450 public static boolean doDebugAll = true;
451 }