KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > printwriter > PrintWriterImpl


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.util.monolog.wrapper.printwriter;
20
21 import org.objectweb.util.monolog.api.Logger;
22 import org.objectweb.util.monolog.api.BasicLevel;
23 import org.objectweb.util.monolog.api.Loggable;
24 import org.objectweb.util.monolog.api.LoggerFactory;
25
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * This class is a PrintWriter wrapper. It exports the PrintWriter methods but
30  * fowards the message to a Logger. This implementation bufferizes the data when
31  * a print method is used. The buffer and the data are always written when a
32  * println method is used. No end of line are inserted by the println methods.
33  * A line is equals to a monolog message.
34  *
35  * @author Sebastien Chassande-Barrioz
36  */

37 public class PrintWriterImpl
38         extends PrintWriter JavaDoc
39         implements Loggable {
40
41     //private static Writer empty = new EmptyWriter();
42

43     /**
44      * The inner logger instance
45      */

46     protected Logger logger = null;
47     protected LoggerFactory loggerFactory = null;
48
49     protected int level;
50
51     /**
52      * This field is the buffer which represents the current line.
53      */

54     protected String JavaDoc currentLine = "";
55
56     /**
57      * This field indicates the setError method was called.
58      */

59     protected boolean errors = false;
60
61     /**
62      * It builds a PrintWriterImpl instance. The default level is DEBUG
63      *
64      * @param l is the logger toward which the message must be send
65      * @throws NullPointerException if the parameter is null.
66      */

67     public PrintWriterImpl(Logger l) throws NullPointerException JavaDoc {
68         super(new EmptyWriter());
69         if (l==null)
70             throw new NullPointerException JavaDoc("Logger parameter is null");
71         logger = l;
72         level = BasicLevel.DEBUG;
73     }
74
75     /**
76      * It builds a PrintWriterImpl instance. The default level is DEBUG
77      *
78      * @param logger is the logger toward which the message must be send
79      * @param loggerFactory is the loggerFactory of the logger
80      * @throws NullPointerException if one of the parameters is null.
81      */

82     public PrintWriterImpl(Logger logger,
83                            LoggerFactory loggerFactory) throws NullPointerException JavaDoc {
84         this(logger);
85         if (loggerFactory==null)
86             throw new NullPointerException JavaDoc("LoggerFactory parameter is null");
87         this.loggerFactory = loggerFactory;
88     }
89
90     /**
91      * It builds a PrintWriterImpl instance.
92      *
93      * @param l is the logger toward which the message must be send
94      * @param level is the level used to log message.
95      * @throws NullPointerException if the parameter is null.
96      */

97     public PrintWriterImpl(Logger l, int level) throws NullPointerException JavaDoc {
98         super(new EmptyWriter());
99         if (l==null)
100             throw new NullPointerException JavaDoc("Logger parameter is null");
101         logger = l;
102         this.level = level;
103     }
104
105     public int getLevel() {
106         return level;
107     }
108
109     public void setLevel(int level) {
110         this.level = level;
111     }
112
113     // IMPLEMENTATION OF THE Loggable INTERFACE //
114
//------------------------------------------//
115

116     /**
117      * Retrieves the logger instance used
118      */

119     public Logger getLogger() {
120         return logger;
121     }
122
123     /**
124      * Assigns the logger instance to use
125      */

126     public void setLogger(Logger logger) {
127         this.logger = logger;
128     }
129
130     /**
131      * Retrieves the logger factory instance used
132      */

133     public LoggerFactory getLoggerFactory() {
134         return loggerFactory;
135     }
136
137     /**
138      * Assigns the logger factory instance to use
139      */

140     public void setLoggerFactory(LoggerFactory lf) {
141         this.loggerFactory = lf;
142     }
143
144     // IMPLEMENTATION OF THE PrintWriter CLASS //
145
//-----------------------------------------//
146

147     /**
148      * Flush the stream and check its error state.
149      */

150     public boolean checkError() {
151         if (currentLine.length()>0) {
152             logger.log(level, currentLine);
153             currentLine = "";
154         }
155         return errors;
156     }
157     /**
158      * It writes the buffer if it is not empty
159      */

160     public void close() {
161         if (currentLine.length()>0) {
162             logger.log(level, currentLine);
163             currentLine = "";
164         }
165     }
166
167     /**
168      * It writes the buffer if it is not empty
169      */

170     public void flush() {
171         if (currentLine.length()>0) {
172             logger.log(level, currentLine);
173             currentLine = "";
174         }
175     }
176
177     /**
178      * Print a boolean value in the buffer.
179      */

180     public void print(boolean x) {
181         currentLine += x;
182     }
183
184     /**
185      * Print a character in the buffer.
186      */

187     public void print(char x) {
188         currentLine += x;
189     }
190
191     /**
192      * Print an array of characters in the buffer.
193      */

194     public void print(char[] x) {
195         currentLine += new String JavaDoc(x);
196     }
197
198     /**
199      * Print a double-precision floating-point number in the buffer.
200      */

201     public void print(double x) {
202         currentLine += x;
203     }
204
205     /**
206      * Print a floating-point number in the buffer.
207      */

208     public void print(float x) {
209         currentLine += x;
210     }
211
212     /**
213      * Print an integer in the buffer.
214      */

215     public void print(int x) {
216         currentLine += x;
217     }
218
219     /**
220      * Print a long integer in the buffer.
221      */

222     public void print(long x) {
223         currentLine += x;
224     }
225
226     /**
227      * Print an object in the buffer.
228      */

229     public void print(Object JavaDoc x) {
230         currentLine += x;
231     }
232
233     /**
234      * Print a string in the buffer.
235      */

236     public void print(String JavaDoc x) {
237         currentLine += x;
238     }
239
240     /**
241      * Send the buffer to the logger
242      */

243     public void println() {
244         logger.log(level, currentLine);
245         currentLine = "";
246     }
247
248     /**
249      * Send the buffer and a boolean value to the logger
250      */

251     public void println(boolean x) {
252         logger.log(level, currentLine + x);
253         currentLine = "";
254     }
255
256     /**
257      * Send the buffer and a character to the logger
258      */

259     public void println(char x) {
260         logger.log(level, currentLine + x);
261         currentLine = "";
262     }
263
264     /**
265      * Send the buffer and an array of characters to the logger
266      */

267     public void println(char[] x) {
268         logger.log(level, currentLine + new String JavaDoc(x));
269         currentLine = "";
270     }
271     /**
272      * Send the buffer and a a double-precision floating-point number to the
273      * logger.
274      */

275     public void println(double x) {
276         logger.log(level, currentLine + x);
277         currentLine = "";
278     }
279
280     /**
281      * Send the buffer and a floating-point number to the logger
282      */

283     public void println(float x) {
284         logger.log(level, currentLine + x);
285         currentLine = "";
286     }
287
288     /**
289      * Send the buffer and an integer to the logger
290      */

291     public void println(int x) {
292         logger.log(level, currentLine + x);
293         currentLine = "";
294     }
295
296
297     /**
298      * Send the buffer and a long integer number to the logger
299      */

300     public void println(long x) {
301         logger.log(level, currentLine + x);
302         currentLine = "";
303     }
304
305     /**
306      * Send the buffer and an object to the logger
307      */

308     public void println(Object JavaDoc x) {
309         logger.log(level, currentLine + x);
310         currentLine = "";
311     }
312
313     /**
314      * Send the buffer and a String to the logger
315      */

316     public void println(String JavaDoc x) {
317         logger.log(level, currentLine + x);
318         currentLine = "";
319     }
320
321     /**
322      * Indicate that an error has occurred.
323      */

324     protected void setError() {
325         errors = true;
326         logger.log(BasicLevel.ERROR, currentLine + "PrintWriter error");
327         currentLine = "";
328     }
329
330     /**
331      * Write an array of characters in the buffer.
332      */

333     public void write(char[] buf) {
334         currentLine += new String JavaDoc(buf);
335     }
336
337     /**
338      * Write a portion of an array of characters in the buffer.
339      */

340     public void write(char[] buf, int off, int len) {
341         currentLine += new String JavaDoc(buf, off, len);
342     }
343
344     /**
345      * Write a single character in the buffer.
346      */

347     public void write(int c) {
348         currentLine += c;
349     }
350
351     /**
352      * Write a string in the buffer.
353      */

354     public void write(String JavaDoc s) {
355         currentLine += s;
356     }
357
358     /**
359      * Write a portion of a string in the buffer.
360      */

361     public void write(String JavaDoc s, int off, int len) {
362         currentLine += (s!=null ? s.substring(off, len) : "");
363     }
364
365 }
366
Popular Tags