KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class PrintStreamImpl
37         extends PrintStream JavaDoc
38         implements Loggable {
39
40
41     protected Logger logger = null;
42     protected LoggerFactory loggerFactory = null;
43
44     /**
45      * This field is the buffer which represents the current line.
46      */

47     protected String JavaDoc currentLine = "";
48
49     protected int level;
50
51     /**
52      * It builds a PrintWriterImpl instance. The default level is DEBUG
53      *
54      * @param l is the logger toward which the message must be send
55      * @throws NullPointerException if the parameter is null.
56      */

57     public PrintStreamImpl(Logger l) throws NullPointerException JavaDoc {
58         super(new EmptyOutputStream());
59         if (l == null)
60             throw new NullPointerException JavaDoc("Logger parameter is null");
61         logger = l;
62         level = BasicLevel.DEBUG;
63     }
64
65     /**
66      * It builds a PrintWriterImpl instance. The default level is DEBUG
67      *
68      * @param l is the logger toward which the message must be send
69      * @param level is the level used to log message.
70      * @throws NullPointerException if the parameter is null.
71      */

72     public PrintStreamImpl(Logger l, int level) throws NullPointerException JavaDoc {
73         super(new EmptyOutputStream());
74         if (l == null)
75             throw new NullPointerException JavaDoc("Logger parameter is null");
76         logger = l;
77         this.level = level;
78     }
79
80     /**
81      * Retrieves the level of the messages
82      * @return an int value representing the message priority (BasicLevel.XXXX)
83      */

84     public int getLevel() {
85         return level;
86     }
87
88     /**
89      * Changes the level of the messages
90      * @param level is the new level
91      */

92     public void setLevel(int level) {
93         this.level = level;
94     }
95
96     // IMPLEMENTATION OF THE Loggable INTERFACE //
97
//------------------------------------------//
98

99     /**
100      * Retrieves the logger instance used
101      */

102     public Logger getLogger() {
103         return logger;
104     }
105
106     /**
107      * Assigns the logger instance to use
108      */

109     public void setLogger(Logger logger) {
110         this.logger = logger;
111     }
112
113     /**
114      * Retrieves the logger factory instance used
115      */

116     public LoggerFactory getLoggerFactory() {
117         return loggerFactory;
118     }
119
120     /**
121      * Assigns the logger factory instance to use
122      */

123     public void setLoggerFactory(LoggerFactory lf) {
124         this.loggerFactory = lf;
125     }
126
127     // IMPLEMENTATION OF THE PrintWriter CLASS //
128
//-----------------------------------------//
129

130
131     /**
132      * Writes the byte[] as a string in the buffer
133      */

134     public void write(byte[] bytes) {
135         currentLine += new String JavaDoc(bytes);
136     }
137
138     /**
139      * Compare the inner loggers
140      */

141     public boolean equals(Object JavaDoc o) {
142         return o instanceof PrintStreamImpl
143             && ((PrintStreamImpl) o).logger == logger;
144     }
145
146     /**
147      * Do nothing
148      */

149     public void flush() {
150     }
151
152     /**
153      * Always throws a CloneNotSupportedException
154      */

155     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
156         throw new CloneNotSupportedException JavaDoc();
157     }
158
159     /**
160      * Do nothing
161      */

162     public void close() {
163     }
164
165     /**
166      * Call the toString() method over the inner logger
167      */

168     public String JavaDoc toString() {
169         return logger.toString();
170     }
171
172     /**
173      * Always retrieves 'false'
174      * @return
175      */

176     public boolean checkError() {
177         return false;
178     }
179
180
181     /**
182      * Do nothing
183      */

184     protected void setError() {
185     }
186
187     /**
188      * Writes the int value in the buffer
189      */

190     public void write(int i) {
191         currentLine += i;
192     }
193
194     /**
195      * Writes the byte[] as a string in the buffer
196      */

