|
1 |
| package edu.rice.cs.plt.debug; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.util.Queue; |
|
5 |
| import java.util.concurrent.ConcurrentLinkedQueue; |
|
6 |
| |
|
7 |
| import edu.rice.cs.plt.concurrent.CompletionMonitor; |
|
8 |
| import edu.rice.cs.plt.lambda.LazyRunnable; |
|
9 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| public class AsynchronousLogSink implements LogSink { |
|
28 |
| |
|
29 |
| private final LogSink _delegate; |
|
30 |
| private final Runnable _startThread; |
|
31 |
| private final Queue<Message> _queue; |
|
32 |
| |
|
33 |
| private final CompletionMonitor _emptyNotifier; |
|
34 |
| private final CompletionMonitor _nonemptyNotifier; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
2
| public AsynchronousLogSink(LogSink delegate) { this(delegate, true); }
|
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
2
| public AsynchronousLogSink(LogSink delegate, final boolean flushOnShutdown) {
|
|
48 |
2
| _delegate = delegate;
|
|
49 |
2
| _startThread = new LazyRunnable(new Runnable() {
|
|
50 |
0
| public void run() {
|
|
51 |
0
| if (flushOnShutdown) {
|
|
52 |
0
| Runtime.getRuntime().addShutdownHook(new Thread() {
|
|
53 |
0
| public void run() {
|
|
54 |
0
| try { flush(); }
|
|
55 |
0
| catch (InterruptedException e) { throw new WrappedException(e); }
|
|
56 |
| } |
|
57 |
| }); |
|
58 |
| } |
|
59 |
0
| new DequeueThread().start();
|
|
60 |
| } |
|
61 |
| }); |
|
62 |
2
| _queue = new ConcurrentLinkedQueue<Message>();
|
|
63 |
2
| _emptyNotifier = new CompletionMonitor(true);
|
|
64 |
2
| _nonemptyNotifier = new CompletionMonitor(false);
|
|
65 |
| } |
|
66 |
| |
|
67 |
0
| public void close() throws IOException { _delegate.close(); }
|
|
68 |
| |
|
69 |
| |
|
70 |
0
| public void flush() throws InterruptedException {
|
|
71 |
0
| _emptyNotifier.ensureSignaled();
|
|
72 |
| } |
|
73 |
| |
|
74 |
0
| public void log(StandardMessage m) { handle(m); }
|
|
75 |
0
| public void logStart(StartMessage m) { handle(m); }
|
|
76 |
0
| public void logEnd(EndMessage m) { handle(m); }
|
|
77 |
0
| public void logError(ErrorMessage m) { handle(m); }
|
|
78 |
0
| public void logStack(StackMessage m) { handle(m); }
|
|
79 |
| |
|
80 |
0
| private void handle(Message m) {
|
|
81 |
0
| boolean wasEmpty = _queue.isEmpty();
|
|
82 |
0
| _queue.offer(m);
|
|
83 |
0
| if (wasEmpty) {
|
|
84 |
0
| synchronized (this) {
|
|
85 |
0
| if (!_queue.isEmpty()) {
|
|
86 |
0
| _emptyNotifier.reset();
|
|
87 |
0
| _nonemptyNotifier.signal();
|
|
88 |
| } |
|
89 |
| } |
|
90 |
0
| _startThread.run();
|
|
91 |
| } |
|
92 |
| } |
|
93 |
| |
|
94 |
| private class DequeueThread extends Thread { |
|
95 |
0
| public DequeueThread() { super(AsynchronousLogSink.this.toString()); setDaemon(true); }
|
|
96 |
| |
|
97 |
0
| public void run() {
|
|
98 |
0
| while (true) {
|
|
99 |
0
| _nonemptyNotifier.attemptEnsureSignaled();
|
|
100 |
0
| while (!_queue.isEmpty()) {
|
|
101 |
0
| _queue.remove().send(_delegate);
|
|
102 |
| } |
|
103 |
0
| synchronized (AsynchronousLogSink.this) {
|
|
104 |
0
| if (_queue.isEmpty()) {
|
|
105 |
0
| _nonemptyNotifier.reset();
|
|
106 |
0
| _emptyNotifier.signal();
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| } |
|
110 |
| } |
|
111 |
| |
|
112 |
| } |
|
113 |
| |
|
114 |
| } |