|
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 |
| package edu.rice.cs.plt.debug; |
|
36 |
| |
|
37 |
| import java.io.*; |
|
38 |
| import java.util.Date; |
|
39 |
| import edu.rice.cs.plt.io.IOUtil; |
|
40 |
| import edu.rice.cs.plt.lambda.LazyThunk; |
|
41 |
| import edu.rice.cs.plt.lambda.Thunk; |
|
42 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
43 |
| import edu.rice.cs.plt.text.TextUtil; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class FileLogSink extends IndentedTextLogSink { |
|
50 |
| |
|
51 |
| private final Thunk<BufferedWriter> _writer; |
|
52 |
| private volatile boolean _active; |
|
53 |
| |
|
54 |
4
| public FileLogSink(String filename) { this(new File(filename), null, true); }
|
|
55 |
| |
|
56 |
0
| public FileLogSink(File f) { this(f, null, true); }
|
|
57 |
| |
|
58 |
0
| public FileLogSink(File f, int idealLineWidth) { this(f, null, idealLineWidth, true); }
|
|
59 |
| |
|
60 |
0
| public FileLogSink(File f, boolean closeOnExit) { this(f, null, closeOnExit); }
|
|
61 |
| |
|
62 |
0
| public FileLogSink(File f, int idealLineWidth, boolean closeOnExit) {
|
|
63 |
0
| this(f, null, idealLineWidth, closeOnExit);
|
|
64 |
| } |
|
65 |
| |
|
66 |
0
| public FileLogSink(File f, String charset) { this(f, charset, true); }
|
|
67 |
| |
|
68 |
4
| public FileLogSink(File f, String charset, boolean closeOnExit) {
|
|
69 |
4
| super();
|
|
70 |
4
| _writer = initWriter(f, charset);
|
|
71 |
4
| _active = false;
|
|
72 |
4
| if (closeOnExit) { IOUtil.closeOnExit(this); }
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public FileLogSink(File f, String charset, int idealLineWidth) {
|
|
76 |
0
| this(f, charset, idealLineWidth, true);
|
|
77 |
| } |
|
78 |
| |
|
79 |
0
| public FileLogSink(File f, String charset, int idealLineWidth, boolean closeOnExit) {
|
|
80 |
0
| super(idealLineWidth);
|
|
81 |
0
| _writer = initWriter(f, charset);
|
|
82 |
0
| _active = false;
|
|
83 |
0
| if (closeOnExit) { IOUtil.closeOnExit(this); }
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
4
| private Thunk<BufferedWriter> initWriter(final File f, final String charset) {
|
|
88 |
4
| return LazyThunk.make(new Thunk<BufferedWriter>() {
|
|
89 |
0
| public BufferedWriter value() {
|
|
90 |
0
| try {
|
|
91 |
0
| OutputStream out = new FileOutputStream(f, true);
|
|
92 |
0
| try {
|
|
93 |
0
| Writer w = (charset == null) ? new OutputStreamWriter(out) : new OutputStreamWriter(out, charset);
|
|
94 |
0
| BufferedWriter result = new BufferedWriter(w);
|
|
95 |
0
| IOUtil.closeOnExit(result);
|
|
96 |
0
| String stars = TextUtil.repeat('*', 40);
|
|
97 |
0
| result.write(stars);
|
|
98 |
0
| result.newLine();
|
|
99 |
0
| result.write("Opened log file " + formatTime(new Date()));
|
|
100 |
0
| result.newLine();
|
|
101 |
0
| result.write(stars);
|
|
102 |
0
| result.newLine();
|
|
103 |
0
| result.newLine();
|
|
104 |
0
| result.flush();
|
|
105 |
0
| _active = true;
|
|
106 |
0
| return result;
|
|
107 |
| } |
|
108 |
0
| finally { if (!_active) { out.close(); } }
|
|
109 |
| } |
|
110 |
0
| catch (IOException e) { throw new WrappedException(e); }
|
|
111 |
| } |
|
112 |
| }); |
|
113 |
| } |
|
114 |
| |
|
115 |
0
| @Override protected BufferedWriter writer(Message m) { return _writer.value(); }
|
|
116 |
| |
|
117 |
| |
|
118 |
4
| public void close() throws IOException {
|
|
119 |
0
| if (_active) { _writer.value().close(); _active = false; }
|
|
120 |
| } |
|
121 |
| |
|
122 |
| } |