KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > debug > Debug


1 /**
2  * com.mckoi.debug.Debug 21 May 1998
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.debug;
26
27 import java.io.PrintStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 /**
32  * This is a static class that should be used to output debugging information.
33  * Since all debug messages go through this class, we can easily turn the
34  * messages on and off, or specify output of different levels of debugging
35  * information. We can also filter out the debugging information and output
36  * it to different output streams.
37  *
38  * @author Tobias Downer
39  * @deprecated use DebugLogger implementations instead.
40  */

41
42 public final class Debug {
43
44   /**
45    * Set this to true if all alerts to messages are to be output to System.out.
46    * The purpose of this flag is to aid debugging.
47    */

48   private static final boolean PRINT_ALERT_TO_MESSAGES = false;
49
50   /**
51    * Set this to true to output all exceptions to System.err.
52    */

53   private static final boolean EXCEPTIONS_TO_ERR = false;
54
55
56   /**
57    * Some sample debug levels.
58    */

59   public final static int INFORMATION = 10; // General processing 'noise'
60
public final static int WARNING = 20; // A message of some importance
61
public final static int ALERT = 30; // Crackers, etc
62
public final static int ERROR = 40; // Errors, exceptions
63
public final static int MESSAGE = 10000; // Always printed messages
64
// (not error's however)
65

66   /**
67    * The debug lock object.
68    */

69   private static final Object JavaDoc debug_lock = new Object JavaDoc();
70
71   /**
72    * The PrintWriter for the system output stream.
73    */

74   static final PrintWriter JavaDoc SYSTEM_OUT = new PrintWriter JavaDoc(System.out, true);
75
76   /**
77    * The PrintWriter for the system error stream.
78    */

79   static final PrintWriter JavaDoc SYSTEM_ERR = new PrintWriter JavaDoc(System.err, true);
80
81
82   /**
83    * This variable specifies the level of debugging information that is
84    * output. Any debugging output above this level is output.
85    */

86   static int debug_level = 0;
87
88   /**
89    * The print stream where the debugging information is output to.
90    */

91   static PrintWriter JavaDoc out = SYSTEM_ERR;
92
93   /**
94    * The print stream where the error information is output to.
95    */

96   static PrintWriter JavaDoc err = SYSTEM_ERR;
97
98   /**
99    * Internal method that writes out the given information on the output
100    * stream provided.
101    */

102   private static final void internalWrite(PrintWriter JavaDoc out,
103                             int level, String JavaDoc class_string, String JavaDoc message) {
104     if (level < MESSAGE) {
105       out.print("> ");
106       out.print(class_string);
107       out.print(" ( lvl: ");
108       out.print(level);
109       out.print(" )\n ");
110     }
111     else {
112       out.print("% ");
113     }
114     out.println(message);
115     out.flush();
116   }
117
118   /**
119    * Sets up the OutputStream to which the debug information is to be output
120    * to.
121    */

122   public static final void setOutput(Writer JavaDoc out) {
123     Debug.out = new PrintWriter JavaDoc(out, false);
124   }
125
126   /**
127    * Sets the debug level that's to be output to the stream. Set to 255 to
128    * stop all output to the stream.
129    */

130   public static final void setDebugLevel(int level) {
131     debug_level = level;
132   }
133
134   /**
135    * Sets up the system so that the debug messenger will intercept event
136    * dispatch errors and output the event to the debug stream.
137    */

138   public static final void listenToEventDispatcher() {
139     // This is only possible in versions of Java post 1.1
140
//#IFDEF(NO_1.1)
141
// According to the EventDispatchThread documentation, this is just a
142
// temporary hack until a proper API has been defined.
143
System.setProperty("sun.awt.exception.handler",
144                        "com.mckoi.debug.DispatchNotify");
145 //#ENDIF
146
}
147
148
149   /**
150    * Queries the current debug level. Returns true if the debug listener is
151    * interested in debug information of this given level. This can be used to
152    * speed up certain complex debug displaying operations where the debug
153    * listener isn't interested in the information be presented.
154    */

155   public static final boolean isInterestedIn(int level) {
156     return (level >= debug_level);
157   }
158
159   /**
160    * This writes the given debugging string. It filters out any messages that
161    * are below the 'debug_level' variable. The 'object' variable specifies
162    * the object that made the call. 'level' must be between 0 and 255. A
163    * message of 'level' 255 will always print.
164    */

165   public final static void write(int level, Object JavaDoc ob, String JavaDoc message) {
166     write(level, ob.getClass().getName(), message);
167   }
168
169   public final static void write(int level, Class JavaDoc cla, String JavaDoc message) {
170     write(level, cla.getName(), message);
171   }
172
173   public final static void write(int level, String JavaDoc class_string, String JavaDoc message) {
174     if (isInterestedIn(level)) {
175
176       synchronized (debug_lock) {
177         if (level >= ERROR && level < MESSAGE) {
178           internalWrite(SYSTEM_ERR, level, class_string, message);
179         }
180         else if (PRINT_ALERT_TO_MESSAGES) {
181           if (out != SYSTEM_ERR && level >= ALERT) { // && level < MESSAGE) {
182
internalWrite(SYSTEM_ERR, level, class_string, message);
183           }
184         }
185
186         internalWrite(out, level, class_string, message);
187       }
188
189     }
190   }
191
192   /**
193    * @deprecated this is a legacy debug method.
194    */

195   public final static void write(Object JavaDoc ob, String JavaDoc message) {
196     write(5, ob, message);
197   }
198
199   /**
200    * Writes out the time to the debug stream.
201    */

202   private static final void writeTime() {
203     out.print("[ TIME: ");
204     out.print(new java.util.Date JavaDoc(System.currentTimeMillis()));
205     out.println(" ]");
206     out.flush();
207   }
208
209   /**
210    * This writes the given Exception. Exceptions are always output to the log
211    * stream.
212    */

213   public final static void writeException(Throwable JavaDoc e) {
214     writeException(ERROR, e);
215   }
216
217   /**
218    * This writes the given Exception but gives it a 'debug_level'. This is
219    * so we can write out a warning exception.
220    */

221   public final static void writeException(int level, Throwable JavaDoc e) {
222
223 // new Error().printStackTrace();
224

225     synchronized (debug_lock) {
226       if (level >= ERROR) {
227         System.err.print("[com.mckoi.debug.Debug - Exception thrown: '");
228         System.err.print(e.getMessage());
229         System.err.println("']");
230         e.printStackTrace(System.err);
231       }
232
233       if (isInterestedIn(level)) {
234         writeTime();
235         out.print("% ");
236         e.printStackTrace(out);
237         out.flush();
238       }
239     }
240   }
241
242
243
244 }
245
Popular Tags