KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.debug.DefaultDebugLogger 28 Mar 2002
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  * A default implementation of DebugLogger that logs messages to
33  * a PrintWriter object.
34  * <p>
35  * This implementation allows for filtering of log messages of particular
36  * depth. So for example, only message above or equal to level ALERT are
37  * shown.
38  *
39  * @author Tobias Downer
40  */

41
42 public class DefaultDebugLogger implements DebugLogger {
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   /**
52    * The debug lock object.
53    */

54   private final Object JavaDoc debug_lock = new Object JavaDoc();
55
56   /**
57    * The PrintWriter for the system output stream.
58    */

59   static final PrintWriter JavaDoc SYSTEM_OUT = new PrintWriter JavaDoc(System.out, true);
60
61   /**
62    * The PrintWriter for the system error stream.
63    */

64   static final PrintWriter JavaDoc SYSTEM_ERR = new PrintWriter JavaDoc(System.err, true);
65
66
67   /**
68    * This variable specifies the level of debugging information that is
69    * output. Any debugging output above this level is output.
70    */

71   private int debug_level = 0;
72
73   /**
74    * The print stream where the debugging information is output to.
75    */

76   private PrintWriter JavaDoc out = SYSTEM_ERR;
77
78   /**
79    * The print stream where the error information is output to.
80    */

81   private PrintWriter JavaDoc err = SYSTEM_ERR;
82
83
84   /**
85    * Internal method that writes out the given information on the output
86    * stream provided.
87    */

88   private final void internalWrite(PrintWriter JavaDoc out,
89                             int level, String JavaDoc class_string, String JavaDoc message) {
90     synchronized(out) {
91       if (level < MESSAGE) {
92         out.print("> ");
93         out.print(class_string);
94         out.print(" ( lvl: ");
95         out.print(level);
96         out.print(" )\n ");
97       }
98       else {
99         out.print("% ");
100       }
101       out.println(message);
102       out.flush();
103     }
104   }
105
106   /**
107    * Sets up the OutputStream to which the debug information is to be output
108    * to.
109    */

110   public final void setOutput(Writer JavaDoc out) {
111     this.out = new PrintWriter JavaDoc(out, false);
112   }
113
114   /**
115    * Sets the debug level that's to be output to the stream. Set to 255 to
116    * stop all output to the stream.
117    */

118   public final void setDebugLevel(int level) {
119     debug_level = level;
120   }
121
122   /**
123    * Sets up the system so that the debug messenger will intercept event
124    * dispatch errors and output the event to the debug stream.
125    */

126   public final void listenToEventDispatcher() {
127     // This is only possible in versions of Java post 1.1
128
//#IFDEF(NO_1.1)
129
// According to the EventDispatchThread documentation, this is just a
130
// temporary hack until a proper API has been defined.
131
System.setProperty("sun.awt.exception.handler",
132                        "com.mckoi.debug.DispatchNotify");
133 //#ENDIF
134
}
135
136
137   // ---------- Implemented from DebugLogger ----------
138

139   public final boolean isInterestedIn(int level) {
140     return (level >= debug_level);
141   }
142
143   public final void write(int level, Object JavaDoc ob, String JavaDoc message) {
144     write(level, ob.getClass().getName(), message);
145   }
146
147   public final void write(int level, Class JavaDoc cla, String JavaDoc message) {
148     write(level, cla.getName(), message);
149   }
150
151   public final void write(int level, String JavaDoc class_string, String JavaDoc message) {
152     if (isInterestedIn(level)) {
153
154       if (level >= ERROR && level < MESSAGE) {
155         internalWrite(SYSTEM_ERR, level, class_string, message);
156       }
157       else if (PRINT_ALERT_TO_MESSAGES) {
158         if (out != SYSTEM_ERR && level >= ALERT) { // && level < MESSAGE) {
159
internalWrite(SYSTEM_ERR, level, class_string, message);
160         }
161       }
162
163       internalWrite(out, level, class_string, message);
164     }
165
166   }
167
168   private final void writeTime() {
169     synchronized(out) {
170       out.print("[ TIME: ");
171       out.print(new java.util.Date JavaDoc(System.currentTimeMillis()));
172       out.println(" ]");
173       out.flush();
174     }
175   }
176
177   public final void writeException(Throwable JavaDoc e) {
178     writeException(ERROR, e);
179   }
180
181   public synchronized final void writeException(int level, Throwable JavaDoc e) {
182
183     if (level >= ERROR) {
184       synchronized(SYSTEM_ERR) {
185         SYSTEM_ERR.print("[com.mckoi.debug.Debug - Exception thrown: '");
186         SYSTEM_ERR.print(e.getMessage());
187         SYSTEM_ERR.println("']");
188         e.printStackTrace(SYSTEM_ERR);
189       }
190     }
191
192     if (isInterestedIn(level)) {
193       synchronized(out) {
194         writeTime();
195         out.print("% ");
196         e.printStackTrace(out);
197         out.flush();
198       }
199     }
200
201   }
202
203 }
204
Popular Tags