|
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 |
| package com.gargoylesoftware.base.trace; |
|
39 |
| |
|
40 |
| import com.gargoylesoftware.base.util.DetailedNullPointerException; |
|
41 |
| import com.gargoylesoftware.base.util.StringUtil; |
|
42 |
| import java.io.PrintStream; |
|
43 |
| import java.lang.reflect.InvocationTargetException; |
|
44 |
| import java.lang.reflect.Method; |
|
45 |
| import java.text.Format; |
|
46 |
| import java.text.SimpleDateFormat; |
|
47 |
| import java.util.Date; |
|
48 |
| import java.util.Iterator; |
|
49 |
| import java.util.Set; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public class TraceItemDispatcher implements Runnable { |
|
59 |
| |
|
60 |
| |
|
61 |
| private static final Format TIMESTAMP_FORMAT = new SimpleDateFormat("HH:mm"); |
|
62 |
| |
|
63 |
| private static final int BUFFER_ENABLED = 1; |
|
64 |
| private static final int BUFFER_SHUTTING_DOWN = 2; |
|
65 |
| private static final int BUFFER_DISABLED = 3; |
|
66 |
| |
|
67 |
| private static int bufferStatus_ = BUFFER_DISABLED; |
|
68 |
| |
|
69 |
| private final TraceItemQueue traceQueue_ = new TraceItemQueue(); |
|
70 |
| |
|
71 |
| |
|
72 |
| private TraceItemQueue cacheTraceItemQueue_ = new TraceItemQueue(); |
|
73 |
| private int cacheMaxSize_ = 50; |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
2
| public TraceItemDispatcher() {
|
|
79 |
2
| new Thread(this, "TraceItemDispatcher Thread").start();
|
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
2
| final Runtime runtime = Runtime.getRuntime();
|
|
88 |
2
| try {
|
|
89 |
2
| final Method method = runtime.getClass().getMethod( "addShutdownHook",
|
|
90 |
| new Class[] { Thread.class } ); |
|
91 |
2
| final Thread thread = new Thread() {
|
|
92 |
2
| public void run() {
|
|
93 |
2
| Trace.getController().setBufferingEnabled(false);
|
|
94 |
| } |
|
95 |
| }; |
|
96 |
2
| method.invoke( runtime, new Object[] { thread } );
|
|
97 |
| } |
|
98 |
| catch( NoSuchMethodException e ) { |
|
99 |
| |
|
100 |
| } |
|
101 |
| catch( final IllegalAccessException e ) { |
|
102 |
| |
|
103 |
0
| e.printStackTrace();
|
|
104 |
| } |
|
105 |
| catch( final InvocationTargetException e ) { |
|
106 |
| |
|
107 |
0
| e.getTargetException().printStackTrace();
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
2
| public void run() {
|
|
115 |
2
| TraceItem item;
|
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
2
| bufferStatus_ = BUFFER_ENABLED;
|
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
2
| try {
|
|
125 |
2
| while(true) {
|
|
126 |
8
| try {
|
|
127 |
8
| item = traceQueue_.pop();
|
|
128 |
8
| if( item == null ) {
|
|
129 |
5
| Thread.sleep(500);
|
|
130 |
| } |
|
131 |
| else { |
|
132 |
3
| dumpTraceElement(item);
|
|
133 |
| } |
|
134 |
| } |
|
135 |
| catch( final Exception e ) { |
|
136 |
0
| System.out.print("Exception when printing debug information e=");
|
|
137 |
0
| e.printStackTrace();
|
|
138 |
| } |
|
139 |
| } |
|
140 |
| } |
|
141 |
| catch( final Throwable t ) { |
|
142 |
0
| System.out.print("Exception when printing debug information e=");
|
|
143 |
0
| t.printStackTrace();
|
|
144 |
| } |
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
4
| private void dumpTraceElement( final TraceItem item ) {
|
|
152 |
4
| assertNotNull("item", item);
|
|
153 |
| |
|
154 |
4
| final TraceChannel channel = item.getChannel();
|
|
155 |
4
| if( channel != null && channel.isEnabled() && item.containsText() ) {
|
|
156 |
3
| final Set traceWriters = channel.getTraceWriters();
|
|
157 |
3
| synchronized( traceWriters ) {
|
|
158 |
3
| final Iterator iterator = traceWriters.iterator();
|
|
159 |
3
| if( iterator.hasNext() == false ) {
|
|
160 |
| |
|
161 |
0
| defaultTraceWriter(item);
|
|
162 |
| } |
|
163 |
| else { |
|
164 |
3
| while( iterator.hasNext() ) {
|
|
165 |
3
| ((TraceWriter)iterator.next()).write(item);
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| } |
|
169 |
| } |
|
170 |
| |
|
171 |
| |
|
172 |
4
| final Object lock = item.getLock();
|
|
173 |
4
| if( lock != null ) {
|
|
174 |
1
| synchronized(lock) {
|
|
175 |
1
| lock.notify();
|
|
176 |
| } |
|
177 |
1
| return;
|
|
178 |
| } |
|
179 |
| |
|
180 |
3
| disposeTraceItem(item);
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
0
| private static void defaultTraceWriter( final TraceItem item ) {
|
|
188 |
| |
|
189 |
0
| final PrintStream outStream
|
|
190 |
| = Trace.getController().getRealSystemOut(); |
|
191 |
0
| final StringBuffer prefixBuffer = new StringBuffer();
|
|
192 |
| |
|
193 |
0
| prefixBuffer.append("[");
|
|
194 |
0
| final Date timestamp = item.getTime();
|
|
195 |
0
| if( timestamp != null ) {
|
|
196 |
0
| prefixBuffer.append( TIMESTAMP_FORMAT.format(timestamp) );
|
|
197 |
0
| prefixBuffer.append(" ");
|
|
198 |
| } |
|
199 |
| |
|
200 |
0
| final String threadName = item.getThreadName();
|
|
201 |
0
| if( threadName != null ) {
|
|
202 |
0
| prefixBuffer.append( threadName );
|
|
203 |
| } |
|
204 |
| |
|
205 |
0
| prefixBuffer.append("] ");
|
|
206 |
| |
|
207 |
0
| final String prefix = prefixBuffer.toString();
|
|
208 |
0
| final String message = item.getMessage();
|
|
209 |
| |
|
210 |
0
| if( message != null ) {
|
|
211 |
0
| outStream.print(prefix);
|
|
212 |
0
| outStream.print( StringUtil.expandTabs(message,3) );
|
|
213 |
| } |
|
214 |
| |
|
215 |
0
| final Throwable throwable = item.getThrowable();
|
|
216 |
| |
|
217 |
0
| if( throwable != null ) {
|
|
218 |
0
| int i;
|
|
219 |
0
| final String strings[] = Trace.throwableToStringArray(throwable);
|
|
220 |
| |
|
221 |
0
| outStream.print(prefix);
|
|
222 |
0
| outStream.println(strings[0]);
|
|
223 |
| |
|
224 |
0
| final String blanks = StringUtil.nCopies(prefix.length(), ' ');
|
|
225 |
0
| for( i=1; i<strings.length; i++ ) {
|
|
226 |
0
| outStream.print(blanks);
|
|
227 |
0
| outStream.println( StringUtil.expandTabs(strings[i],3) );
|
|
228 |
| } |
|
229 |
| } |
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
0
| public TraceItemQueue getTraceItemQueue() {
|
|
237 |
0
| return traceQueue_;
|
|
238 |
| } |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
4
| public void dispatch( final TraceItem item ) {
|
|
245 |
4
| item.setThread( Thread.currentThread() );
|
|
246 |
4
| item.setTime( new Date() );
|
|
247 |
| |
|
248 |
4
| switch( bufferStatus_ ) {
|
|
249 |
3
| case BUFFER_ENABLED:
|
|
250 |
3
| traceQueue_.push(item);
|
|
251 |
3
| break;
|
|
252 |
| |
|
253 |
0
| case BUFFER_SHUTTING_DOWN:
|
|
254 |
0
| flush();
|
|
255 |
0
| dumpTraceElement(item);
|
|
256 |
0
| break;
|
|
257 |
| |
|
258 |
1
| case BUFFER_DISABLED:
|
|
259 |
1
| dumpTraceElement(item);
|
|
260 |
1
| break;
|
|
261 |
| |
|
262 |
0
| default:
|
|
263 |
0
| throw new IllegalStateException(
|
|
264 |
| "Unexpected value: bufferStatus_=" |
|
265 |
| + bufferStatus_); |
|
266 |
| } |
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
3
| private synchronized void waitForQueueToEmpty() {
|
|
274 |
3
| while( traceQueue_.size() != 0 ) {
|
|
275 |
5
| try {
|
|
276 |
5
| Thread.sleep(100);
|
|
277 |
| } |
|
278 |
| catch( final InterruptedException e ) { |
|
279 |
0
| return;
|
|
280 |
| } |
|
281 |
| } |
|
282 |
| } |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
5
| public synchronized void setBufferingEnabled( final boolean enabled ) {
|
|
290 |
| |
|
291 |
5
| if( enabled ) {
|
|
292 |
2
| if( bufferStatus_ == BUFFER_SHUTTING_DOWN ) {
|
|
293 |
| |
|
294 |
0
| flush();
|
|
295 |
| } |
|
296 |
| |
|
297 |
2
| if( bufferStatus_ == BUFFER_DISABLED ) {
|
|
298 |
1
| bufferStatus_ = BUFFER_ENABLED;
|
|
299 |
| } |
|
300 |
| } |
|
301 |
| else { |
|
302 |
3
| if( bufferStatus_ == BUFFER_ENABLED ) {
|
|
303 |
3
| bufferStatus_ = BUFFER_SHUTTING_DOWN;
|
|
304 |
3
| flush();
|
|
305 |
3
| bufferStatus_ = BUFFER_DISABLED;
|
|
306 |
| } |
|
307 |
| } |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
0
| public boolean isBufferingEnabled() {
|
|
315 |
0
| return bufferStatus_ == BUFFER_ENABLED;
|
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
4
| public void flush() {
|
|
322 |
| |
|
323 |
4
| switch( bufferStatus_ ) {
|
|
324 |
1
| case BUFFER_ENABLED:
|
|
325 |
1
| final TraceItem item = getNewTraceItem();
|
|
326 |
1
| final Object lock = new Object();
|
|
327 |
1
| item.setLock( lock );
|
|
328 |
1
| try {
|
|
329 |
1
| synchronized(lock) {
|
|
330 |
1
| dispatch( item );
|
|
331 |
1
| lock.wait();
|
|
332 |
| } |
|
333 |
| } |
|
334 |
| catch( final InterruptedException e ) { |
|
335 |
| |
|
336 |
| } |
|
337 |
1
| break;
|
|
338 |
| |
|
339 |
3
| case BUFFER_SHUTTING_DOWN:
|
|
340 |
3
| waitForQueueToEmpty();
|
|
341 |
3
| break;
|
|
342 |
| |
|
343 |
0
| case BUFFER_DISABLED:
|
|
344 |
0
| return;
|
|
345 |
| |
|
346 |
0
| default:
|
|
347 |
0
| throw new IllegalStateException(
|
|
348 |
| "Unexpected value: bufferStatus_=" |
|
349 |
| + bufferStatus_); |
|
350 |
| } |
|
351 |
| } |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
4
| public TraceItem getNewTraceItem() {
|
|
358 |
4
| TraceItem item = cacheTraceItemQueue_.pop();
|
|
359 |
4
| if( item == null ) {
|
|
360 |
2
| item = new TraceItem();
|
|
361 |
| } |
|
362 |
| |
|
363 |
4
| return item;
|
|
364 |
| } |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
3
| public void disposeTraceItem( final TraceItem item ) {
|
|
371 |
3
| if( cacheTraceItemQueue_.size() < cacheMaxSize_ ) {
|
|
372 |
3
| item.clear();
|
|
373 |
3
| cacheTraceItemQueue_.push(item);
|
|
374 |
| } |
|
375 |
| } |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
4
| protected final void assertNotNull( final String fieldName, final Object fieldValue )
|
|
386 |
| throws DetailedNullPointerException { |
|
387 |
| |
|
388 |
4
| if( fieldValue == null ) {
|
|
389 |
0
| throw new DetailedNullPointerException(fieldName);
|
|
390 |
| } |
|
391 |
| } |
|
392 |
| } |
|
393 |
| |