|
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.BufferedWriter; |
|
38 |
| import java.io.IOException; |
|
39 |
| import java.util.WeakHashMap; |
|
40 |
| |
|
41 |
| import edu.rice.cs.plt.collect.TotalMap; |
|
42 |
| import edu.rice.cs.plt.concurrent.LockMap; |
|
43 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
44 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
45 |
| import edu.rice.cs.plt.lambda.WrappedException; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public abstract class IndentedTextLogSink extends TextLogSink { |
|
52 |
| |
|
53 |
| private static final String HANGING_INDENT = " "; |
|
54 |
| |
|
55 |
| private static final Lambda<Long, Indenter> MAKE_INDENTER = new Lambda<Long, Indenter>() { |
|
56 |
0
| public Indenter value(Long l) { return new Indenter(); }
|
|
57 |
| }; |
|
58 |
| |
|
59 |
| private final TotalMap<Long, Indenter> _indenters; |
|
60 |
| private final LockMap<BufferedWriter> _locks; |
|
61 |
| private final WeakHashMap<BufferedWriter, Long> _lastThreads; |
|
62 |
| |
|
63 |
14
| protected IndentedTextLogSink() {
|
|
64 |
14
| super();
|
|
65 |
14
| _indenters = new TotalMap<Long, Indenter>(MAKE_INDENTER, true);
|
|
66 |
14
| _locks = new LockMap<BufferedWriter>(5);
|
|
67 |
14
| _lastThreads = new WeakHashMap<BufferedWriter, Long>(5);
|
|
68 |
| } |
|
69 |
| |
|
70 |
0
| protected IndentedTextLogSink(int idealLineWidth) {
|
|
71 |
0
| super(idealLineWidth);
|
|
72 |
0
| _indenters = new TotalMap<Long, Indenter>(MAKE_INDENTER, true);
|
|
73 |
0
| _locks = new LockMap<BufferedWriter>(5);
|
|
74 |
0
| _lastThreads = new WeakHashMap<BufferedWriter, Long>(5);
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| protected abstract BufferedWriter writer(Message m); |
|
79 |
| |
|
80 |
0
| protected void write(Message m, SizedIterable<String> text) {
|
|
81 |
0
| BufferedWriter w = writer(m);
|
|
82 |
0
| doWrite(w, m, text);
|
|
83 |
| } |
|
84 |
| |
|
85 |
0
| protected void writeStart(StartMessage m, SizedIterable<String> text) {
|
|
86 |
0
| write(m, text);
|
|
87 |
0
| synchronized(_indenters) { _indenters.get(m.thread().getId()).push(); }
|
|
88 |
| } |
|
89 |
| |
|
90 |
0
| protected void writeEnd(EndMessage m, SizedIterable<String> text) {
|
|
91 |
0
| synchronized(_indenters) { _indenters.get(m.thread().getId()).pop(); }
|
|
92 |
0
| write(m, text);
|
|
93 |
| } |
|
94 |
| |
|
95 |
0
| private void doWrite(BufferedWriter w, Message m, SizedIterable<String> text) {
|
|
96 |
0
| Long threadId = m.thread().getId();
|
|
97 |
0
| String indentString;
|
|
98 |
0
| synchronized(_indenters) { indentString = _indenters.get(threadId).indentString(); }
|
|
99 |
0
| Runnable unlock = _locks.lock(w);
|
|
100 |
0
| try {
|
|
101 |
0
| if (_lastThreads.containsKey(w)) {
|
|
102 |
0
| Long prevId = _lastThreads.get(w);
|
|
103 |
0
| if (!prevId.equals(threadId)) { w.newLine(); _lastThreads.put(w, threadId); }
|
|
104 |
| } |
|
105 |
0
| else { _lastThreads.put(w, threadId); }
|
|
106 |
0
| w.write(indentString);
|
|
107 |
0
| w.write("[" + formatLocation(m.caller()) + " - " + formatThread(m.thread()) +" - " +
|
|
108 |
| formatTime(m.time()) + "]"); |
|
109 |
0
| w.newLine();
|
|
110 |
0
| for (String s : text) {
|
|
111 |
0
| w.write(indentString);
|
|
112 |
0
| w.write(HANGING_INDENT);
|
|
113 |
0
| w.write(s);
|
|
114 |
0
| w.newLine();
|
|
115 |
| } |
|
116 |
0
| w.flush();
|
|
117 |
| } |
|
118 |
| catch (IOException e) { |
|
119 |
| |
|
120 |
| |
|
121 |
0
| throw new WrappedException(e);
|
|
122 |
| } |
|
123 |
0
| finally { unlock.run(); }
|
|
124 |
| } |
|
125 |
| |
|
126 |
| } |