1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2011, 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. As the
21 * name suggests, if the size of the buffer is N, only the latest N events are available.
22 *
23 *
24 * @author Ceki Gulcu
25 */
26 public class CyclicBufferAppender<E> extends AppenderBase<E> {
27
28 CyclicBuffer<E> cb;
29 int maxSize = 512;
30
31 public void start() {
32 cb = new CyclicBuffer<E>(maxSize);
33 super.start();
34 }
35
36 public void stop() {
37 cb = null;
38 super.stop();
39 }
40
41 @Override
42 protected void append(E eventObject) {
43 if (!isStarted()) {
44 return;
45 }
46 cb.add(eventObject);
47 }
48
49 public int getLength() {
50 if (isStarted()) {
51 return cb.length();
52 } else {
53 return 0;
54 }
55 }
56
57 public Object get(int i) {
58 if (isStarted()) {
59 return cb.get(i);
60 } else {
61 return null;
62 }
63 }
64
65 /**
66 * Set the size of the cyclic buffer.
67 */
68 public int getMaxSize() {
69 return maxSize;
70 }
71
72 public void setMaxSize(int maxSize) {
73 this.maxSize = maxSize;
74 }
75
76 }