01: /*
02: * Copyright 2007 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.antinstaller.runtime;
17:
18: import java.io.OutputStream;
19: import java.io.PrintStream;
20:
21: import org.tp23.antinstaller.InstallerContext;
22: import org.tp23.antinstaller.renderer.AntOutputRenderer;
23:
24: /**
25: * An Ant output stream that also logs to the default logger
26: * @author teknopaul
27: *
28: */
29: public class LoggingAntOutputRenderer implements AntOutputRenderer {
30:
31: private PrintStream out;
32: private PrintStream err;
33: private InstallerContext ctx;
34:
35: public LoggingAntOutputRenderer(PrintStream out, PrintStream err,
36: InstallerContext ctx) {
37: this .out = out;
38: this .err = err;
39: this .ctx = ctx;
40: }
41:
42: public PrintStream getOut() {
43: return new LoggingPrintStream(out);
44: }
45:
46: public PrintStream getErr() {
47: return new LoggingPrintStream(err);
48: }
49:
50: /**
51: * This works with the Sun APIs with other it might miss some messages if there exists a PrintStream
52: * implementation that writes directly to the strem without calling its own write() methods, not the end of the world
53: * PrintStreams ignore all IOExceptions anyway
54: * @author teknopaul
55: */
56: private class LoggingPrintStream extends PrintStream {
57:
58: public LoggingPrintStream(PrintStream out) {
59: super (out, true);
60: }
61:
62: public void write(byte[] buf, int off, int len) {
63: ctx.loge(new String(buf, off, len));
64: super .write(buf, off, len);
65: }
66:
67: public void write(int b) {
68: ctx.loge((char) b);
69: super.write(b);
70: }
71: }
72: }
|