001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2006-04-12 16:44:20 +0200 (Mi, 12 Apr 2006) $
025: * $Author: til132 $
026: * $Revision: 209 $
027: */
028: package net.sf.regain.util.io;
029:
030: import java.util.Iterator;
031: import java.util.LinkedList;
032:
033: import net.sf.regain.RegainException;
034:
035: import org.apache.log4j.AppenderSkeleton;
036: import org.apache.log4j.spi.LoggingEvent;
037:
038: /**
039: * A Log4j appender that keeps a number of logging events in memory. These
040: * events may be layouted on demand.
041: *
042: * @author Til Schneider, www.murfman.de
043: */
044: public class MemoryAppender extends AppenderSkeleton {
045:
046: /** Holds the cached log messages. */
047: private LinkedList mCache;
048:
049: /** The maximum cache size. */
050: private int mMaxCacheSize;
051:
052: /**
053: * Creates a new MemoryAppender instance.
054: */
055: public MemoryAppender() {
056: mCache = new LinkedList();
057: mMaxCacheSize = 30;
058: }
059:
060: /**
061: * Sets the maximum cache size.
062: *
063: * @param maxCacheSize the maximum cache size.
064: */
065: public void setMaxCacheSize(int maxCacheSize) {
066: mMaxCacheSize = maxCacheSize;
067: }
068:
069: /**
070: * Prints the cached logging events to a page printer.
071: *
072: * @param printer The page printer to print to.
073: * @throws RegainException If printing failed.
074: */
075: public void printLog(Printer printer) throws RegainException {
076: synchronized (mCache) {
077: Iterator iter = mCache.iterator();
078: while (iter.hasNext()) {
079: Object[] itemArr = (Object[]) iter.next();
080: LoggingEvent evt = (LoggingEvent) itemArr[0];
081: String formattedEvt = (String) itemArr[1];
082:
083: if (formattedEvt == null) {
084: formattedEvt = getLayout().format(evt);
085: itemArr[1] = formattedEvt;
086: }
087:
088: printer.print(formattedEvt);
089: }
090: }
091: };
092:
093: // overridden
094: protected void append(LoggingEvent evt) {
095: synchronized (mCache) {
096: mCache.add(new Object[] { evt, null });
097:
098: if (mCache.size() > mMaxCacheSize) {
099: mCache.removeFirst();
100: }
101: }
102: }
103:
104: // overridden
105: public void close() {
106: }
107:
108: // overridden
109: public boolean requiresLayout() {
110: return true;
111: }
112:
113: }
|