001: /*
002: ** Java cvs client library package.
003: ** Copyright (c) 1997-2002 by Timothy Gerard Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** Library General Public License (LGPL) as published by the Free Software
009: ** Foundation.
010: **
011: ** Version 2 of the license should be included with this distribution in
012: ** the file LICENSE.txt, as well as License.html. If the license is not
013: ** included with this distribution, you may find a copy at the FSF web
014: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the Free
015: ** Software Foundation at 59 Temple Place - Suite 330, Boston, MA 02111 USA.
016: **
017: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
018: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
019: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
020: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
021: ** REDISTRIBUTION OF THIS SOFTWARE.
022: **
023: */
024:
025: package com.ice.cvsc;
026:
027: import java.io.*;
028: import java.lang.*;
029: import java.text.*;
030: import java.util.*;
031:
032: /**
033: * The CVSTracer class implements the a tracing mechanism for
034: * the CVS package. This allows for more control and details
035: * than a simple 'CVSLog.logMsg()' provides. This is a
036: * <strong>strictly</strong> class based interface. There are
037: * no instance methods.
038: *
039: * @version $Revision: 2.4 $
040: * @author Timothy Gerard Endres, <a href="mailto:time@ice.com">time@ice.com</a>.
041: * @see CVSClient
042: *
043: */
044:
045: public class CVSTracer extends Object {
046: static public final String RCS_ID = "$Id: CVSTracer.java,v 2.4 2003/07/27 01:08:32 time Exp $";
047: static public final String RCS_REV = "$Revision: 2.4 $";
048:
049: static private PrintWriter out = null;
050:
051: static private boolean on = false;
052: static private boolean ifOverOn = true;
053:
054: static private int traceState = 0;
055:
056: static private boolean outIsSystem = false;
057:
058: static private boolean echoAccum = false;
059: static private StringBuffer outBuffer = null;
060:
061: static public void turnOn() {
062: CVSTracer.on = true;
063: }
064:
065: static public void turnOff() {
066: CVSTracer.on = false;
067: }
068:
069: static public void setEchoAccumulation(boolean state) {
070: CVSTracer.echoAccum = state;
071: }
072:
073: static public void accumulateInBuffer(StringBuffer buffer) {
074: CVSTracer.outBuffer = buffer;
075: }
076:
077: static public void turnOffAccumulation() {
078: CVSTracer.outBuffer = null;
079: }
080:
081: static public StringBuffer getAccumulationBuffer() {
082: return CVSTracer.outBuffer;
083: }
084:
085: static public void println(String line) {
086: if (line == null)
087: return;
088:
089: if (CVSTracer.outBuffer != null) {
090: CVSTracer.outBuffer.append(line);
091: CVSTracer.outBuffer.append("\n");
092:
093: if (!CVSTracer.echoAccum)
094: return;
095: }
096:
097: if (out != null) {
098: CVSTracer.out.println(line);
099: } else {
100: CVSLog.logMsg(line);
101: }
102: }
103:
104: static public void trace(String line) {
105: if (line == null)
106: return;
107:
108: if (CVSTracer.on) {
109: CVSTracer.println(line);
110: }
111: }
112:
113: static public void traceIf(boolean flag, String line) {
114: if ((!flag) || (line == null))
115: return;
116:
117: if (CVSTracer.ifOverOn) {
118: CVSTracer.println(line);
119: }
120: }
121:
122: static public void traceException(String line, Exception ex) {
123: if (line == null)
124: return;
125:
126: if (CVSTracer.on) {
127: CVSTracer.println(line);
128: }
129:
130: if (CVSTracer.out == null)
131: ex.printStackTrace(System.err);
132: else
133: ex.printStackTrace(CVSTracer.out);
134: }
135:
136: static public void traceWithStack(String line) {
137: if (line == null)
138: return;
139:
140: Throwable thrower = new Throwable(line);
141:
142: if (CVSTracer.on) {
143: CVSTracer.println(line);
144: }
145:
146: if (CVSTracer.out == null)
147: thrower.printStackTrace(System.err);
148: else
149: thrower.printStackTrace(CVSTracer.out);
150: }
151:
152: static private void checkClose() {
153: if (CVSTracer.out != null) {
154: if (!CVSTracer.outIsSystem) {
155: CVSTracer.out.close();
156: CVSTracer.out = null;
157: CVSTracer.outIsSystem = false;
158: }
159: }
160: }
161:
162: /**
163: * Sets the tracer's output writer to the BufferedWriter
164: * passed in. The new writer <em>newOut</em> <strong>must never</strong>
165: * be System.err or System.err, since the writer will be
166: * closed at some point.
167: *
168: * @param newOut The new buffered writer to send trace output to.
169: */
170:
171: static public void setWriter(PrintWriter newOut) {
172: CVSTracer.checkClose();
173:
174: CVSTracer.out = newOut;
175: CVSTracer.outIsSystem = false;
176:
177: CVSTracer.outBuffer = null;
178: }
179:
180: static public void setWriterToStdout() {
181: PrintWriter newOut = new PrintWriter(new OutputStreamWriter(
182: System.out));
183:
184: if (newOut != null) {
185: CVSTracer.checkClose();
186: CVSTracer.out = newOut;
187: CVSTracer.outIsSystem = true;
188:
189: CVSTracer.outBuffer = null;
190: }
191: }
192:
193: static public void setWriterToStderr() {
194: PrintWriter newOut = new PrintWriter(new OutputStreamWriter(
195: System.err));
196:
197: if (newOut != null) {
198: CVSTracer.checkClose();
199: CVSTracer.out = newOut;
200: CVSTracer.outIsSystem = true;
201:
202: CVSTracer.outBuffer = null;
203: }
204: }
205:
206: }
|