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.Loggable;
022: import org.objectweb.util.monolog.api.LoggerFactory;
023:
024: import java.io.PrintWriter;
025:
026: /**
027: * This class is a PrintWriter wrapper. It exports the PrintWriter 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 Sebastien Chassande-Barrioz
034: */
035: public class PrintWriterImpl extends PrintWriter implements Loggable {
036:
037: //private static Writer empty = new EmptyWriter();
038:
039: /**
040: * The inner logger instance
041: */
042: protected Logger logger = null;
043: protected LoggerFactory loggerFactory = null;
044:
045: protected int level;
046:
047: /**
048: * This field is the buffer which represents the current line.
049: */
050: protected String currentLine = "";
051:
052: /**
053: * This field indicates the setError method was called.
054: */
055: protected boolean errors = false;
056:
057: /**
058: * It builds a PrintWriterImpl instance. The default level is DEBUG
059: *
060: * @param l is the logger toward which the message must be send
061: * @throws NullPointerException if the parameter is null.
062: */
063: public PrintWriterImpl(Logger l) throws NullPointerException {
064: super (new EmptyWriter());
065: if (l == null)
066: throw new NullPointerException("Logger parameter is null");
067: logger = l;
068: level = BasicLevel.DEBUG;
069: }
070:
071: /**
072: * It builds a PrintWriterImpl instance. The default level is DEBUG
073: *
074: * @param logger is the logger toward which the message must be send
075: * @param loggerFactory is the loggerFactory of the logger
076: * @throws NullPointerException if one of the parameters is null.
077: */
078: public PrintWriterImpl(Logger logger, LoggerFactory loggerFactory)
079: throws NullPointerException {
080: this (logger);
081: if (loggerFactory == null)
082: throw new NullPointerException(
083: "LoggerFactory parameter is null");
084: this .loggerFactory = loggerFactory;
085: }
086:
087: /**
088: * It builds a PrintWriterImpl instance.
089: *
090: * @param l is the logger toward which the message must be send
091: * @param level is the level used to log message.
092: * @throws NullPointerException if the parameter is null.
093: */
094: public PrintWriterImpl(Logger l, int level)
095: throws NullPointerException {
096: super (new EmptyWriter());
097: if (l == null)
098: throw new NullPointerException("Logger parameter is null");
099: logger = l;
100: this .level = level;
101: }
102:
103: public int getLevel() {
104: return level;
105: }
106:
107: public void setLevel(int level) {
108: this .level = level;
109: }
110:
111: // IMPLEMENTATION OF THE Loggable INTERFACE //
112: //------------------------------------------//
113:
114: /**
115: * Retrieves the logger instance used
116: */
117: public Logger getLogger() {
118: return logger;
119: }
120:
121: /**
122: * Assigns the logger instance to use
123: */
124: public void setLogger(Logger logger) {
125: this .logger = logger;
126: }
127:
128: /**
129: * Retrieves the logger factory instance used
130: */
131: public LoggerFactory getLoggerFactory() {
132: return loggerFactory;
133: }
134:
135: /**
136: * Assigns the logger factory instance to use
137: */
138: public void setLoggerFactory(LoggerFactory lf) {
139: this .loggerFactory = lf;
140: }
141:
142: // IMPLEMENTATION OF THE PrintWriter CLASS //
143: //-----------------------------------------//
144:
145: /**
146: * Flush the stream and check its error state.
147: */
148: public boolean checkError() {
149: if (currentLine.length() > 0) {
150: logger.log(level, currentLine);
151: currentLine = "";
152: }
153: return errors;
154: }
155:
156: /**
157: * It writes the buffer if it is not empty
158: */
159: public void close() {
160: if (currentLine.length() > 0) {
161: logger.log(level, currentLine);
162: currentLine = "";
163: }
164: }
165:
166: /**
167: * It writes the buffer if it is not empty
168: */
169: public void flush() {
170: if (currentLine.length() > 0) {
171: logger.log(level, currentLine);
172: currentLine = "";
173: }
174: }
175:
176: /**
177: * Print a boolean value in the buffer.
178: */
179: public void print(boolean x) {
180: currentLine += x;
181: }
182:
183: /**
184: * Print a character in the buffer.
185: */
186: public void print(char x) {
187: currentLine += x;
188: }
189:
190: /**
191: * Print an array of characters in the buffer.
192: */
193: public void print(char[] x) {
194: currentLine += new String(x);
195: }
196:
197: /**
198: * Print a double-precision floating-point number in the buffer.
199: */
200: public void print(double x) {
201: currentLine += x;
202: }
203:
204: /**
205: * Print a floating-point number in the buffer.
206: */
207: public void print(float x) {
208: currentLine += x;
209: }
210:
211: /**
212: * Print an integer in the buffer.
213: */
214: public void print(int x) {
215: currentLine += x;
216: }
217:
218: /**
219: * Print a long integer in the buffer.
220: */
221: public void print(long x) {
222: currentLine += x;
223: }
224:
225: /**
226: * Print an object in the buffer.
227: */
228: public void print(Object x) {
229: currentLine += x;
230: }
231:
232: /**
233: * Print a string in the buffer.
234: */
235: public void print(String x) {
236: currentLine += x;
237: }
238:
239: /**
240: * Send the buffer to the logger
241: */
242: public void println() {
243: logger.log(level, currentLine);
244: currentLine = "";
245: }
246:
247: /**
248: * Send the buffer and a boolean value to the logger
249: */
250: public void println(boolean x) {
251: logger.log(level, currentLine + x);
252: currentLine = "";
253: }
254:
255: /**
256: * Send the buffer and a character to the logger
257: */
258: public void println(char x) {
259: logger.log(level, currentLine + x);
260: currentLine = "";
261: }
262:
263: /**
264: * Send the buffer and an array of characters to the logger
265: */
266: public void println(char[] x) {
267: logger.log(level, currentLine + new String(x));
268: currentLine = "";
269: }
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: * Send the buffer and a long integer number to the logger
298: */
299: public void println(long x) {
300: logger.log(level, currentLine + x);
301: currentLine = "";
302: }
303:
304: /**
305: * Send the buffer and an object to the logger
306: */
307: public void println(Object x) {
308: logger.log(level, currentLine + x);
309: currentLine = "";
310: }
311:
312: /**
313: * Send the buffer and a String to the logger
314: */
315: public void println(String x) {
316: logger.log(level, currentLine + x);
317: currentLine = "";
318: }
319:
320: /**
321: * Indicate that an error has occurred.
322: */
323: protected void setError() {
324: errors = true;
325: logger.log(BasicLevel.ERROR, currentLine + "PrintWriter error");
326: currentLine = "";
327: }
328:
329: /**
330: * Write an array of characters in the buffer.
331: */
332: public void write(char[] buf) {
333: currentLine += new String(buf);
334: }
335:
336: /**
337: * Write a portion of an array of characters in the buffer.
338: */
339: public void write(char[] buf, int off, int len) {
340: currentLine += new String(buf, off, len);
341: }
342:
343: /**
344: * Write a single character in the buffer.
345: */
346: public void write(int c) {
347: currentLine += c;
348: }
349:
350: /**
351: * Write a string in the buffer.
352: */
353: public void write(String s) {
354: currentLine += s;
355: }
356:
357: /**
358: * Write a portion of a string in the buffer.
359: */
360: public void write(String s, int off, int len) {
361: currentLine += (s != null ? s.substring(off, len) : "");
362: }
363:
364: }
|