001: /*
002: ** Copyright (c) 1997 by Timothy Gerard Endres
003: **
004: ** This program is free software.
005: **
006: ** You may redistribute it and/or modify it under the terms of the GNU
007: ** General Public License as published by the Free Software Foundation.
008: ** Version 2 of the license should be included with this distribution in
009: ** the file LICENSE, as well as License.html. If the license is not
010: ** included with this distribution, you may find a copy at the FSF web
011: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
012: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
013: **
014: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
015: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
016: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
017: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
018: ** REDISTRIBUTION OF THIS SOFTWARE.
019: **
020: */
021:
022: package com.ice.util;
023:
024: import java.io.*;
025: import java.lang.*;
026: import java.text.*;
027: import java.util.*;
028:
029: /**
030: * The ICETracer class implements the a stack tracing mechanism
031: * for debugging use. This is a <strong>strictly</strong> class
032: * based interface. There are no instance methods.
033: *
034: * @version $Revision: 1.4 $
035: * @author Timothy Gerard Endres,
036: * <a href="mailto:time@ice.com">time@ice.com</a>.
037: *
038: * @see com.ice.util.UserProperties
039: *
040: */
041:
042: public class ICETracer extends Object {
043: static public final String RCS_ID = "$Id: ICETracer.java,v 1.4 1998/04/29 16:30:13 time Exp $";
044: static public final String RCS_REV = "$Revision: 1.4 $";
045:
046: static private PrintWriter out = null;
047:
048: static private boolean state = false;
049: static private boolean ifOverOn = true;
050:
051: static private int traceState = 0;
052:
053: static private boolean outIsSystem = false;
054:
055: static private boolean echoAccum = false;
056: static private StringBuffer outBuffer = null;
057:
058: static public void setTraceState(boolean state) {
059: ICETracer.state = state;
060: }
061:
062: static public void setEchoAccumulation(boolean state) {
063: ICETracer.echoAccum = state;
064: }
065:
066: static public void accumulateInBuffer(StringBuffer buffer) {
067: ICETracer.outBuffer = buffer;
068: }
069:
070: static public void turnOffAccumulation() {
071: ICETracer.outBuffer = null;
072: }
073:
074: static public StringBuffer getAccumulationBuffer() {
075: return ICETracer.outBuffer;
076: }
077:
078: static public void println(String line) {
079: if (line == null)
080: return;
081:
082: if (ICETracer.outBuffer != null) {
083: ICETracer.outBuffer.append(line);
084: ICETracer.outBuffer.append("\n");
085:
086: if (!ICETracer.echoAccum)
087: return;
088: }
089:
090: if (out != null) {
091: ICETracer.out.println(line);
092: } else {
093: System.err.println(line);
094: }
095: }
096:
097: static public void trace(String line) {
098: if (line == null)
099: return;
100:
101: if (ICETracer.state) {
102: ICETracer.println(line);
103: }
104: }
105:
106: static public void traceIf(boolean flag, String line) {
107: if ((!flag) || (line == null))
108: return;
109:
110: if (ICETracer.ifOverOn) {
111: ICETracer.println(line);
112: }
113: }
114:
115: static public void traceWithStack(String line) {
116: if (line == null)
117: return;
118:
119: Throwable thrower = new Throwable(line);
120:
121: if (ICETracer.state) {
122: ICETracer.println(line);
123: }
124:
125: if (ICETracer.out == null)
126: thrower.printStackTrace(System.err);
127: else
128: thrower.printStackTrace(ICETracer.out);
129: }
130:
131: static public String getStackLines(Throwable thrower) {
132: StringWriter sWrtr = new StringWriter();
133: PrintWriter pWrtr = new PrintWriter(sWrtr);
134: thrower.printStackTrace(pWrtr);
135: return sWrtr.toString();
136: }
137:
138: static public String getStackLines(Throwable thrower, int maxLines) {
139: if (maxLines == 0)
140: return ICETracer.getStackLines(thrower);
141:
142: StringWriter sWrtr = new StringWriter();
143: PrintWriter pWrtr = new PrintWriter(sWrtr);
144:
145: thrower.printStackTrace(pWrtr);
146:
147: String trcStr = sWrtr.getBuffer().toString();
148:
149: String sep = System.getProperty("line.separator", "\n");
150:
151: int offset = 0;
152: int index = trcStr.length();
153: for (int ln = 0; ln < maxLines; ++ln) {
154: int idx = trcStr.indexOf(sep, offset);
155: if (idx == -1)
156: break;
157:
158: index = idx;
159: offset = idx + 1;
160: }
161:
162: return trcStr.substring(0, index);
163: }
164:
165: static public void traceWithStack(int maxPrintLines, String line) {
166: if (line == null || maxPrintLines < 1)
167: return;
168:
169: Throwable thrower = new Throwable(line);
170:
171: if (ICETracer.state) {
172: ICETracer.println(line);
173: }
174:
175: String outStr = ICETracer.getStackLines(thrower, maxPrintLines);
176:
177: if (ICETracer.out == null)
178: System.err.println(outStr);
179: else
180: ICETracer.out.println(outStr);
181: }
182:
183: static public void traceWithStack(Throwable thrower, String line) {
184: if (thrower == null && line == null)
185: return;
186:
187: if (line != null)
188: ICETracer.println(line);
189:
190: String outStr = ICETracer.getStackLines(thrower, 0);
191:
192: if (ICETracer.out == null)
193: System.err.println(outStr);
194: else
195: ICETracer.out.println(outStr);
196: }
197:
198: static public void traceWithStack(Throwable thrower, int lines,
199: String line) {
200: if (thrower == null && line == null)
201: return;
202:
203: if (line != null)
204: ICETracer.println(line);
205:
206: String outStr = ICETracer.getStackLines(thrower, lines);
207:
208: if (ICETracer.out == null)
209: System.err.println(outStr);
210: else
211: ICETracer.out.println(outStr);
212: }
213:
214: static private void checkClose() {
215: if (ICETracer.out != null) {
216: if (!ICETracer.outIsSystem) {
217: ICETracer.out.close();
218: ICETracer.out = null;
219: ICETracer.outIsSystem = false;
220: }
221: }
222: }
223:
224: /**
225: * Sets the tracer's output writer to the BufferedWriter
226: * passed in. The new writer <em>newOut</em> <strong>must never</strong>
227: * be System.err or System.err, since the writer will be
228: * closed at some point.
229: *
230: * @param newOut The new buffered writer to send trace output to.
231: */
232:
233: static public void setWriter(PrintWriter newOut) {
234: ICETracer.checkClose();
235:
236: ICETracer.out = newOut;
237: ICETracer.outIsSystem = false;
238:
239: ICETracer.outBuffer = null;
240: }
241:
242: static public void setWriterToStdout() {
243: PrintWriter newOut = new PrintWriter(new OutputStreamWriter(
244: System.out));
245:
246: if (newOut != null) {
247: ICETracer.checkClose();
248: ICETracer.out = newOut;
249: ICETracer.outIsSystem = true;
250:
251: ICETracer.outBuffer = null;
252: }
253: }
254:
255: static public void setWriterToStderr() {
256: PrintWriter newOut = new PrintWriter(new OutputStreamWriter(
257: System.err));
258:
259: if (newOut != null) {
260: ICETracer.checkClose();
261: ICETracer.out = newOut;
262: ICETracer.outIsSystem = true;
263:
264: ICETracer.outBuffer = null;
265: }
266: }
267:
268: }
|