View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.read;
11  
12  import ch.qos.logback.core.AppenderBase;
13  import ch.qos.logback.core.helpers.CyclicBuffer;
14  
15  /**
16   * CyclicBufferAppender stores events in a cyclic buffer of user-specified size. As the 
17   * name suggests, if the size of the buffer is N, only the latest N events are available.
18   * 
19   * 
20   * @author Ceki Gulcu
21   */
22  public class CyclicBufferAppender<E> extends AppenderBase<E> {
23  
24    CyclicBuffer<E> cb;
25    int maxSize = 512;
26  
27    public void start() {
28      cb = new CyclicBuffer<E>(maxSize);
29      super.start();
30    }
31  
32    public void stop() {
33      cb = null;
34      super.stop();
35    }
36  
37    @Override
38    protected void append(E eventObject) {
39      if (!isStarted()) {
40        return;
41      }
42      cb.add(eventObject);
43    }
44  
45    public int getLength() {
46      if (isStarted()) {
47        return cb.length();
48      } else {
49        return 0;
50      }
51    }
52  
53    public Object get(int i) {
54      if (isStarted()) {
55        return cb.get(i);
56      } else {
57        return null;
58      }
59    }
60  
61    /**
62     * Set the size of the cyclic buffer.
63     */
64    public int getMaxSize() {
65      return maxSize;
66    }
67  
68    public void setMaxSize(int maxSize) {
69      this.maxSize = maxSize;
70    }
71  
72  }