001: /*
002: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
018: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
019: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
020: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
021: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: */
030:
031: package rcm.util;
032:
033: import java.lang.reflect.*;
034: import java.io.PrintStream;
035:
036: public abstract class Debug {
037: public static final Debug VERBOSE = new Verbose();
038: public static final Debug QUIET = new Quiet();
039: public static final Debug NONE = new NoDebug();
040:
041: public static Debug getDebugLevel(Class cls)
042: throws NoSuchFieldException {
043: try {
044: Field fld = cls.getField("debug");
045: if (fld.getType() != Debug.class
046: || !Modifier.isStatic(fld.getModifiers()))
047: throw new NoSuchFieldException();
048: return (Debug) fld.get(null);
049: } catch (IllegalArgumentException e) {
050: throw new NoSuchFieldException();
051: } catch (IllegalAccessException e) {
052: throw new NoSuchFieldException();
053: } catch (SecurityException e) {
054: throw new NoSuchFieldException();
055: }
056: }
057:
058: public static void setDebugLevel(Class cls, Debug level)
059: throws NoSuchFieldException {
060: try {
061: Field fld = cls.getField("debug");
062: if (fld.getType() != Debug.class
063: || !Modifier.isStatic(fld.getModifiers()))
064: throw new NoSuchFieldException();
065: fld.set(null, level);
066: } catch (IllegalArgumentException e) {
067: throw new NoSuchFieldException();
068: } catch (IllegalAccessException e) {
069: throw new NoSuchFieldException();
070: } catch (SecurityException e) {
071: throw new NoSuchFieldException();
072: }
073: }
074:
075: public abstract boolean isEnabled();
076:
077: public abstract void print(String message);
078:
079: public abstract void println(String message);
080:
081: public abstract void print(Object obj);
082:
083: public abstract void println(Object obj);
084:
085: public abstract void report(Throwable t);
086:
087: public abstract void printThreadInfo();
088:
089: public abstract void printStackTrace();
090:
091: public abstract void assertion(boolean f);
092:
093: public static class Verbose extends Debug {
094: protected PrintStream out;
095:
096: public Verbose() {
097: this (System.err);
098: }
099:
100: public Verbose(PrintStream out) {
101: this .out = out;
102: }
103:
104: public boolean isEnabled() {
105: return true;
106: }
107:
108: public void print(String message) {
109: out.print(message);
110: out.flush();
111: }
112:
113: public void println(String message) {
114: out.println(message);
115: out.flush();
116: }
117:
118: public void print(Object obj) {
119: print(obj.toString());
120: }
121:
122: public void println(Object obj) {
123: println(obj.toString());
124: }
125:
126: public void report(Throwable t) {
127: t.printStackTrace(out);
128: out.flush();
129: }
130:
131: public void printThreadInfo() {
132: ThreadGroup g = Thread.currentThread().getThreadGroup();
133: Thread[] t = new Thread[g.activeCount()];
134: g.enumerate(t);
135: out.println("Active threads in " + g);
136: for (int i = 0; i < t.length; ++i)
137: out.println(t[i]);
138: out.flush();
139: }
140:
141: public void printStackTrace() {
142: try {
143: throw new Exception();
144: } catch (Exception e) {
145: e.printStackTrace(out);
146: out.flush();
147: }
148: }
149:
150: public void assertion(boolean f) {
151: if (!f)
152: throw new RuntimeException("assertion failure");
153: }
154: }
155:
156: public static class Quiet extends Verbose {
157: public Quiet() {
158: }
159:
160: public Quiet(PrintStream out) {
161: super (out);
162: }
163:
164: public boolean isEnabled() {
165: return false;
166: }
167:
168: public void print(String message) {
169: }
170:
171: public void println(String message) {
172: }
173:
174: public void print(Object message) {
175: }
176:
177: public void println(Object message) {
178: }
179:
180: public void report(Throwable t) {
181: t.printStackTrace(out);
182: out.flush();
183: }
184:
185: public void printThreadInfo() {
186: }
187:
188: public void printStackTrace() {
189: }
190:
191: public void assertion(boolean f) {
192: if (!f) {
193: try {
194: throw new RuntimeException("assertion failure");
195: } catch (RuntimeException e) {
196: e.printStackTrace(out);
197: out.flush();
198: }
199: }
200: }
201: }
202:
203: public static class NoDebug extends Debug {
204: public boolean isEnabled() {
205: return false;
206: }
207:
208: public void print(String message) {
209: }
210:
211: public void println(String message) {
212: }
213:
214: public void print(Object message) {
215: }
216:
217: public void println(Object message) {
218: }
219:
220: public void report(Throwable t) {
221: }
222:
223: public void printThreadInfo() {
224: }
225:
226: public void printStackTrace() {
227: }
228:
229: public void assertion(boolean f) {
230: }
231: }
232:
233: }
|