KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > util > Debug


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.util;
32
33 import java.lang.reflect.*;
34 import java.io.PrintStream JavaDoc;
35
36 public abstract class Debug {
37     public static final Debug VERBOSE = new Verbose ();
38     public static final Debug QUIET = new Quiet ();
39     public static final Debug NONE = new NoDebug ();
40
41     public static Debug getDebugLevel (Class JavaDoc cls)
42         throws NoSuchFieldException JavaDoc {
43         try {
44             Field fld = cls.getField ("debug");
45             if (fld.getType () != Debug.class
46                 || !Modifier.isStatic (fld.getModifiers ()))
47                 throw new NoSuchFieldException JavaDoc ();
48             return (Debug) fld.get (null);
49         } catch (IllegalArgumentException JavaDoc e) {
50             throw new NoSuchFieldException JavaDoc ();
51         } catch (IllegalAccessException JavaDoc e) {
52             throw new NoSuchFieldException JavaDoc ();
53         } catch (SecurityException JavaDoc e) {
54             throw new NoSuchFieldException JavaDoc ();
55         }
56     }
57
58     public static void setDebugLevel (Class JavaDoc cls, Debug level)
59         throws NoSuchFieldException JavaDoc {
60         try {
61             Field fld = cls.getField ("debug");
62             if (fld.getType () != Debug.class
63                 || !Modifier.isStatic (fld.getModifiers ()))
64                 throw new NoSuchFieldException JavaDoc ();
65             fld.set (null, level);
66         } catch (IllegalArgumentException JavaDoc e) {
67             throw new NoSuchFieldException JavaDoc ();
68         } catch (IllegalAccessException JavaDoc e) {
69             throw new NoSuchFieldException JavaDoc ();
70         } catch (SecurityException JavaDoc e) {
71             throw new NoSuchFieldException JavaDoc ();
72         }
73     }
74
75     public abstract boolean isEnabled ();
76     public abstract void print (String JavaDoc message);
77     public abstract void println (String JavaDoc message);
78     public abstract void print (Object JavaDoc obj);
79     public abstract void println (Object JavaDoc obj);
80     public abstract void report (Throwable JavaDoc t);
81     public abstract void printThreadInfo ();
82     public abstract void printStackTrace ();
83     public abstract void assertion (boolean f);
84
85     public static class Verbose extends Debug {
86         protected PrintStream JavaDoc out;
87
88         public Verbose () {
89             this (System.err);
90         }
91
92         public Verbose (PrintStream JavaDoc out) {
93             this.out = out;
94         }
95
96         public boolean isEnabled () {
97             return true;
98         }
99
100         public void print (String JavaDoc message) {
101             out.print (message);
102             out.flush ();
103         }
104
105         public void println (String JavaDoc message) {
106             out.println (message);
107             out.flush ();
108         }
109
110         public void print (Object JavaDoc obj) {
111             print (obj.toString ());
112         }
113
114         public void println (Object JavaDoc obj) {
115             println (obj.toString ());
116         }
117
118         public void report (Throwable JavaDoc t) {
119             t.printStackTrace (out);
120             out.flush ();
121         }
122
123         public void printThreadInfo () {
124             ThreadGroup JavaDoc g = Thread.currentThread().getThreadGroup ();
125             Thread JavaDoc[] t = new Thread JavaDoc[g.activeCount ()];
126             g.enumerate (t);
127             out.println ("Active threads in " + g);
128             for (int i=0; i<t.length; ++i)
129                 out.println (t[i]);
130             out.flush ();
131         }
132
133         public void printStackTrace () {
134             try {
135                 throw new Exception JavaDoc ();
136             } catch (Exception JavaDoc e) {
137                 e.printStackTrace (out);
138                 out.flush ();
139             }
140         }
141
142         public void assertion (boolean f) {
143             if (!f)
144                 throw new RuntimeException JavaDoc ("assertion failure");
145         }
146     }
147
148     public static class Quiet extends Verbose {
149         public Quiet () {
150         }
151
152         public Quiet (PrintStream JavaDoc out) {
153             super (out);
154         }
155
156         public boolean isEnabled () {
157             return false;
158         }
159
160         public void print (String JavaDoc message) {
161         }
162         public void println (String JavaDoc message) {
163         }
164         public void print (Object JavaDoc message) {
165         }
166         public void println (Object JavaDoc message) {
167         }
168         public void report (Throwable JavaDoc t) {
169             t.printStackTrace (out);
170             out.flush ();
171         }
172         public void printThreadInfo () {
173         }
174         public void printStackTrace () {
175         }
176         public void assertion (boolean f) {
177             if (!f) {
178                 try {
179                     throw new RuntimeException JavaDoc ("assertion failure");
180                 } catch (RuntimeException JavaDoc e) {
181                     e.printStackTrace (out);
182                     out.flush ();
183                 }
184             }
185         }
186     }
187
188     public static class NoDebug extends Debug {
189         public boolean isEnabled () {
190             return false;
191         }
192
193         public void print (String JavaDoc message) {
194         }
195         public void println (String JavaDoc message) {
196         }
197         public void print (Object JavaDoc message) {
198         }
199         public void println (Object JavaDoc message) {
200         }
201         public void report (Throwable JavaDoc t) {
202         }
203         public void printThreadInfo () {
204         }
205         public void printStackTrace () {
206         }
207         public void assertion (boolean f) {
208         }
209     }
210
211 }
212
Popular Tags