Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/mysql/jdbc/Debug.java


1   /*
2    Copyright (C) 2002-2004 MySQL AB
3   
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of version 2 of the GNU General Public License as
6    published by the Free Software Foundation.
7    
8   
9    There are special exceptions to the terms and conditions of the GPL 
10   as it is applied to this software. View the full text of the 
11   exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
12   software distribution.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18  
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23   */
24  package com.mysql.jdbc;
25  
26  import java.sql.DriverManager;
27  import java.util.Hashtable;
28  import java.util.StringTokenizer;
29  
30  
31  /**
32   * The Debug class allows debug messages on a per-class basis.
33   *
34   * <p>
35   * The user issues a trace() call, listing the classes they wish to debug.
36   * </p>
37   *
38   * @author Mark Matthews
39   */
40  public class Debug {
41      private static final Hashtable CLASSES = new Hashtable();
42      private static final Object MUTEX = new Object();
43      private static boolean watchAll = false;
44  
45      /**
46       * Trace a method call.
47       *
48       * <p>
49       * If the user has registered in interest in the Class of Source, then  the
50       * Source class can trace method calls through this method.
51       * </p>
52       *
53       * @param source the Object issuing the methodCall() method
54       * @param method the name of the Method
55       * @param args a list of arguments
56       */
57      public static void methodCall(Object source, String method, Object[] args) {
58          synchronized (MUTEX) {
59              if (watchAll || CLASSES.contains(source.getClass().getName())) {
60                  // Print the message
61                  StringBuffer mesg = new StringBuffer("\nTRACE: ");
62                  mesg.append(source.toString());
63                  mesg.append(".");
64                  mesg.append(method);
65                  mesg.append("( ");
66  
67                  // Print the argument list
68                  for (int i = 0; i < (args.length - 1); i++) {
69                      if (args[i] == null) {
70                          mesg.append("null");
71                      } else {
72                          if (args[i] instanceof String) {
73                              mesg.append("\"");
74                          }
75  
76                          mesg.append(args[i].toString());
77  
78                          if (args[i] instanceof String) {
79                              mesg.append("\"");
80                          }
81                      }
82  
83                      mesg.append(", ");
84                  }
85  
86                  if (args.length > 0) {
87                      if (args[args.length - 1] instanceof String) {
88                          mesg.append("\"");
89                      }
90  
91                      mesg.append(args[args.length - 1]);
92  
93                      if (args[args.length - 1] instanceof String) {
94                          mesg.append("\"");
95                      }
96                  }
97  
98                  mesg.append(" )\n");
99  
100                 if (DriverManager.getLogWriter() == null) {
101                     System.out.println(mesg.toString());
102                 } else {
103                     DriverManager.println(mesg.toString());
104                 }
105             }
106         }
107     }
108 
109     /**
110      * Log a message.
111      *
112      * <p>
113      * If the user has registered in interest in the Class of Source, then  the
114      * Source class can trace return calls through this method.
115      * </p>
116      *
117      * @param source the Object issuing the msg() method
118      * @param message the name of the method
119      */
120     public static void msg(Object source, String message) {
121         synchronized (MUTEX) {
122             if (watchAll || CLASSES.contains(source.getClass().getName())) {
123                 // Print the message
124                 StringBuffer mesg = new StringBuffer("\nTRACE: ");
125                 mesg.append(source.toString());
126                 mesg.append(": ");
127                 mesg.append(message);
128                 mesg.append("\n");
129 
130                 if (DriverManager.getLogWriter() == null) {
131                     System.out.println(mesg.toString());
132                 } else {
133                     DriverManager.println(mesg.toString());
134                 }
135             }
136         }
137     }
138 
139     /**
140      * Trace a method call.
141      *
142      * <p>
143      * If the user has registered in interest in the Class of Source, then  the
144      * Source class can trace return calls through this method.
145      * </p>
146      *
147      * @param source the Object issuing the returnValue() method
148      * @param method the name of the method
149      * @param value the return value
150      */
151     public static void returnValue(Object source, String method, Object value) {
152         synchronized (MUTEX) {
153             if (watchAll || CLASSES.contains(source.getClass().getName())) {
154                 // Print the message
155                 StringBuffer mesg = new StringBuffer("\nTRACE: ");
156                 mesg.append(source.toString());
157                 mesg.append(".");
158                 mesg.append(method);
159                 mesg.append(": Returning -> ");
160 
161                 if (value == null) {
162                     mesg.append("null");
163                 } else {
164                     mesg.append(value.toString());
165                 }
166 
167                 mesg.append("\n");
168 
169                 
170                 if (DriverManager.getLogWriter() == null) {
171                     System.out.println(mesg.toString());
172                 } else {
173                     DriverManager.println(mesg.toString());
174                 }
175             }
176         }
177     }
178 
179     /**
180      * Set the classes to trace.
181      *
182      * @param classList the list of classes to trace, separated by colons or
183      *        the keyword &quot;ALL&quot; to trace all classes that use the
184      *        Debug class.
185      */
186     public static void trace(String classList) {
187         StringTokenizer tokenizer = new StringTokenizer(classList, ":");
188 
189         synchronized (MUTEX) {
190             watchAll = false;
191 
192             if (classList.equals("ALL")) {
193                 watchAll = true;
194             } else {
195                 while (tokenizer.hasMoreTokens()) {
196                     String className = tokenizer.nextToken().trim();
197 
198                     if (!CLASSES.contains(className)) {
199                         CLASSES.put(className, className);
200                     }
201                 }
202             }
203         }
204     }
205 }