KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > RingBufferLogHandler


1 package hudson.util;
2
3 import java.util.AbstractList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.logging.Handler JavaDoc;
6 import java.util.logging.LogRecord JavaDoc;
7
8 /**
9  * Log {@link Handler} that stores the log records into a ring buffer.
10  *
11  * @author Kohsuke Kawaguchi
12  */

13 public class RingBufferLogHandler extends Handler JavaDoc {
14
15     private int start = 0;
16     private final LogRecord JavaDoc[] records;
17     private int size = 0;
18
19     public RingBufferLogHandler() {
20         this(256);
21     }
22
23     public RingBufferLogHandler(int ringSize) {
24         records = new LogRecord JavaDoc[ringSize];
25     }
26
27     public synchronized void publish(LogRecord JavaDoc record) {
28         int len = records.length;
29         records[(start+size)%len]=record;
30         if(size==len) {
31             start++;
32         } else {
33             size++;
34         }
35     }
36
37     /**
38      * Returns the list view of {@link LogRecord}s in the ring buffer.
39      *
40      * <p>
41      * New records are always placed early in the list.
42      */

43     public List JavaDoc<LogRecord JavaDoc> getView() {
44         return new AbstractList JavaDoc<LogRecord JavaDoc>() {
45             public LogRecord JavaDoc get(int index) {
46                 // flip the order
47
return records[(start+(size-(index+1)))%records.length];
48             }
49
50             public int size() {
51                 return size;
52             }
53         };
54     }
55
56     // noop
57
public void flush() {}
58     public void close() throws SecurityException JavaDoc {}
59 }
60
Popular Tags