|
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.Closeable; |
|
38 |
| import java.io.IOException; |
|
39 |
| import java.io.Serializable; |
|
40 |
| import java.rmi.Remote; |
|
41 |
| import java.rmi.RemoteException; |
|
42 |
| |
|
43 |
| import edu.rice.cs.plt.concurrent.ConcurrentUtil; |
|
44 |
| import edu.rice.cs.plt.concurrent.JVMBuilder; |
|
45 |
| import edu.rice.cs.plt.io.IOUtil; |
|
46 |
| import edu.rice.cs.plt.lambda.LazyThunk; |
|
47 |
| import edu.rice.cs.plt.lambda.Thunk; |
|
48 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class RMILogSink implements LogSink, Closeable { |
|
57 |
| |
|
58 |
| private final Thunk<RemoteLogSink> _delegate; |
|
59 |
| private volatile boolean _active; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
3
| public RMILogSink(final Thunk<? extends LogSink> factory) { this(factory, JVMBuilder.DEFAULT, true); }
|
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
2
| public RMILogSink(final Thunk<? extends LogSink> factory, boolean closeOnExit) {
|
|
74 |
2
| this(factory, JVMBuilder.DEFAULT, closeOnExit);
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
0
| public RMILogSink(final Thunk<? extends LogSink> factory, JVMBuilder jvm) {
|
|
86 |
0
| this(factory, jvm, true);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
5
| public RMILogSink(final Thunk<? extends LogSink> factory, final JVMBuilder jvm, boolean closeOnExit) {
|
|
97 |
5
| _delegate = LazyThunk.make(new Thunk<RemoteLogSink>() {
|
|
98 |
3
| public RemoteLogSink value() {
|
|
99 |
3
| try {
|
|
100 |
3
| RemoteLogSink result = (RemoteLogSink) ConcurrentUtil.exportInProcess(new ServerFactory(factory), jvm);
|
|
101 |
3
| _active = true;
|
|
102 |
3
| return result;
|
|
103 |
| } |
|
104 |
0
| catch (Exception e) { throw new WrappedException(e); }
|
|
105 |
| } |
|
106 |
| }); |
|
107 |
5
| _active = false;
|
|
108 |
3
| if (closeOnExit) { IOUtil.closeOnExit(this); }
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
1
| public void close() throws IOException {
|
|
113 |
0
| if (_active) { _delegate.value().close(); _active = false; }
|
|
114 |
| } |
|
115 |
| |
|
116 |
6
| public void log(StandardMessage m) {
|
|
117 |
6
| try { _delegate.value().log(m.serializable()); }
|
|
118 |
0
| catch (RemoteException e) { throw new WrappedException(e); }
|
|
119 |
| } |
|
120 |
| |
|
121 |
3
| public void logStart(StartMessage m) {
|
|
122 |
3
| try { _delegate.value().logStart(m.serializable()); }
|
|
123 |
0
| catch (RemoteException e) { throw new WrappedException(e); }
|
|
124 |
| } |
|
125 |
| |
|
126 |
3
| public void logEnd(EndMessage m) {
|
|
127 |
3
| try { _delegate.value().logEnd(m.serializable()); }
|
|
128 |
0
| catch (RemoteException e) { throw new WrappedException(e); }
|
|
129 |
| } |
|
130 |
| |
|
131 |
3
| public void logError(ErrorMessage m) {
|
|
132 |
3
| try { _delegate.value().logError(m.serializable()); }
|
|
133 |
0
| catch (RemoteException e) { throw new WrappedException(e); }
|
|
134 |
| } |
|
135 |
| |
|
136 |
3
| public void logStack(StackMessage m) {
|
|
137 |
3
| try { _delegate.value().logStack(m.serializable()); }
|
|
138 |
0
| catch (RemoteException e) { throw new WrappedException(e); }
|
|
139 |
| } |
|
140 |
| |
|
141 |
| private static interface RemoteLogSink extends Remote { |
|
142 |
| public void log(StandardMessage m) throws RemoteException; |
|
143 |
| public void logStart(StartMessage m) throws RemoteException; |
|
144 |
| public void logEnd(EndMessage m) throws RemoteException; |
|
145 |
| public void logError(ErrorMessage m) throws RemoteException; |
|
146 |
| public void logStack(StackMessage m) throws RemoteException; |
|
147 |
| public void close() throws IOException; |
|
148 |
| } |
|
149 |
| |
|
150 |
| private static class ServerFactory implements Thunk<RemoteLogSink>, Serializable { |
|
151 |
| private final Thunk<? extends LogSink> _sinkFactory; |
|
152 |
3
| public ServerFactory(Thunk<? extends LogSink> sinkFactory) { _sinkFactory = sinkFactory; }
|
|
153 |
3
| public RemoteLogSink value() {
|
|
154 |
| |
|
155 |
3
| final LogSink sink = _sinkFactory.value();
|
|
156 |
3
| return new RemoteLogSink() {
|
|
157 |
3
| public void close() throws IOException { System.exit(0); }
|
|
158 |
6
| public void log(StandardMessage m) { sink.log(m); }
|
|
159 |
3
| public void logStart(StartMessage m) { sink.logStart(m); }
|
|
160 |
3
| public void logEnd(EndMessage m) { sink.logEnd(m); }
|
|
161 |
3
| public void logError(ErrorMessage m) { sink.logError(m); }
|
|
162 |
3
| public void logStack(StackMessage m) { sink.logStack(m); }
|
|
163 |
| }; |
|
164 |
| } |
|
165 |
| } |
|
166 |
| |
|
167 |
| } |