Source code: openfuture/util/misc/Trace.java
1 // ***********************************************************************************
2 // ** THIS PROGRAM CONTAINS PROPRIETARY INFORMAION WHICH IS TRADE SECRET OF DEBIS **
3 // ** SYSTEMHAUS AND ALSO IS PROTECTED AS AN UNPULISHED WORK UNDER APPLICABLE **
4 // ** COPYRIGHT LAWS. THE PROGRAM IS TO BE RETAINED IN CONFIDENCE. ANY USE BY **
5 // ** THIRD PARTIES (E.G. USE AS A CONTROL PROGRAM, REPRODUCTION, MODOFICATION **
6 // ** AND TRANSLATION) IS GOVERNED SOLELY BY WRITTEN AGREEMENTS WITH DEBIS **
7 // ** SYSTEMHAUS. **
8 // ***********************************************************************************
9 //
10 // Configuration Management Information:
11 // -------------------------------------
12 // $Id: Trace.java,v 1.1.1.1 2001/07/08 18:29:34 wreissen Exp $
13 //
14 // Version History:
15 // ----------------
16 // $Log: Trace.java,v $
17 // Revision 1.1.1.1 2001/07/08 18:29:34 wreissen
18 // initial version registered ad SourceForge
19 //
20 // Revision 1.1 2000/09/27 15:53:24 wreissen
21 // moved to openfuture.
22 //
23 // Revision 1.2 2000/09/03 18:07:28 wreissen
24 // deprecated readline corrected.
25 //
26 // Revision 1.1 2000/02/14 06:49:51 wreissen
27 // initial version.
28 //
29 // Revision 1.1 1999/11/15 12:45:13 zv332
30 // class copied from Jawa package
31 //
32 // Revision 2.11 1999/05/19 17:22:36 zv152
33 // major cleanups & performance improvements
34 //
35 // Revision 2.10 1999/02/18 15:22:39 zv152
36 // - repackaged
37 //
38 // Revision 2.9 1999/02/09 10:05:46 s.balz
39 // - set default output mode to TD_CONSOLE
40 //
41 // Revision 2.8 1999/01/29 14:31:34 s.balz
42 // - merge from DSC activities (see rev. comments for details)
43 //
44 // Revision 2.7 1999/01/29 14:15:27 s.balz
45 // - cleanups & added javadoc comments
46 // - added closeOutputFile
47 //
48 // Revision 2.6 1998/11/05 13:54:35 s.balz
49 // - cosmetic changes to getCurrentDateAndTimeString
50 //
51 // Revision 2.5 1998/11/05 13:48:10 s.balz
52 // - cosmetic changes to getStatusString
53 // - minor bug in getCurrentDateAndTimeString (return value was not
54 // initialized to "" but to null)
55 // - minor bug in hasDebugFlagSet (method returned true when a class
56 // was not registered)
57 //
58 // Revision 2.4 1998/11/04 16:06:57 s.balz
59 // - added getStatusString and getDetailedStatusString + some cleanups
60 //
61 // Revision 2.3 1998/11/04 14:17:00 s.balz
62 // - added user(...) methods with second string parameter that
63 // is used as prefix
64 //
65 // Revision 2.2 1998/11/04 14:15:25 s.balz
66 // - added TT_OFF, TT_DATE, TT_TIME, TT_TIME_MS to control
67 // date & time tracing
68 // - added support for millisecond tracing
69 // - default output file is %JAVA_HOME%\trace.log
70 //
71 // Revision 2.1 1998/10/30 13:13:23 s.balz
72 // - setTraceModeDefault setOutputMode etc eingebaut
73 //
74 // Revision 2.0 1998/10/21 16:38:04 s.balz
75 // - first version in context DSC
76 //
77 // Revision 1.9 1998/09/25 17:04:46 s.balz
78 // - added support for file output
79 //
80 // Revision 1.8 1998/03/17 14:47:38 s.balz
81 // - added feature: if traceflag not found -> warning and add 0 value to permissions
82 //
83 // Revision 1.7 1998/03/16 16:52:42 s.balz
84 // - exceptions are not maskable and are therefore always displayed !!!
85 //
86 // Revision 1.6 1998/03/16 16:08:52 s.balz
87 // - changed userdef to user
88 //
89 // Revision 1.5 1998/03/16 16:04:04 s.balz
90 // - added WARN & USER
91 //
92 // Revision 1.4 1998/02/26 14:59:23 s.balz
93 // - minor modifications in printContent
94 // - added print message if object class is not contained in hashtable
95 //
96 // Revision 1.3 1998/02/17 13:23:09 s.balz
97 // - added support for Class Methods
98 //
99 // Revision 1.2 1998/02/17 12:24:39 s.balz
100 // - minor bug fix in hasDebugFlagSet
101 //
102 // Revision 1.1 1998/02/17 09:50:42 s.balz
103 // Initial revision
104 //
105 // *******************************************************************************
106 package openfuture.util.misc;
107
108
109 import java.util.*;
110 import java.io.*;
111 import java.net.*;
112
113 // *******************************************************************************
114 /**
115 * The Trace class provides static methods that can be used in Java Applets and
116 * Applications to output trace messages to a trace device. Traces can be activated
117 * on a per class basis and for each class a combination of any of the following
118 * trace levels is possible:
119 *
120 * 0.ERROR: should be used in case of severe errors
121 * 1.WARNING: should be used in case of warnings and uncritical errors
122 * 2.IN: should be used as the first statement in a method
123 * 3.OUT: should be used before existing a method
124 * 4.PARAMETER: should be used to trace out method parameters
125 * 5.MESSAGE: should be used to trace out any message within the method body
126 * 6.USER-DEFINED: user defined trace level
127 * 7.EXCEPTION: should be used when an exception occurs (is not maskable)
128 *
129 * The trace mechanism is initialized via a config file that contains a bit mask
130 * for each class that uses traces, e.g.
131 *
132 * util.email.MailData: 0
133 * util.email.MailSender: 0
134 * jawa.fzbewertung.AufnahmeGUI: 255
135 * jawa.fzbewertung.CICSData: 3
136 * jawa.fzbewertung.CreateMapApplet: 3
137 *
138 * The value corresponds to the combination of trace levels 0. - 6. where each
139 * trace level is activated by setting the appropriate bit to 1, e.g. a value
140 * of 3 (0000 0011 in binary mode) means that only ERRORS and WARNING are going
141 * to be traced for a given class.
142 *
143 * The config file can either be read via HTTP using the method readTraceConfig
144 * or directly from the local file system using the method readTraceConfigFromFile.
145 *
146 * @author Stefan Balz
147 * @version $Revision: 1.1.1.1 $
148 */
149 // ********************************************************************************
150 abstract public class Trace
151 // ********************************************************************************
152 {
153 private static Hashtable permissions = new Hashtable(50,50);
154
155 private static String[] MESSAGE_TYPE_STRINGS =
156 {
157 " ERROR : ",
158 " WARNING : ",
159 " IN : ",
160 " OUT : ",
161 " PARAM : ",
162 " MESSAGE : ",
163 " USER : ",
164 " EXCEPTION : ",
165 };
166
167 private static final int TRACE_ERR = 1;
168 private static final int TRACE_WARN = 2;
169 private static final int TRACE_IN = 4;
170 private static final int TRACE_OUT = 8;
171 private static final int TRACE_PARAM = 16;
172 private static final int TRACE_MSG = 32;
173 private static final int TRACE_USER = 64;
174 private final static String[] TRACE_BITS =
175 {
176 "TRACE_ERR",
177 "TRACE_WARN",
178 "TRACE_IN",
179 "TRACE_OUT",
180 "TRACE_PARAM",
181 "TRACE_MSG",
182 "TRACE_USER",
183 };
184
185 public final static int TL_OFF = 0;
186 public final static int TL_LOW = TRACE_ERR ;
187 public final static int TL_MEDIUM = TL_LOW | TRACE_WARN;
188 public final static int TL_VERBOSE = TL_MEDIUM | TRACE_MSG | TRACE_USER | TRACE_IN | TRACE_OUT;
189
190 public final static String TL_OFF_STRING = "OFF";
191 public final static String TL_LOW_STRING = "LOW";
192 public final static String TL_MEDIUM_STRING = "MEDIUM";
193 public final static String TL_VERBOSE_STRING = "VERBOSE";
194
195 public final static int TD_CONSOLE = 1;
196 public final static int TD_FILE = 2;
197 public final static int TD_ALL = 3;
198 private final static String[] TD =
199 {
200 "",
201 "CONSOLE",
202 "FILE",
203 "ALL",
204 };
205
206 public final static int TT_OFF = 0;
207 public final static int TT_DATE = 1;
208 public final static int TT_TIME = 2;
209 public final static int TT_TIME_MS = 4;
210 private final static String[] TT =
211 {
212 "OFF",
213 "DATE",
214 "TIME",
215 "DATE and TIME",
216 "TIME_MS",
217 "DATE and TIME_MS (!nonsense!)",
218 "TIME and TIME_MS",
219 "DATE and TIME and TIME_MS",
220 };
221
222 private static int dateAndTimeMode = TT_OFF;
223 private static int outputMode = TD_CONSOLE;
224 private static PrintWriter outputStream = null;
225 private static int defaultBitMask = 0;
226
227 private static GregorianCalendar calendar = new GregorianCalendar();
228 private static final String DEFAULT_TRACE_FILENAME = "trace.log";
229 private static String traceFilename = DEFAULT_TRACE_FILENAME;
230
231
232
233 // ********************************************************************************
234 /**
235 * Describe 'error' method here.
236 *
237 * @param theObject a value of type 'Object'
238 * @param msg a value of type 'String'
239 */
240 public static void error(Object theObject, String msg)
241 // ********************************************************************************
242 {
243 if (hasDebugFlagSet(theObject, TRACE_ERR))
244 if (theObject instanceof java.lang.String)
245 {
246 trace(theObject + " *** ERROR*** " + msg);
247 }
248 else
249 {
250 trace(theObject.getClass().getName() + " *** ERROR*** " + msg);
251 }
252 }
253
254
255 // ********************************************************************************
256 /**
257 * Describe 'error' method here.
258 *
259 * @param theObject object reference (this pointer) to the calling object
260 * @param msg the trace output message
261 * @param nRID a request ID used for identifying multiple instances of the same class
262 */
263 public static void error(Object theObject, String msg, int nRID)
264 // ********************************************************************************
265 {
266 if (hasDebugFlagSet(theObject, TRACE_ERR))
267 trace("ERR: " + msg, nRID);
268 }
269
270
271 // ********************************************************************************
272 /**
273 * Describe 'exception' method here.
274 *
275 * @param theObject object reference (this pointer) to the calling object
276 * @param msg the trace output message
277 */
278 public static void exception(Object theObject, String msg)
279 // ********************************************************************************
280 {
281 if (theObject instanceof java.lang.String)
282 {
283 trace(theObject + " *** EXCEPTION *** " + msg);
284 }
285 else
286 {
287 trace(theObject.getClass().getName() + " *** EXCEPTION *** " + msg);
288 }
289 }
290
291
292 // ********************************************************************************
293 /**
294 * Describe 'exception' method here.
295 *
296 * @param theObject object reference (this pointer) to the calling object
297 * @param msg the trace output message
298 * @param nRID a request ID used for identifying multiple instances of the same class
299 */
300 public static void exception(Object theObject, String msg, int nRID)
301 // ********************************************************************************
302 {
303 trace("EXC: " + msg, nRID);
304 }
305
306
307
308 // ********************************************************************************
309 /**
310 * Describe 'in' method here.
311 *
312 * @param theObject object reference (this pointer) to the calling object
313 * @param msg the trace output message
314 */
315 public static void in(Object theObject, String msg)
316 // ********************************************************************************
317 {
318 if (hasDebugFlagSet(theObject, TRACE_IN))
319 if (theObject instanceof java.lang.String)
320 {
321 trace(theObject + "::" + msg + "++");
322 }
323 else
324 {
325 trace(theObject.getClass().getName() + "::" + msg + "++");
326 }
327 }
328
329
330 // ********************************************************************************
331 /**
332 * Describe 'message' method here.
333 *
334 * @param theObject object reference (this pointer) to the calling object
335 * @param msg the trace output message
336 */
337 public static void message(Object theObject, String msg)
338 // ********************************************************************************
339 {
340 if (hasDebugFlagSet(theObject, TRACE_MSG))
341 if (theObject instanceof java.lang.String)
342 {
343 trace(theObject + " MESSAGE: " + msg);
344 }
345 else
346 {
347 trace(theObject.getClass().getName() + " MESSAGE: " + msg);
348 }
349 }
350
351
352 // ********************************************************************************
353 /**
354 * Describe 'message' method here.
355 *
356 * @param theObject object reference (this pointer) to the calling object
357 * @param msg the trace output message
358 * @param nRID a request ID used for identifying multiple instances of the same class
359 */
360 public static void message(Object theObject, String msg, int nRID)
361 // ********************************************************************************
362 {
363 if (hasDebugFlagSet(theObject, TRACE_MSG))
364 trace("MSG: " + msg, nRID);
365 }
366
367
368
369
370 // ********************************************************************************
371 /**
372 * Describe 'out' method here.
373 *
374 * @param theObject object reference (this pointer) to the calling object
375 * @param msg the trace output message
376 */
377 public static void out(Object theObject, String msg)
378 // ********************************************************************************
379 {
380 if (hasDebugFlagSet(theObject, TRACE_OUT))
381 if (theObject instanceof java.lang.String)
382 {
383 trace(theObject + "::" + msg + "--");
384 }
385 else
386 {
387 trace(theObject.getClass().getName() + "::" + msg + "--");
388 }
389 }
390
391
392 // ********************************************************************************
393 /**
394 * Describe 'param' method here.
395 *
396 * @param theObject object reference (this pointer) to the calling object
397 * @param msg the trace output message
398 */
399 public static void param(Object theObject, String msg)
400 // ********************************************************************************
401 {
402 if (hasDebugFlagSet(theObject, TRACE_PARAM))
403 if (theObject instanceof java.lang.String)
404 {
405 trace(theObject + " PARAM: " + msg);
406 }
407 else
408 {
409 trace(theObject.getClass().getName() + " PARAM: " + msg);
410 }
411 }
412
413
414 // ********************************************************************************
415 /**
416 * Describe 'user' method here.
417 *
418 * @param theObject object reference (this pointer) to the calling object
419 * @param msg the trace output message
420 */
421 public static void user(Object theObject, String msg)
422 // ********************************************************************************
423 {
424 if (hasDebugFlagSet(theObject, TRACE_USER))
425 if (theObject instanceof java.lang.String)
426 {
427 trace(theObject + " USER DEFINED: " + msg);
428 }
429 else
430 {
431 trace(theObject.getClass().getName() + " USER DEFINED: " + msg);
432 }
433 }
434
435
436 // ********************************************************************************
437 /**
438 * Describe 'user' method here.
439 *
440 * @param theObject object reference (this pointer) to the calling object
441 * @param msg the trace output message
442 * @param nRID a request ID used for identifying multiple instances of the same class
443 */
444 public static void user(Object theObject, String msg, int nRID)
445 // ********************************************************************************
446 {
447 if (hasDebugFlagSet(theObject, TRACE_USER))
448 trace(" USR: " + msg, nRID);
449 }
450
451
452 // ********************************************************************************
453 /**
454 * Describe 'user' method here.
455 *
456 * @param theObject object reference (this pointer) to the calling object
457 * @param prefix a value of type 'String'
458 * @param msg the trace output message
459 */
460 public static void user(Object theObject, String prefix, String msg)
461 // ********************************************************************************
462 {
463 if (hasDebugFlagSet(theObject, TRACE_USER))
464 if (theObject instanceof java.lang.String)
465 {
466 trace(theObject + " " + prefix + ": " + msg);
467 }
468 else
469 {
470 trace(theObject.getClass().getName() + " " + prefix + ": " + msg);
471 }
472 }
473
474
475 // ********************************************************************************
476 /**
477 * Describe 'user' method here.
478 *
479 * @param theObject object reference (this pointer) to the calling object
480 * @param prefix a value of type 'String'
481 * @param msg the trace output message
482 * @param nRID a request ID used for identifying multiple instances of the same class
483 */
484 public static void user(Object theObject, String prefix, String msg, int nRID)
485 // ********************************************************************************
486 {
487 if (hasDebugFlagSet(theObject, TRACE_USER))
488 trace(prefix + ": " + msg, nRID);
489 }
490
491
492
493
494 // ********************************************************************************
495 /**
496 * Describe 'warning' method here.
497 *
498 * @param theObject object reference (this pointer) to the calling object
499 * @param msg the trace output message
500 */
501 public static void warning(Object theObject, String msg)
502 // ********************************************************************************
503 {
504 if (hasDebugFlagSet(theObject, TRACE_WARN))
505 if (theObject instanceof java.lang.String)
506 {
507 trace(theObject + " WARNING: " + msg);
508 }
509 else
510 {
511 trace(theObject.getClass().getName() + " WARNING: " + msg);
512 }
513 }
514
515
516 // ********************************************************************************
517 /**
518 * Describe 'warning' method here.
519 *
520 * @param theObject object reference (this pointer) to the calling object
521 * @param msg the trace output message
522 * @param nRID a request ID used for identifying multiple instances of the same class
523 */
524 public static void warning(Object theObject, String msg, int nRID)
525 // ********************************************************************************
526 {
527 if (hasDebugFlagSet(theObject, TRACE_WARN))
528 trace("WRN: " + msg, nRID);
529 }
530
531
532 // ********************************************************************************
533 /**
534 * is used to retrieve information about the config file that is currently in use.
535 * The method returns a string containing the registred class names and the
536 * corresponding bit mask value in a somehow 'preformatted' way.
537 *
538 * @return a string containing the registred class names and the
539 * corresponding bit mask value in a somehow 'preformatted' way.
540 */
541 // ********************************************************************************
542 public static String printContent()
543 // ********************************************************************************
544 {
545 return getStatusString();
546 }
547
548
549 // ********************************************************************************
550 /**
551 * tries to read the config file given as a parameter and sets up the internal
552 * hashtable.
553 *
554 * @param urlPrefix URL path pointing to the directory where the config file
555 * is stored.
556 * @param filename name the config file
557 * @return returns 0 upon success and -1 when something went wrong (Yep, I
558 * know I could have done this which some exceptions...)
559 *
560 */
561 // ********************************************************************************
562 public static int readTraceConfig(String urlPrefix, String filename)
563 // ********************************************************************************
564 {
565 URL myURL=null;
566 BufferedReader inStream;
567 String zeile, klasse, traceFlag;
568 int flag;
569 char c;
570
571 try
572 {
573 myURL = new URL(urlPrefix + filename);
574
575 inStream = new BufferedReader(new InputStreamReader(myURL.openStream()));
576
577 do
578 {
579 zeile = inStream.readLine();
580 if (zeile != null)
581 {
582 // retrieve the Langtext
583 int pos = zeile.indexOf(':');
584 if (-1 == pos)
585 {
586 System.out.println("TRACE::readTraceConfig - INVALID CONFIG FILE ENTRY");
587 return -1;
588 }
589
590 klasse = substring(zeile,0,pos);
591
592 pos++;
593 // skip the blanks and tabs that follow the langText
594 do
595 {
596 c = zeile.charAt(pos);
597 pos++;
598 }
599 while ((c == ' ') || (c == '\t'));
600
601 traceFlag = zeile.substring(pos-1);
602 flag = stringToInt(traceFlag,255);
603
604 permissions.put(klasse, new Integer(flag));
605 }
606 }
607 while (null != zeile);
608 }
609 catch (NullPointerException npex)
610 {
611 System.out.println(npex + " while trying to read " + myURL);
612 }
613 catch (MalformedURLException urlex)
614 {
615 System.out.println(urlex + " while trying to read " + myURL);
616 return -1;
617 }
618 catch (IOException ioex)
619 {
620 System.out.println(ioex + " while trying to read " + myURL);
621 return -1;
622 }
623 return 0;
624 }
625
626
627 // ********************************************************************************
628 /**
629 * tries to read the config file given as a parameter and sets up the internal
630 * hashtable.
631 *
632 * @param filename complete path that points to the config file
633 * @return returns 0 upon success and -1 when something went wrong (Yep, I
634 * know I could have done this which some exceptions...)
635 *
636 */
637 // ********************************************************************************
638 public static int readTraceConfigFromFile(String filename)
639 // ********************************************************************************
640 {
641 BufferedReader inStream;
642 String zeile, klasse, traceFlag;
643 int flag;
644 char c;
645
646 try
647 {
648 inStream = new BufferedReader(new FileReader(filename));
649
650 do
651 {
652 zeile = inStream.readLine();
653 if (zeile != null)
654 {
655 // retrieve the Langtext
656 int pos = zeile.indexOf(':');
657 if (-1 == pos)
658 {
659 System.out.println("TRACE::readTraceConfig - INVALID CONFIG FILE ENTRY");
660 return -1;
661 }
662
663 klasse = substring(zeile,0,pos);
664
665 pos++;
666 // skip the blanks and tabs that follow the langText
667 do
668 {
669 c = zeile.charAt(pos);
670 pos++;
671 }
672 while ((c == ' ') || (c == '\t'));
673
674 traceFlag = zeile.substring(pos-1);
675 flag = stringToInt(traceFlag,255);
676
677 permissions.put(klasse, new Integer(flag));
678 }
679 }
680 while (null != zeile);
681 }
682 catch (NullPointerException npex)
683 {
684 System.out.println(npex + " while trying to read: " + filename);
685 }
686 catch (IOException ioex)
687 {
688 System.out.println(ioex + " while trying to read: " + filename);
689 return -1;
690 }
691 return 0;
692 }
693
694
695 // ********************************************************************************
696 /**
697 * is used to retrieve information about the config file that is currently in use.
698 * The method returns a string containing the registred class names and the
699 * corresponding bit mask value in a somehow 'preformatted' way.
700 *
701 * @return a string containing the registred class names and the
702 * corresponding bit mask value in a somehow 'preformatted' way.
703 */
704 // ********************************************************************************
705 public static String getDetailedStatusString()
706 // ********************************************************************************
707 {
708 String result = getStatusString() +
709 "\n\nDETAILED TRACE INFORMATION:\n---------------------------\n\n" ;
710 Enumeration perm = permissions.keys();
711 while (perm.hasMoreElements())
712 {
713 Object myObject = perm.nextElement();
714 int bits = ((Integer) permissions.get(myObject)).intValue();
715 result = result + "\t" + String.valueOf(bits) + " :\t" +
716 myObject.toString() +"\n";
717 }
718 return result;
719 }
720
721
722 // ********************************************************************************
723 /**
724 * is used to retrieve information about the config file that is currently in use.
725 * The method returns a string containing the registred class names and the
726 * corresponding bit mask value in a somehow 'preformatted' way.
727 *
728 * @return a string containing the registred class names and the
729 * corresponding bit mask value in a somehow 'preformatted' way.
730 */
731 // ********************************************************************************
732 public static String getStatusString()
733 // ********************************************************************************
734 {
735 StringBuffer result=new StringBuffer();
736 result.append("\n\nTrace settings:\n---------------\n\n\tTrace level : ");
737
738 switch (defaultBitMask)
739 {
740 case TL_OFF:
741 {
742 result.append(TL_OFF_STRING).append("\n");
743 break;
744 }
745 case TL_LOW:
746 {
747 result.append(TL_LOW_STRING).append("\n");
748 break;
749 }
750 case TL_MEDIUM:
751 {
752 result.append(TL_MEDIUM_STRING).append("\n");
753 break;
754 }
755 case TL_VERBOSE:
756 {
757 result.append(TL_VERBOSE_STRING).append("\n");
758 break;
759 }
760 default:
761 {
762 result.append("<UNDEFINED>\n");
763 break;
764 }
765 }
766
767 result.
768 append("\toutput mode : ").
769 append(TD[outputMode]).
770 append("\n");
771
772 if (0 != (TD_FILE & outputMode))
773 result.
774 append("\tfile name : ").
775 append(traceFilename).
776 append("\n");
777
778 result.
779 append("\ttime+date trace : ").
780 append(TT[dateAndTimeMode]).
781 append("\n");
782
783 return result.toString();
784 }
785
786 // ********************************************************************************
787 /**
788 * determines if date and time (and eventually milliseconds) will be used as
789 * a prefix for the trace output.
790 *
791 * @param flag a bitwise combination of TT_DATE, TT_TIME, TT_TIME_MS
792 */
793 // ********************************************************************************
794 public static void setDateAndTimeMode(int flag)
795 // ********************************************************************************
796 {
797 dateAndTimeMode = flag;
798 }
799
800
801 // ********************************************************************************
802 /**
803 * determines the output mode. It can either be a file or the console or both.
804 *
805 * @param flag the output mode flag. This is either TD_FILE, TD_CONSOLE or TD_ALL
806 * @param filename the name of the output file (if TD_FILE or TD_ALL is chosen)
807 */
808 public static void setOutputMode(int flag, String filename)
809 // ********************************************************************************
810 {
811 traceFilename = filename;
812 outputMode = flag;
813 if (TD_FILE == (outputMode & TD_FILE))
814 {
815 String fileSeparator = System.getProperty("file.separator");
816 String javaHome = System.getProperty("java.home");
817
818 if (null != filename)
819 createOutputFileFile(filename);
820 else
821 createOutputFileFile(javaHome + fileSeparator + DEFAULT_TRACE_FILENAME);
822 }
823 }
824
825
826 // ********************************************************************************
827 /**
828 * closes the outputStream in case of TD_FILE & TD_ALL
829 */
830 public static void closeOutputFile()
831 // ********************************************************************************
832 {
833 if (TD_FILE == (outputMode & TD_FILE))
834 {
835 outputStream.flush();
836 outputStream.close();
837 }
838 }
839
840
841 // ********************************************************************************
842 /**
843 * sets the trace bit mask for a given class manually.
844 *
845 * @param theObject the object instance (upon which the class will be determined)
846 * @param flag the trace bit flag
847 */
848 // ********************************************************************************
849 public static void setTraceMode(Object theObject, int flag)
850 // ********************************************************************************
851 {
852 permissions.put(theObject.getClass().getName(), new Integer(flag));
853 }
854
855
856 // ********************************************************************************
857 /**
858 * sets the trace default bit mask. This bit mask is used if the trace mechanism
859 * encounters a class that has not yet been registered.
860 *
861 * @param flag the default trace bit flag
862 */
863 // ********************************************************************************
864 public static void setTraceModeDefault(int flag)
865 // ********************************************************************************
866 {
867 defaultBitMask = flag;
868 }
869
870
871 // ********************************************************************************
872 /**
873 * This method is being called by the method setOutputMode and creates a file
874 * for the logging of trace information.
875 *
876 * @param filename the name of the file given as 'String'
877 */
878 private static void createOutputFileFile(String filename)
879 // ********************************************************************************
880 {
881 try
882 {
883 outputStream = new PrintWriter(new FileOutputStream(filename));
884 }
885 catch (IOException ioex)
886 {
887 System.out.println("Trace::setOutputToFile - unable to open OutputStream!");
888 outputMode = outputMode & ( ~ TD_FILE ) ;
889 }
890 }
891
892
893 // ********************************************************************************
894 /**
895 * Describe 'getCurrentDateAndTimeString' method here.
896 *
897 * @return a value of type 'String'
898 */
899 private static String getCurrentDateAndTimeString()
900 // ********************************************************************************
901 {
902 StringBuffer result = new StringBuffer();
903
904 String hString;
905 String mString;
906 String sString;
907 String msString;
908 StringBuffer monthString = new StringBuffer();
909 String yearString;
910 StringBuffer dateString = new StringBuffer();
911
912 calendar.setTime(new Date());
913
914 if (0 != (dateAndTimeMode & TT_DATE))
915 {
916 int month = calendar.get(Calendar.MONTH) + 1;
917 int date = calendar.get(Calendar.DATE);
918 yearString = String.valueOf(calendar.get(Calendar.YEAR));
919
920 if (month < 10)
921 monthString.append("0").append(month);
922 else
923 monthString.append(month);
924
925 if (date < 10)
926 dateString.append("0").append(date);
927 else
928 dateString.append(date);
929
930 result.append(dateString).append(".").append(monthString).append(".").append(yearString);
931 }
932
933 if (0 != (dateAndTimeMode & TT_TIME))
934 {
935 int h = calendar.get(Calendar.HOUR);
936 int m = calendar.get(Calendar.MINUTE);
937 int s = calendar.get(Calendar.SECOND);
938
939 if (h < 10)
940 hString = "0" + h;
941 else
942 hString = "" + h;
943
944 if (m < 10)
945 mString = "0" + m;
946 else
947 mString = "" + m;
948
949 if (s < 10)
950 sString = "0" + s;
951 else
952 sString = "" + s;
953
954 if (result.toString() != "")
955 result.append(" ").append(hString).
956 append(":").append(mString).append(":").append(sString);
957 else
958 result.append(hString).append(":").append(mString).append(":").append(sString);
959 }
960
961 if (0 != (dateAndTimeMode & TT_TIME_MS))
962 {
963 msString = String.valueOf(calendar.get(Calendar.MILLISECOND));
964 for (int i=msString.length(); i<3; i++)
965 msString = "0" + msString;
966
967 result.append(".").append(msString);
968 }
969
970 return result.append(" ").toString();
971 }
972
973
974 // ********************************************************************************
975 /**
976 * is used to perform the check if a class (given as an object instance) has activated
977 * a given trace level.
978 *
979 * @param theObject the object to check
980 * @param traceflag the traceflag as an int
981 *
982 * @return true, if this trace mode is activated, false otherwise
983 */
984 // ********************************************************************************
985 private static boolean hasDebugFlagSet(Object theObject, int traceflag)
986 // ********************************************************************************
987 {
988 Integer value;
989
990 // determine if the parameter theObject is of type String or if it
991 // is a genuine object
992 if (theObject instanceof java.lang.String)
993 value =(Integer)permissions.get((String)theObject);
994 else
995 value =(Integer)permissions.get(theObject.getClass().getName());
996
997 if (null != value)
998 if (traceflag == (value.intValue() & traceflag))
999 return true;
1000 else
1001 return false;
1002 else
1003 {
1004 permissions.put(theObject.getClass().getName(), new Integer(defaultBitMask));
1005 if (traceflag == (defaultBitMask & traceflag))
1006 return true;
1007 else
1008 return false;
1009 }
1010 }
1011
1012
1013// // ********************************************************************************
1014// /**
1015// * is used to perform the check if a class has activated a given trace level.
1016// *
1017// * @param theObjectClass the name of the class
1018// * @param traceflag the traceflag as an int
1019// *
1020// * @return true, if this trace mode is activated, false otherwise
1021// */
1022// // ********************************************************************************
1023// private static boolean hasDebugFlagSet(String theObjectClass, int traceflag)
1024// // ********************************************************************************
1025// {
1026// Integer value =(Integer)permissions.get(theObjectClass);
1027// if (null != value)
1028// if (traceflag == (value.intValue() & traceflag))
1029// return true;
1030// else
1031// return false;
1032// else
1033// {
1034// permissions.put(theObjectClass, new Integer(defaultBitMask));
1035// if (traceflag == (defaultBitMask & traceflag))
1036// return true;
1037// else
1038// return false;
1039// }
1040// }
1041
1042
1043 // ********************************************************************************
1044 /**
1045 * private method that does the same as the standard string to int conversion method
1046 * but catches the NumberFormatException and outputs an appropriate message. In case
1047 * of an exception the method return the default value def that has been passed to
1048 * the method as a parameter.
1049 *
1050 * @param s the string instance on which the method should operate
1051 * @param def the default value that should be returned in case of an exception
1052 *
1053 * @return the resulting int value
1054 */
1055 // ********************************************************************************
1056 private static int stringToInt(String s, int def)
1057 // ********************************************************************************
1058 {
1059 int val = def;
1060
1061 try
1062 {
1063 val = new Integer(s).intValue();
1064 }
1065 catch (NumberFormatException n1)
1066 {
1067 System.out.println("NumberFormatException: " + n1 +
1068 " in Trace::stringToInt parsing: " + s);
1069 val = def;
1070 }
1071 return val;
1072 }
1073
1074
1075 // ********************************************************************************
1076 /**
1077 * private method that does the same as the standard substring method but catches
1078 * the StringIndexOutOfBoundsException and outputs an appropriate message and returns
1079 * an empty string so that the calling instance can continue without worrying.
1080 *
1081 * @param s the string instance on which the method should operate
1082 * @param start the starting position
1083 * @param end the end position (exclusive!)
1084 *
1085 * @return the resulting substring
1086 */
1087 // ********************************************************************************
1088 private static String substring(String s, int start, int end)
1089 // ********************************************************************************
1090 {
1091 String result;
1092 try
1093 {
1094 result = s.substring(start,end);
1095 }
1096 catch (StringIndexOutOfBoundsException e1)
1097 {
1098 System.out.println(e1 + " in Trace::substring while performing substring(" +
1099 s + ", " + start + ", " + end + ")");
1100 return "";
1101 }
1102 return result;
1103 }
1104
1105
1106 // ********************************************************************************
1107 /**
1108 * used to output the message to the trace device. The output goes either to
1109 * the system console or to a file or to both devices.
1110 *
1111 * @param msg the message to be traced
1112 */
1113 // ********************************************************************************
1114 private static void trace(String msg)
1115 // ********************************************************************************
1116 {
1117 // add date and time to the message
1118 if (TT_OFF != dateAndTimeMode)
1119 msg = getCurrentDateAndTimeString() + msg;
1120
1121 if (TD_CONSOLE == outputMode)
1122 System.out.println(msg);
1123 else if (TD_FILE == outputMode)
1124 {
1125 outputStream.println(msg);
1126 outputStream.flush();
1127 }
1128 else if (TD_ALL == outputMode)
1129 {
1130 System.out.println(msg);
1131 outputStream.println(msg);
1132 outputStream.flush();
1133 }
1134 }
1135
1136
1137 // ********************************************************************************
1138 /**
1139 * used to output the message to the trace device. The output goes either to
1140 * the system console or to a file or to both devices.
1141 *
1142 * @param msg the message to be traced
1143 */
1144 // ********************************************************************************
1145 private static void trace(String msg, int nRID)
1146 // ********************************************************************************
1147 {
1148 // add date, time and request ID to the message
1149 msg = (TT_OFF != dateAndTimeMode) ?
1150 (getCurrentDateAndTimeString() + " " + nRID + " " + msg) :
1151 (Integer.toString (nRID) + " " + msg);
1152
1153 if (TD_CONSOLE == outputMode)
1154 System.out.println(msg);
1155 else if (TD_FILE == outputMode)
1156 {
1157 outputStream.println(msg);
1158 outputStream.flush();
1159 }
1160 else if (TD_ALL == outputMode)
1161 {
1162 System.out.println(msg);
1163 outputStream.println(msg);
1164 outputStream.flush();
1165 }
1166 }
1167}