197     public void write(byte[] bytes, int i, int i1) {
198         currentLine += new String JavaDoc(bytes, i, i1);
199     }
200
201     /**
202      * Writes the boolean value in the buffer
203      */

204     public void print(boolean b) {
205         currentLine += b;
206     }
207
208     /**
209      * Writes the char value in the buffer
210      */

211     public void print(char c) {
212         currentLine += c;
213     }
214
215     /**
216      * Writes the int value in the buffer
217      */

218     public void print(int i) {
219         currentLine += i;
220     }
221
222     /**
223      * Writes the long value in the buffer
224      */

225     public void print(long l) {
226         currentLine += l;
227     }
228
229     /**
230      * Writes the float value in the buffer
231      */

232     public void print(float v) {
233         currentLine += v;
234     }
235
236     /**
237      * Writes the double value in the buffer
238      */

239     public void print(double v) {
240         currentLine += v;
241     }
242
243     /**
244      * Writes the char[] value as a String in the buffer
245      */

246     public void print(char[] chars) {
247         logger.log(level, currentLine + new String JavaDoc(chars));
248         currentLine = "";
249     }
250
251     /**
252      * Writes the String value in the buffer
253      */

254     public void print(String JavaDoc s) {
255         currentLine += s;
256     }
257
258     /**
259      * Writes the Object value as a String in the buffer
260      */

261     public void print(Object JavaDoc o) {
262         currentLine += o;
263     }
264
265     /**
266      * log the buffer
267      */

268     public void println() {
269         logger.log(level, currentLine);
270         currentLine = "";
271     }
272
273     /**
274      * 1/ Write a boolean value in the buffer.
275      * 2/ Log the buffer.
276      * 3/ Clear the buffer.
277      */

278     public void println(boolean b) {
279         logger.log(level, currentLine + b);
280         currentLine = "";
281     }
282
283     /**
284      * 1/ Write a char value in the buffer
285      * 2/ Log the buffer
286      * 3/ Clear the buffer
287      */

288     public void println(char c) {
289         logger.log(level, currentLine + c);
290         currentLine = "";
291     }
292
293     /**
294      * 1/ Write an int value in the buffer
295      * 2/ Log the buffer
296      * 3/ Clear the buffer
297      */

298     public void println(int i) {
299         logger.log(level, currentLine + i);
300         currentLine = "";
301     }
302
303     /**
304      * 1/ Write a long value in the buffer
305      * 2/ Log the buffer
306      * 3/ Clear the buffer
307      */

308     public void println(long l) {
309         logger.log(level, currentLine + l);
310         currentLine = "";
311     }
312
313     /**
314      * 1/ Write a float value in the buffer
315      * 2/ Log the buffer
316      * 3/ Clear the buffer
317      */

318     public void println(float v) {
319         logger.log(level, currentLine + v);
320         currentLine = "";
321     }
322
323     /**
324      * 1/ Write a double value in the buffer
325      * 2/ Log the buffer
326      * 3/ Clear the buffer
327      */

328     public void println(double v) {
329         logger.log(level, currentLine + v);
330         currentLine = "";
331     }
332
333     /**
334      * 1/ Write a char[] value in the buffer
335      * 2/ Log the buffer
336      * 3/ Clear the buffer
337      */

338     public void println(char[] chars) {
339         logger.log(level, currentLine + new String JavaDoc(chars));
340         currentLine = "";
341     }
342
343     /**
344      * 1/ Write a String value in the buffer
345      * 2/ Log the buffer
346      * 3/ Clear the buffer
347      */

348     public void println(String JavaDoc s) {
349         logger.log(level, currentLine + s);
350         currentLine = "";
351     }
352
353     /**
354      * 1/ Write a object value in the buffer
355      * 2/ Log the buffer
356      * 3/ Clear the buffer
357      */

358     public void println(Object JavaDoc o) {
359         logger.log(level, currentLine + o);
360         currentLine = "";
361     }
362 }
363
Popular Tags