001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.printwriter;
018:
019: import org.objectweb.util.monolog.api.Logger;
020: import org.objectweb.util.monolog.api.BasicLevel;
021: import org.objectweb.util.monolog.api.LoggerFactory;
022: import org.objectweb.util.monolog.api.Loggable;
023:
024: import java.io.PrintStream;
025:
026: /**
027: * This class is a PrintStream wrapper. It exports the Printstream methods but
028: * fowards the message to a Logger. This implementation bufferizes the data when
029: * a print method is used. The buffer and the data are always written when a
030: * println method is used. No end of line are inserted by the println methods.
031: * A line is equals to a monolog message.
032: *
033: * @author S.Chassande-Barrioz
034: */
035: public class PrintStreamImpl extends PrintStream implements Loggable {
036:
037: protected Logger logger = null;
038: protected LoggerFactory loggerFactory = null;
039:
040: /**
041: * This field is the buffer which represents the current line.
042: */
043: protected String currentLine = "";
044:
045: protected int level;
046:
047: /**
048: * It builds a PrintWriterImpl instance. The default level is DEBUG
049: *
050: * @param l is the logger toward which the message must be send
051: * @throws NullPointerException if the parameter is null.
052: */
053: public PrintStreamImpl(Logger l) throws NullPointerException {
054: super (new EmptyOutputStream());
055: if (l == null)
056: throw new NullPointerException("Logger parameter is null");
057: logger = l;
058: level = BasicLevel.DEBUG;
059: }
060:
061: /**
062: * It builds a PrintWriterImpl instance. The default level is DEBUG
063: *
064: * @param l is the logger toward which the message must be send
065: * @param level is the level used to log message.
066: * @throws NullPointerException if the parameter is null.
067: */
068: public PrintStreamImpl(Logger l, int level)
069: throws NullPointerException {
070: super (new EmptyOutputStream());
071: if (l == null)
072: throw new NullPointerException("Logger parameter is null");
073: logger = l;
074: this .level = level;
075: }
076:
077: /**
078: * Retrieves the level of the messages
079: * @return an int value representing the message priority (BasicLevel.XXXX)
080: */
081: public int getLevel() {
082: return level;
083: }
084:
085: /**
086: * Changes the level of the messages
087: * @param level is the new level
088: */
089: public void setLevel(int level) {
090: this .level = level;
091: }
092:
093: // IMPLEMENTATION OF THE Loggable INTERFACE //
094: //------------------------------------------//
095:
096: /**
097: * Retrieves the logger instance used
098: */
099: public Logger getLogger() {
100: return logger;
101: }
102:
103: /**
104: * Assigns the logger instance to use
105: */
106: public void setLogger(Logger logger) {
107: this .logger = logger;
108: }
109:
110: /**
111: * Retrieves the logger factory instance used
112: */
113: public LoggerFactory getLoggerFactory() {
114: return loggerFactory;
115: }
116:
117: /**
118: * Assigns the logger factory instance to use
119: */
120: public void setLoggerFactory(LoggerFactory lf) {
121: this .loggerFactory = lf;
122: }
123:
124: // IMPLEMENTATION OF THE PrintWriter CLASS //
125: //-----------------------------------------//
126:
127: /**
128: * Writes the byte[] as a string in the buffer
129: */
130: public void write(byte[] bytes) {
131: currentLine += new String(bytes);
132: }
133:
134: /**
135: * Compare the inner loggers
136: */
137: public boolean equals(Object o) {
138: return o instanceof PrintStreamImpl
139: && ((PrintStreamImpl) o).logger == logger;
140: }
141:
142: /**
143: * Do nothing
144: */
145: public void flush() {
146: }
147:
148: /**
149: * Always throws a CloneNotSupportedException
150: */
151: protected Object clone() throws CloneNotSupportedException {
152: throw new CloneNotSupportedException();
153: }
154:
155: /**
156: * Do nothing
157: */
158: public void close() {
159: }
160:
161: /**
162: * Call the toString() method over the inner logger
163: */
164: public String toString() {
165: return logger.toString();
166: }
167:
168: /**
169: * Always retrieves 'false'
170: * @return
171: */
172: public boolean checkError() {
173: return false;
174: }
175:
176: /**
177: * Do nothing
178: */
179: protected void setError() {
180: }
181:
182: /**
183: * Writes the int value in the buffer
184: */
185: public void write(int i) {
186: currentLine += i;
187: }
188:
189: /**
190: * Writes the byte[] as a string in the buffer
191: */
192: public void write(byte[] bytes, int i, int i1) {
193: currentLine += new String(bytes, i, i1);
194: }
195:
196: /**
197: * Writes the boolean value in the buffer
198: */
199: public void print(boolean b) {
200: currentLine += b;
201: }
202:
203: /**
204: * Writes the char value in the buffer
205: */
206: public void print(char c) {
207: currentLine += c;
208: }
209:
210: /**
211: * Writes the int value in the buffer
212: */
213: public void print(int i) {
214: currentLine += i;
215: }
216:
217: /**
218: * Writes the long value in the buffer
219: */
220: public void print(long l) {
221: currentLine += l;
222: }
223:
224: /**
225: * Writes the float value in the buffer
226: */
227: public void print(float v) {
228: currentLine += v;
229: }
230:
231: /**
232: * Writes the double value in the buffer
233: */
234: public void print(double v) {
235: currentLine += v;
236: }
237:
238: /**
239: * Writes the char[] value as a String in the buffer
240: */
241: public void print(char[] chars) {
242: logger.log(level, currentLine + new String(chars));
243: currentLine = "";
244: }
245:
246: /**
247: * Writes the String value in the buffer
248: */
249: public void print(String s) {
250: currentLine += s;
251: }
252:
253: /**
254: * Writes the Object value as a String in the buffer
255: */
256: public void print(Object o) {
257: currentLine += o;
258: }
259:
260: /**
261: * log the buffer
262: */
263: public void println() {
264: logger.log(level, currentLine);
265: currentLine = "";
266: }
267:
268: /**
269: * 1/ Write a boolean value in the buffer.
270: * 2/ Log the buffer.
271: * 3/ Clear the buffer.
272: */
273: public void println(boolean b) {
274: logger.log(level, currentLine + b);
275: currentLine = "";
276: }
277:
278: /**
279: * 1/ Write a char value in the buffer
280: * 2/ Log the buffer
281: * 3/ Clear the buffer
282: */
283: public void println(char c) {
284: logger.log(level, currentLine + c);
285: currentLine = "";
286: }
287:
288: /**
289: * 1/ Write an int value in the buffer
290: * 2/ Log the buffer
291: * 3/ Clear the buffer
292: */
293: public void println(int i) {
294: logger.log(level, currentLine + i);
295: currentLine = "";
296: }
297:
298: /**
299: * 1/ Write a long value in the buffer
300: * 2/ Log the buffer
301: * 3/ Clear the buffer
302: */
303: public void println(long l) {
304: logger.log(level, currentLine + l);
305: currentLine = "";
306: }
307:
308: /**
309: * 1/ Write a float value in the buffer
310: * 2/ Log the buffer
311: * 3/ Clear the buffer
312: */
313: public void println(float v) {
314: logger.log(level, currentLine + v);
315: currentLine = "";
316: }
317:
318: /**
319: * 1/ Write a double value in the buffer
320: * 2/ Log the buffer
321: * 3/ Clear the buffer
322: */
323: public void println(double v) {
324: logger.log(level, currentLine + v);
325: currentLine = "";
326: }
327:
328: /**
329: * 1/ Write a char[] value in the buffer
330: * 2/ Log the buffer
331: * 3/ Clear the buffer
332: */
333: public void println(char[] chars) {
334: logger.log(level, currentLine + new String(chars));
335: currentLine = "";
336: }
337:
338: /**
339: * 1/ Write a String value in the buffer
340: * 2/ Log the buffer
341: * 3/ Clear the buffer
342: */
343: public void println(String s) {
344: logger.log(level, currentLine + s);
345: currentLine = "";
346: }
347:
348: /**
349: * 1/ Write a object value in the buffer
350: * 2/ Log the buffer
351: * 3/ Clear the buffer
352: */
353: public void println(Object o) {
354: logger.log(level, currentLine + o);
355: currentLine = "";
356: }
357: }
|