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