01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.wfs;
17:
18: import java.io.IOException;
19: import java.io.Writer;
20: import java.util.logging.Level;
21: import java.util.logging.Logger;
22:
23: /**
24: * A decorator that writes to the log as well as the wrapped writer.
25: *
26: * @author Jesse
27: * @since 1.1.0
28: */
29: public class LogWriterDecorator extends Writer {
30:
31: private Writer delegate;
32: private Level level;
33:
34: StringBuffer buffer = new StringBuffer("Output: ");
35: private Logger logger;
36:
37: public LogWriterDecorator(Writer w, Logger logger, Level logLevel) {
38: this .delegate = w;
39: this .level = logLevel;
40: this .logger = logger;
41: }
42:
43: public synchronized void close() throws IOException {
44: delegate.close();
45: logger.log(level, buffer.toString());
46: buffer = new StringBuffer("Output: ");
47: }
48:
49: public synchronized void flush() throws IOException {
50: delegate.flush();
51: logger.log(level, buffer.toString());
52: buffer = new StringBuffer("Output: ");
53: }
54:
55: public synchronized void write(char[] cbuf, int off, int len)
56: throws IOException {
57: char[] msg = new char[len];
58: System.arraycopy(cbuf, off, msg, 0, len);
59: buffer.append(msg);
60: delegate.write(cbuf, off, len);
61: }
62:
63: }
|