001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.read; 015 016import ch.qos.logback.core.AppenderBase; 017import ch.qos.logback.core.helpers.CyclicBuffer; 018 019/** 020 * CyclicBufferAppender stores events in a cyclic buffer of user-specified size. 021 * As the name suggests, if the size of the buffer is N, only the latest N 022 * events are available. 023 * 024 * 025 * @author Ceki Gulcu 026 */ 027public class CyclicBufferAppender<E> extends AppenderBase<E> { 028 029 CyclicBuffer<E> cb; 030 int maxSize = 512; 031 032 public void start() { 033 cb = new CyclicBuffer<E>(maxSize); 034 super.start(); 035 } 036 037 public void stop() { 038 cb = null; 039 super.stop(); 040 } 041 042 @Override 043 protected void append(E eventObject) { 044 if (!isStarted()) { 045 return; 046 } 047 cb.add(eventObject); 048 } 049 050 public int getLength() { 051 if (isStarted()) { 052 return cb.length(); 053 } else { 054 return 0; 055 } 056 } 057 058 public E get(int i) { 059 if (isStarted()) { 060 return cb.get(i); 061 } else { 062 return null; 063 } 064 } 065 066 public void reset() { 067 cb.clear(); 068 } 069 070 /** 071 * Set the size of the cyclic buffer. 072 */ 073 public int getMaxSize() { 074 return maxSize; 075 } 076 077 public void setMaxSize(int maxSize) { 078 this.maxSize = maxSize; 079 } 080 081}