KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > Debug


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.sql.DriverManager JavaDoc;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26
27 /**
28  * The Debug class allows debug messages on a per-class basis.
29  *
30  * <p>
31  * The user issues a trace() call, listing the classes they wish to debug.
32  * </p>
33  *
34  * @author Mark Matthews
35  */

36 public class Debug {
37     private static final Hashtable JavaDoc CLASSES = new Hashtable JavaDoc();
38     private static final Object JavaDoc MUTEX = new Object JavaDoc();
39     private static boolean watchAll = false;
40
41     /**
42      * Trace a method call.
43      *
44      * <p>
45      * If the user has registered in interest in the Class of Source, then the
46      * Source class can trace method calls through this method.
47      * </p>
48      *
49      * @param source the Object issuing the methodCall() method
50      * @param method the name of the Method
51      * @param args a list of arguments
52      */

53     public static void methodCall(Object JavaDoc source, String JavaDoc method, Object JavaDoc[] args) {
54         synchronized (MUTEX) {
55             if (watchAll || CLASSES.contains(source.getClass().getName())) {
56                 // Print the message
57
StringBuffer JavaDoc mesg = new StringBuffer JavaDoc("\nTRACE: ");
58                 mesg.append(source.toString());
59                 mesg.append(".");
60                 mesg.append(method);
61                 mesg.append("( ");
62
63                 // Print the argument list
64
for (int i = 0; i < (args.length - 1); i++) {
65                     if (args[i] == null) {
66                         mesg.append("null");
67                     } else {
68                         if (args[i] instanceof String JavaDoc) {
69                             mesg.append("\"");
70                         }
71
72                         mesg.append(args[i].toString());
73
74                         if (args[i] instanceof String JavaDoc) {
75                             mesg.append("\"");
76                         }
77                     }
78
79                     mesg.append(", ");
80                 }
81
82                 if (args.length > 0) {
83                     if (args[args.length - 1] instanceof String JavaDoc) {
84                         mesg.append("\"");
85                     }
86
87                     mesg.append(args[args.length - 1]);
88
89                     if (args[args.length - 1] instanceof String JavaDoc) {
90                         mesg.append("\"");
91                     }
92                 }
93
94                 mesg.append(" )\n");
95
96                 if (DriverManager.getLogStream() == null) {
97                     System.out.println(mesg.toString());
98                 } else {
99                     DriverManager.println(mesg.toString());
100                 }
101             }
102         }
103     }
104
105     /**
106      * Log a message.
107      *
108      * <p>
109      * If the user has registered in interest in the Class of Source, then the
110      * Source class can trace return calls through this method.
111      * </p>
112      *
113      * @param source the Object issuing the msg() method
114      * @param message the name of the method
115      */

116     public static void msg(Object JavaDoc source, String JavaDoc message) {
117         synchronized (MUTEX) {
118             if (watchAll || CLASSES.contains(source.getClass().getName())) {
119                 // Print the message
120
StringBuffer JavaDoc mesg = new StringBuffer JavaDoc("\nTRACE: ");
121                 mesg.append(source.toString());
122                 mesg.append(": ");
123                 mesg.append(message);
124                 mesg.append("\n");
125
126                 if (DriverManager.getLogStream() == null) {
127                     System.out.println(mesg.toString());
128                 } else {
129                     DriverManager.println(mesg.toString());
130                 }
131             }
132         }
133     }
134
135     /**
136      * Trace a method call.
137      *
138      * <p>
139      * If the user has registered in interest in the Class of Source, then the
140      * Source class can trace return calls through this method.
141      * </p>
142      *
143      * @param source the Object issuing the returnValue() method
144      * @param method the name of the method
145      * @param value the return value
146      */

147     public static void returnValue(Object JavaDoc source, String JavaDoc method, Object JavaDoc value) {
148         synchronized (MUTEX) {
149             if (watchAll || CLASSES.contains(source.getClass().getName())) {
150                 // Print the message
151
StringBuffer JavaDoc mesg = new StringBuffer JavaDoc("\nTRACE: ");
152                 mesg.append(source.toString());
153                 mesg.append(".");
154                 mesg.append(method);
155                 mesg.append(": Returning -> ");
156
157                 if (value == null) {
158                     mesg.append("null");
159                 } else {
160                     mesg.append(value.toString());
161                 }
162
163                 mesg.append("\n");
164
165                 if (DriverManager.getLogStream() == null) {
166                     System.out.println(mesg.toString());
167                 } else {
168                     DriverManager.println(mesg.toString());
169                 }
170             }
171         }
172     }
173
174     /**
175      * Set the classes to trace.
176      *
177      * @param classList the list of classes to trace, separated by colons or
178      * the keyword &quot;ALL&quot; to trace all classes that use the
179      * Debug class.
180      */

181     public static void trace(String JavaDoc classList) {
182         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classList, ":");
183
184         synchronized (MUTEX) {
185             watchAll = false;
186
187             if (classList.equals("ALL")) {
188                 watchAll = true;
189             } else {
190                 while (tokenizer.hasMoreTokens()) {
191                     String JavaDoc className = tokenizer.nextToken().trim();
192
193                     if (!CLASSES.contains(className)) {
194                         CLASSES.put(className, className);
195                     }
196                 }
197             }
198         }
199     }
200 }
201
Popular Tags