01: /*
02: * The contents of this file are subject to the Mozilla Public License
03: * Version 1.1 (the "License"); you may not use this file except in
04: * compliance with the License. You may obtain a copy of the License at
05: * http://www.mozilla.org/MPL/
06: *
07: * Software distributed under the License is distributed on an "AS IS"
08: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
09: * License for the specific language governing rights and limitations
10: * under the License.
11: *
12: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
13: *
14: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
15: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
16: *
17: * Contributor(s):
18: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
19: *
20: * If you didn't download this code from the following link, you should check
21: * if you aren't using an obsolete version: http://www.isqlviewer.com
22: */
23: package org.isqlviewer.util;
24:
25: import java.io.OutputStream;
26:
27: import org.apache.log4j.Logger;
28:
29: /**
30: * OutputStream wrapper for provider streams and writers for logging purposes.
31: * <p>
32: *
33: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
34: * @version 1.0
35: */
36: public class Log4jOutputStream extends OutputStream {
37:
38: private StringBuffer buf = new StringBuffer("");
39: private final Logger logger;
40:
41: public Log4jOutputStream(Logger logger) {
42:
43: this .logger = logger;
44:
45: }
46:
47: @Override
48: public synchronized void write(int b) {
49:
50: buf.append(Character.toString((char) b));
51: }
52:
53: @Override
54: public synchronized void write(byte[] b, int offset, int length) {
55:
56: buf.append(new String(b, offset, length));
57:
58: }
59:
60: @Override
61: public synchronized void write(byte[] b) {
62:
63: buf.append(new String(b));
64: }
65:
66: @Override
67: public synchronized void flush() {
68:
69: synchronized (buf) {
70: if (buf.length() > 0) {
71: char last = buf.charAt(buf.length() - 1);
72: if (last == '\n' || last == '\r') {
73: String text = buf.toString().trim();
74: logger.info(text);
75: buf.setLength(0);
76: }
77: }
78: }
79: }
80: }
|