KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > DebuggingPrintWriter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.util;
17
18 import java.io.BufferedWriter JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23
24 /**
25  * A PrintWriter that also sends its output to a log stream
26  * @author Joe Walker [joe at getahead dot ltd dot uk]
27  */

28 public class DebuggingPrintWriter extends PrintWriter JavaDoc
29 {
30     /**
31      * Create a new PrintWriter, without automatic line flushing.
32      * @param prefix A tag to prefix lines with for debugging purposes
33      * @param out A character-output stream
34      */

35     public DebuggingPrintWriter(String JavaDoc prefix, Writer JavaDoc out)
36     {
37         super(out, false);
38         this.prefix = prefix;
39     }
40
41     /**
42      * Create a new PrintWriter.
43      * @param prefix A tag to prefix lines with for debugging purposes
44      * @param out A character-output stream
45      * @param autoFlush A boolean; if true, the println() methods will flush the output buffer
46      */

47     public DebuggingPrintWriter(String JavaDoc prefix, Writer JavaDoc out, boolean autoFlush)
48     {
49         super(out, autoFlush);
50         this.prefix = prefix;
51     }
52
53     /**
54      * Create a new PrintWriter, without automatic line flushing, from an
55      * existing OutputStream. This convenience constructor creates the
56      * necessary intermediate OutputStreamWriter, which will convert characters
57      * into bytes using the default character encoding.
58      * @param prefix A tag to prefix lines with for debugging purposes
59      * @param out An output stream
60      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
61      */

62     public DebuggingPrintWriter(String JavaDoc prefix, OutputStream JavaDoc out)
63     {
64         super(out, false);
65         this.prefix = prefix;
66     }
67
68     /**
69      * Create a new PrintWriter from an existing OutputStream. This convenience
70      * constructor creates the necessary intermediate OutputStreamWriter, which
71      * will convert characters into bytes using the default character encoding.
72      * @param prefix A tag to prefix lines with for debugging purposes
73      * @param out An output stream
74      * @param autoFlush A boolean; if true, the println() methods will flush the output buffer
75      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
76      */

77     public DebuggingPrintWriter(String JavaDoc prefix, OutputStream JavaDoc out, boolean autoFlush)
78     {
79         super(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out)), autoFlush);
80         this.prefix = prefix;
81     }
82
83     /* (non-Javadoc)
84      * @see java.io.PrintWriter#print(boolean)
85      */

86     public void print(boolean x)
87     {
88         super.print(x);
89         buffer.append(x);
90     }
91
92     /* (non-Javadoc)
93      * @see java.io.PrintWriter#print(char)
94      */

95     public void print(char x)
96     {
97         super.print(x);
98         buffer.append(x);
99     }
100
101     /* (non-Javadoc)
102      * @see java.io.PrintWriter#print(int)
103      */

104     public void print(int x)
105     {
106         super.print(x);
107         buffer.append(x);
108     }
109
110     /* (non-Javadoc)
111      * @see java.io.PrintWriter#print(long)
112      */

113     public void print(long x)
114     {
115         super.print(x);
116         buffer.append(x);
117     }
118
119     /* (non-Javadoc)
120      * @see java.io.PrintWriter#print(float)
121      */

122     public void print(float x)
123     {
124         super.print(x);
125         buffer.append(x);
126     }
127
128     /* (non-Javadoc)
129      * @see java.io.PrintWriter#print(double)
130      */

131     public void print(double x)
132     {
133         super.print(x);
134         buffer.append(x);
135     }
136
137     /* (non-Javadoc)
138      * @see java.io.PrintWriter#print(char[])
139      */

140     public void print(char x[])
141     {
142         super.print(x);
143         buffer.append(x);
144     }
145
146     /* (non-Javadoc)
147      * @see java.io.PrintWriter#print(java.lang.String)
148      */

149     public void print(String JavaDoc x)
150     {
151         super.print(x);
152         buffer.append(x);
153     }
154
155     /* (non-Javadoc)
156      * @see java.io.PrintWriter#print(java.lang.Object)
157      */

158     public void print(Object JavaDoc x)
159     {
160         super.print(x);
161         buffer.append(x);
162     }
163
164     /* (non-Javadoc)
165      * @see java.io.PrintWriter#println()
166      */

167     public void println()
168     {
169         synchronized (lock)
170         {
171             printBuffer();
172             super.println();
173         }
174     }
175
176     /* (non-Javadoc)
177      * @see java.io.PrintWriter#println(boolean)
178      */

179     public void println(boolean x)
180     {
181         synchronized (lock)
182         {
183             printBuffer();
184             super.println(x);
185         }
186     }
187
188     /* (non-Javadoc)
189      * @see java.io.PrintWriter#println(char)
190      */

191     public void println(char x)
192     {
193         synchronized (lock)
194         {
195             printBuffer();
196             super.println(x);
197         }
198     }
199
200     /* (non-Javadoc)
201      * @see java.io.PrintWriter#println(int)
202      */

203     public void println(int x)
204     {
205         synchronized (lock)
206         {
207             printBuffer();
208             super.println(x);
209         }
210     }
211
212     /* (non-Javadoc)
213      * @see java.io.PrintWriter#println(long)
214      */

215     public void println(long x)
216     {
217         synchronized (lock)
218         {
219             printBuffer();
220             super.println(x);
221         }
222     }
223
224     /* (non-Javadoc)
225      * @see java.io.PrintWriter#println(float)
226      */

227     public void println(float x)
228     {
229         synchronized (lock)
230         {
231             printBuffer();
232             super.println(x);
233         }
234     }
235
236     /* (non-Javadoc)
237      * @see java.io.PrintWriter#println(double)
238      */

239     public void println(double x)
240     {
241         synchronized (lock)
242         {
243             printBuffer();
244             super.println(x);
245         }
246     }
247
248     /* (non-Javadoc)
249      * @see java.io.PrintWriter#println(char[])
250      */

251     public void println(char x[])
252     {
253         synchronized (lock)
254         {
255             printBuffer();
256             super.println(x);
257         }
258     }
259
260     /* (non-Javadoc)
261      * @see java.io.PrintWriter#println(java.lang.String)
262      */

263     public void println(String JavaDoc x)
264     {
265         synchronized (lock)
266         {
267             printBuffer();
268             super.println(x);
269         }
270     }
271
272     /* (non-Javadoc)
273      * @see java.io.PrintWriter#println(java.lang.Object)
274      */

275     public void println(Object JavaDoc x)
276     {
277         synchronized (lock)
278         {
279             printBuffer();
280             super.println(x);
281         }
282     }
283
284     /**
285      * Write the characters in the print buffer out to the stream
286      */

287     private void printBuffer()
288     {
289         if (buffer.length() > 0)
290         {
291             log.debug(prefix + buffer.toString());
292             buffer.setLength(0);
293         }
294     }
295
296     /**
297      * How to we prefix all the debugging lines?
298      * @return the prefix
299      */

300     public String JavaDoc getPrefix()
301     {
302         return prefix;
303     }
304
305     /**
306      * How to we prefix all the debugging lines?
307      * @param prefix the prefix to set
308      */

309     public void setPrefix(String JavaDoc prefix)
310     {
311         this.prefix = prefix;
312     }
313
314     /**
315      * How to we prefix all the debugging lines?
316      */

317     private String JavaDoc prefix;
318
319     /**
320      * A buffer where we store stuff before a newline
321      */

322     protected final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
323
324     /**
325      * The log stream
326      */

327     private static final Logger log = Logger.getLogger(DebuggingPrintWriter.class);
328 }
329
Popular Tags