001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.io.BufferedWriter;
019: import java.io.OutputStream;
020: import java.io.OutputStreamWriter;
021: import java.io.PrintWriter;
022: import java.io.Writer;
023:
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.commons.logging.Log;
026:
027: /**
028: * A PrintWriter that also sends its output to a log stream
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public class DebuggingPrintWriter extends PrintWriter {
032: /**
033: * Create a new PrintWriter, without automatic line flushing.
034: * @param prefix A tag to prefix lines with for debugging purposes
035: * @param out A character-output stream
036: */
037: public DebuggingPrintWriter(String prefix, Writer out) {
038: super (out, false);
039: this .prefix = prefix;
040: }
041:
042: /**
043: * Create a new PrintWriter.
044: * @param prefix A tag to prefix lines with for debugging purposes
045: * @param out A character-output stream
046: * @param autoFlush A boolean; if true, the println() methods will flush the output buffer
047: */
048: public DebuggingPrintWriter(String prefix, Writer out,
049: boolean autoFlush) {
050: super (out, autoFlush);
051: this .prefix = prefix;
052: }
053:
054: /**
055: * Create a new PrintWriter, without automatic line flushing, from an
056: * existing OutputStream. This convenience constructor creates the
057: * necessary intermediate OutputStreamWriter, which will convert characters
058: * into bytes using the default character encoding.
059: * @param prefix A tag to prefix lines with for debugging purposes
060: * @param out An output stream
061: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
062: */
063: public DebuggingPrintWriter(String prefix, OutputStream out) {
064: super (out, false);
065: this .prefix = prefix;
066: }
067:
068: /**
069: * Create a new PrintWriter from an existing OutputStream. This convenience
070: * constructor creates the necessary intermediate OutputStreamWriter, which
071: * will convert characters into bytes using the default character encoding.
072: * @param prefix A tag to prefix lines with for debugging purposes
073: * @param out An output stream
074: * @param autoFlush A boolean; if true, the println() methods will flush the output buffer
075: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
076: */
077: public DebuggingPrintWriter(String prefix, OutputStream out,
078: boolean autoFlush) {
079: super (new BufferedWriter(new OutputStreamWriter(out)),
080: autoFlush);
081: this .prefix = prefix;
082: }
083:
084: /* (non-Javadoc)
085: * @see java.io.PrintWriter#print(boolean)
086: */
087: @Override
088: public void print(boolean x) {
089: super .print(x);
090: buffer.append(x);
091: }
092:
093: /* (non-Javadoc)
094: * @see java.io.PrintWriter#print(char)
095: */
096: @Override
097: public void print(char x) {
098: super .print(x);
099: buffer.append(x);
100: }
101:
102: /* (non-Javadoc)
103: * @see java.io.PrintWriter#print(int)
104: */
105: @Override
106: public void print(int x) {
107: super .print(x);
108: buffer.append(x);
109: }
110:
111: /* (non-Javadoc)
112: * @see java.io.PrintWriter#print(long)
113: */
114: @Override
115: public void print(long x) {
116: super .print(x);
117: buffer.append(x);
118: }
119:
120: /* (non-Javadoc)
121: * @see java.io.PrintWriter#print(float)
122: */
123: @Override
124: public void print(float x) {
125: super .print(x);
126: buffer.append(x);
127: }
128:
129: /* (non-Javadoc)
130: * @see java.io.PrintWriter#print(double)
131: */
132: @Override
133: public void print(double x) {
134: super .print(x);
135: buffer.append(x);
136: }
137:
138: /* (non-Javadoc)
139: * @see java.io.PrintWriter#print(char[])
140: */
141: @Override
142: public void print(char[] x) {
143: super .print(x);
144: buffer.append(x);
145: }
146:
147: /* (non-Javadoc)
148: * @see java.io.PrintWriter#print(java.lang.String)
149: */
150: @Override
151: public void print(String x) {
152: super .print(x);
153: buffer.append(x);
154: }
155:
156: /* (non-Javadoc)
157: * @see java.io.PrintWriter#print(java.lang.Object)
158: */
159: @Override
160: public void print(Object x) {
161: super .print(x);
162: buffer.append(x);
163: }
164:
165: /* (non-Javadoc)
166: * @see java.io.PrintWriter#println()
167: */
168: @Override
169: public void println() {
170: synchronized (lock) {
171: printBuffer();
172: super .println();
173: }
174: }
175:
176: /* (non-Javadoc)
177: * @see java.io.PrintWriter#println(boolean)
178: */
179: @Override
180: public void println(boolean x) {
181: synchronized (lock) {
182: printBuffer();
183: super .println(x);
184: }
185: }
186:
187: /* (non-Javadoc)
188: * @see java.io.PrintWriter#println(char)
189: */
190: @Override
191: public void println(char x) {
192: synchronized (lock) {
193: printBuffer();
194: super .println(x);
195: }
196: }
197:
198: /* (non-Javadoc)
199: * @see java.io.PrintWriter#println(int)
200: */
201: @Override
202: public void println(int x) {
203: synchronized (lock) {
204: printBuffer();
205: super .println(x);
206: }
207: }
208:
209: /* (non-Javadoc)
210: * @see java.io.PrintWriter#println(long)
211: */
212: @Override
213: public void println(long x) {
214: synchronized (lock) {
215: printBuffer();
216: super .println(x);
217: }
218: }
219:
220: /* (non-Javadoc)
221: * @see java.io.PrintWriter#println(float)
222: */
223: @Override
224: public void println(float x) {
225: synchronized (lock) {
226: printBuffer();
227: super .println(x);
228: }
229: }
230:
231: /* (non-Javadoc)
232: * @see java.io.PrintWriter#println(double)
233: */
234: @Override
235: public void println(double x) {
236: synchronized (lock) {
237: printBuffer();
238: super .println(x);
239: }
240: }
241:
242: /* (non-Javadoc)
243: * @see java.io.PrintWriter#println(char[])
244: */
245: @Override
246: public void println(char[] x) {
247: synchronized (lock) {
248: printBuffer();
249: super .println(x);
250: }
251: }
252:
253: /* (non-Javadoc)
254: * @see java.io.PrintWriter#println(java.lang.String)
255: */
256: @Override
257: public void println(String x) {
258: synchronized (lock) {
259: printBuffer();
260: super .println(x);
261: }
262: }
263:
264: /* (non-Javadoc)
265: * @see java.io.PrintWriter#println(java.lang.Object)
266: */
267: @Override
268: public void println(Object x) {
269: synchronized (lock) {
270: printBuffer();
271: super .println(x);
272: }
273: }
274:
275: /**
276: * Write the characters in the print buffer out to the stream
277: */
278: private void printBuffer() {
279: if (buffer.length() > 0) {
280: log.debug(prefix + buffer.toString());
281: buffer.setLength(0);
282: }
283: }
284:
285: /**
286: * How to we prefix all the debugging lines?
287: * @return the prefix
288: */
289: public String getPrefix() {
290: return prefix;
291: }
292:
293: /**
294: * How to we prefix all the debugging lines?
295: * @param prefix the prefix to set
296: */
297: public void setPrefix(String prefix) {
298: this .prefix = prefix;
299: }
300:
301: /**
302: * How to we prefix all the debugging lines?
303: */
304: private String prefix;
305:
306: /**
307: * A buffer where we store stuff before a newline
308: */
309: protected final StringBuffer buffer = new StringBuffer();
310:
311: /**
312: * The log stream
313: */
314: private static final Log log = LogFactory
315: .getLog(DebuggingPrintWriter.class);
316: }
|