KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > Debug


1 package com.quadcap.util;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.PrintStream JavaDoc;
45
46 import java.util.Date JavaDoc;
47 import java.util.Hashtable JavaDoc;
48
49 import java.sql.SQLException JavaDoc;
50
51 import java.text.SimpleDateFormat JavaDoc;
52
53 import com.quadcap.sql.file.DatafileException;
54
55 /**
56  * Common functions for debug and log output.
57  *
58  * @author Stan Bailes
59  */

60 public class Debug {
61     static public PrintStream JavaDoc debugStream = System.out;
62     static public int printLevel = 0;
63
64     private static String JavaDoc debugFileName = null;
65     private static FileOutputStream JavaDoc debugFileStream = null;
66
67     static Hashtable JavaDoc debugMap = new Hashtable JavaDoc();
68     static NullSecurityManager sm = null;
69
70     static public final int debugOff = 0;
71     static public final int debugSome = 1;
72     static public final int debugAll = 3;
73     static public final int debugPackage = 4;
74
75     static public int debugMode = debugAll;
76
77     static public final int debugNoTime = 0;
78     static public final int debugElap = 1;
79     static public final int debugInterval = 2;
80     static public final int debugShowTime = 3;
81
82     static public int debugTime = debugShowTime;
83
84     static public long start = new Date JavaDoc().getTime();
85     static public long last = start;
86
87     static SimpleDateFormat JavaDoc sdf =
88     new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.SSS");
89
90
91     /*{com.quadcap.util.Config-vars.xml-0}
92      *
93      * <config>
94      */

95
96     /*{com.quadcap.util.Config-vars.xml-999999}
97      *
98      * </config>
99      */

100
101     /*{com.quadcap.util.Config-vars.xml-10}
102      *
103      * <config-var>
104      * <config-name>debug.file</config-name>
105      * <config-dflt>stderr</config-dflt>
106      * <config-desc>Specify the name of the debug log file. The default
107      * value, <code>stderr</code>, causes debug information to
108      * be written to <code>System.err</code>.</config-desc>
109      * </config-var>
110      *
111      * <config-var>
112      * <config-name>debug.level</config-name>
113      * <config-dflt>0</config-dflt>
114      * <config-desc>Specify the level of debugging output. Level zero,
115      * the lowest level, essentially turns debug output off (though
116      * errors/exceptions will still be reported.) Increasing levels
117      * will result in more debug output. The maximum debug level is
118      * five (5).</config-desc>
119      * </config-var>
120      */

121     static {
122     //System.setSecurityManager(sm = new NullSecurityManager());
123
try {
124         ConfigString file = ConfigString.find("debug.file", "stderr");
125             if (file != null) {
126         setLogFile(file.toString());
127         }
128             ConfigNumber level = ConfigNumber.find("debug.level", "0");
129             if (level != null) {
130                 printLevel = level.intValue();
131             }
132     } catch (IOException JavaDoc e) {
133         Debug.print(e);
134     }
135     }
136
137     static public Class JavaDoc[] getClassContext() {
138     return sm.pubGetClassContext();
139     }
140
141     static public void setDebugLevel(String JavaDoc name, String JavaDoc level) {
142     debugMap.put(name, level);
143     }
144     
145     static public void print(Throwable JavaDoc e) {
146         if (e == null) return;
147     if (!(e instanceof SQLException JavaDoc)) {
148         Debug.println(0, 2, e.getClass().getName());
149     }
150         debugStream.print(hdr());
151     e.printStackTrace(debugStream);
152     if (e instanceof SQLException JavaDoc) {
153         printSQLException((SQLException JavaDoc)e);
154     } else if (e.getCause() != null) {
155             Debug.println("Cause of this exception:");
156             print(e.getCause());
157         }
158     }
159
160     static void printSQLException(SQLException JavaDoc e) {
161     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(e.toString());
162     int idx;
163     while ((idx = sb.toString().indexOf("Exception:")) >= 0) {
164         sb.setCharAt(idx + 9, ',');
165     }
166     Debug.println(0, 3, sb.toString() + ", SQLState = " + e.getSQLState() +
167               ", errorCode = " + e.getErrorCode());
168     SQLException JavaDoc next = e.getNextException();
169     if (next != null) {
170             Debug.println("Next SQL Exception:");
171         printSQLException(next);
172     } else if (e.getCause() != null) {
173             Debug.println("Cause of this exception:");
174             print(e);
175         }
176     }
177
178     static public void println(String JavaDoc s) {
179     println(0, 1, s);
180     }
181
182     static public String JavaDoc getInterval() {
183     String JavaDoc ret = null;
184     Date JavaDoc d = new Date JavaDoc();
185     long now = d.getTime();
186
187     switch (debugTime) {
188     case debugInterval:
189         ret = "" + (now - last);
190         last = now;
191         break;
192     case debugElap:
193         ret = "" + (now - start);
194         break;
195     case debugShowTime:
196         ret = "" + sdf.format(d);
197         break;
198     }
199     return ret;
200     }
201
202     /**
203      * Specify the file to be used for debug/log output. If there is
204      * already a debug file open with a different name, it is closed first,
205      * and if the name specified is <code>"stdout"</code>, then debug
206      * output is directed to <code>System.out</code>.
207      *
208      * @param name the name of the debug log file.
209      * @exception IOException may be thrown.
210      */

211     static public void setLogFile(String JavaDoc name) throws IOException JavaDoc {
212     if (name.equals("stdout")) {
213         if (debugFileStream != null) {
214         debugFileStream.close();
215         debugFileStream = null;
216         }
217         debugStream = System.out;
218         } else if (name.equals("stderr")) {
219         if (debugFileStream != null) {
220         debugFileStream.close();
221         debugFileStream = null;
222         }
223         debugStream = System.err;
224     } else {
225             boolean append = name.startsWith("append:");
226             if (append) name = name.substring(7).trim();
227         name = new File JavaDoc(name).getAbsolutePath();
228         if (debugFileName == null || !name.equals(debugFileName)) {
229         debugFileName = name;
230         if (debugFileStream != null) {
231             debugFileStream.close();
232             debugFileStream = null;
233         }
234         debugFileStream = new FileOutputStream JavaDoc(name, append);
235         debugStream = new PrintStream JavaDoc(debugFileStream);
236                 println("---- [Debug started]");
237         }
238     }
239     }
240
241     /**
242      * Specify the 'level' for debugging output. Only debug statements
243      * satisfying <code>level &lt;= printLevel</code> result in output.
244      *
245      * @param printLevel the new debug output level.
246      */

247     static public void setLogLevel(int printLevel) {
248     if (printLevel != Debug.printLevel) {
249         Debug.println(0, "Changing log level from " + Debug.printLevel +
250               " to " + printLevel);
251         Debug.printLevel = printLevel;
252     }
253     }
254
255     /**
256      * Specify the debug mode using the convenient string representation
257      *
258      * @param mode the debug mode.
259      */

260     static public void setDebugMode(String JavaDoc mode) {
261     if (mode.equals("off")) {
262         Debug.debugMode = Debug.debugOff;
263     } else if (mode.equals("some")) {
264         Debug.debugMode = Debug.debugSome;
265     } else if (mode.equals("all")) {
266         Debug.debugMode = Debug.debugAll;
267     } else if (mode.equals("package")) {
268         Debug.debugMode = Debug.debugPackage;
269     }
270     }
271
272     /**
273      * Debug output at level <code>level</code>.
274      */

275     static synchronized public void println(int level, String JavaDoc s) {
276     println(level, 1, s);
277     }
278
279     static String JavaDoc hdr() {
280         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
281     if (debugTime != debugNoTime) {
282             sb.append(getInterval());
283             sb.append(": ");
284         }
285         sb.append("[");
286         sb.append(Thread.currentThread().getName());
287         sb.append("] ");
288         return sb.toString();
289     }
290     
291     /**
292      * Debug output at level <code>level</code>.
293      */

294     static synchronized public void println(int level, int depth, String JavaDoc s) {
295     if (level > printLevel) return;
296     debugStream.print(hdr());
297     if (sm != null) {
298         String JavaDoc className = debugSite(level, depth+1);
299         debugStream.print(className + ": ");
300     }
301     debugStream.println(s);
302     }
303
304     static String JavaDoc debugSite(int level, int depth) {
305     if (debugMode == debugOff || level > printLevel) return null;
306     boolean print = true;
307     Class JavaDoc[] classes = sm.pubGetClassContext();
308
309     String JavaDoc className = "";
310     String JavaDoc packageName = "";
311     if (classes.length > 2 + depth) {
312         Class JavaDoc caller = classes[2 + depth];
313         className = caller.getName();
314         int pos = className.lastIndexOf('.');
315         if (pos >= 0) {
316         packageName = className.substring(0, pos);
317         className = className.substring(pos+1);
318         }
319     }
320
321     String JavaDoc debugVal;
322     
323     switch (debugMode) {
324     case debugSome:
325         debugVal = (String JavaDoc)debugMap.get(className);
326         if (debugVal != null && level <= Integer.parseInt(debugVal)) {
327         print = debugMode == debugSome;
328         }
329         break;
330     case debugPackage:
331         debugVal = (String JavaDoc)debugMap.get(packageName);
332         if (debugVal != null && level <= Integer.parseInt(debugVal)) {
333         print = debugMode == debugPackage;
334         }
335         break;
336     }
337     
338     if (print) return className;
339     return null;
340     }
341
342 }
343
Popular Tags