-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStdOutStdErrTree
90 lines (73 loc) · 2.41 KB
/
StdOutStdErrTree
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
/*
* **********************************************
* San Francisco State University
* CSC 220 - Data Structures
* File Name: StdOutStdErrTree.java
* Author: Duc Ta
* Author: Bryan Khor
* **********************************************
*/
package CSC220ASMT2;
import java.io.*;
public class StdOutStdErrTree extends OutputStream {
//
// Instance Data Fields
//
private String stdOutFilePath;
private String stdOutErrPath;
private OutputStream[] streamsToConsoleToFile;
//
// Constructors
//
public StdOutStdErrTree() {
this.stdOutFilePath = Config.getDefaultStdOutFilePath();
this.stdOutErrPath = Config.getDefaultStdErrFilePath();
}
public StdOutStdErrTree(PrintStream printStream, FileOutputStream fileOutputStream) {
this.streamsToConsoleToFile = new OutputStream[2];
streamsToConsoleToFile[0] = printStream;
streamsToConsoleToFile[1] = fileOutputStream;
}
public StdOutStdErrTree(String stdOutFilePath, String stdErrFilePath) {
this.stdOutFilePath = stdOutFilePath;
this.stdOutErrPath = stdErrFilePath;
}
//
// Instance Methods
//
public void startLog() {
try {
FileOutputStream stdOutFile = new FileOutputStream(new File(this.stdOutFilePath));
FileOutputStream stdErrFile = new FileOutputStream(new File(this.stdOutErrPath));
StdOutStdErrTree allStdOut = new StdOutStdErrTree(System.out, stdOutFile);
StdOutStdErrTree allStdErr = new StdOutStdErrTree(System.err, stdErrFile);
PrintStream stdOut = new PrintStream(allStdOut);
PrintStream stdErr = new PrintStream(allStdErr);
System.setOut(stdOut);
System.setErr(stdErr);
} catch (FileNotFoundException exception) {
System.out.println(exception.getMessage());
}
}
public static void stopLog() {
//
// Add code which our own implementation needs
//
}
//
// Override
//
@Override
public void write(int b) throws IOException {
for (OutputStream out : this.streamsToConsoleToFile) {
out.write(b);
out.flush();
}
}
public Object getStdErrFilePath() {
return "./src/assignment02PartB/log/StandardErr.log";
}
public Object getStdOutFilePath() {
return "./src/assignment02PartB/log/StandardOut.log";
}
}