-
Notifications
You must be signed in to change notification settings - Fork 40
/
TeePrintStream.java
104 lines (91 loc) · 3.31 KB
/
TeePrintStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
/** TeePrintStream tees all PrintStream operations into a file, rather
* like the UNIX tee(1) command. It is a PrintStream subclass. The
* expected usage would be something like the following:
* <PRE>
* ...
* TeePrintStream ts = new TeePrintStream(System.err, "err.log");
* System.setErr(ts);
* // ...lots of code that occasionally writes to System.err...
* ts.close();
* ...
* <PRE>
* <P>I only override Constructors, the write(), check() and close() methods,
* since any of the print() or println() methods must go through these.
* Thanks to Svante Karlsson for help formulating this.
* <br/>
* Note: there is another way of doing this, using a FilterStream;
* see the example at http://www.javaspecialists.eu/archive/Issue003.html
* (written a year after the initial import of my version).
* @author Ian F. Darwin, http://www.darwinsys.com/
*/
// BEGIN main
public class TeePrintStream extends PrintStream {
/** The original/direct print stream */
protected PrintStream parent;
/** The filename we are tee-ing too, if known;
* intended for use in future error reporting.
*/
protected String fileName;
/** The name for when the input filename is not known */
private static final String UNKNOWN_NAME = "(opened Stream)";
/** Construct a TeePrintStream given an existing PrintStream,
* an opened OutputStream, and a boolean to control auto-flush.
* This is the main constructor, to which others delegate via "this".
*/
public TeePrintStream(PrintStream orig, OutputStream os, boolean flush)
throws IOException {
super(os, true);
fileName = UNKNOWN_NAME;
parent = orig;
}
/** Construct a TeePrintStream given an existing PrintStream and
* an opened OutputStream.
*/
public TeePrintStream(PrintStream orig, OutputStream os)
throws IOException {
this(orig, os, true);
}
/* Construct a TeePrintStream given an existing Stream and a filename.
*/
public TeePrintStream(PrintStream os, String fn) throws IOException {
this(os, fn, true);
}
/* Construct a TeePrintStream given an existing Stream, a filename,
* and a boolean to control the flush operation.
*/
public TeePrintStream(PrintStream orig, String fn, boolean flush)
throws IOException {
this(orig, new FileOutputStream(fn), flush);
fileName = fn;
}
/** Return true if either stream has an error. */
public boolean checkError() {
return parent.checkError() || super.checkError();
}
/** override write(). This is the actual "tee" operation. */
public void write(int x) {
parent.write(x); // "write once;
super.write(x); // write somewhere else."
}
/** override write(). This is the actual "tee" operation. */
public void write(byte[] x, int o, int l) {
parent.write(x, o, l); // "write once;
super.write(x, o, l); // write somewhere else."
}
/** Close both streams. */
public void close() {
parent.close();
super.close();
}
/** Flush both streams. */
public void flush() {
parent.flush();
super.flush();
}
}
// END main