/* MemoryHandler.java -- a class for buffering log messages in a memory buffer Copyright (C) 2002 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.util.logging; /** * A MemoryHandler maintains a circular buffer of * log records. * *

Configuration: Values of the subsequent * LogManager properties are taken into consideration * when a MemoryHandler is initialized. * If a property is not defined, or if it has an invalid * value, a default is taken without an exception being thrown. * *

* * @author Sascha Brawer (brawer@acm.org) */ public class MemoryHandler extends Handler { private final LogRecord[] buffer; private int position; private int numPushed; private Level pushLevel; private final Handler target; /** * Constructs a MemoryHandler for keeping a circular * buffer of LogRecords; the initial configuration is determined by * the LogManager properties described above. * * @throws java.lang.IllegalArgumentException if the * java.util.logging.MemoryHandler.target * LogManager property is not defined, or does not specify * the name of a subclass of {@link Handler}. *

Specification Note: The documentation * of the Sun reference implementation does not specify * that this exception is thrown, but this would be * the analogous behaviour to other handlers. * FIXME: Verify this, and file a bug report with Sun * asking to clarify their documentation. */ public MemoryHandler() { this(null, LogManager.getIntProperty("java.util.logging.MemoryHandler.size", 1000), Level.INFO); } public MemoryHandler(Handler target, int size, Level pushLevel) { buffer = new LogRecord[size]; this.pushLevel = pushLevel; this.target = target; } /** * Stores a LogRecord in a fixed-size circular buffer, * provided the record passes all tests for being loggable. If the * buffer is full, the oldest record will be discarded. * *

If the record has a severity level which is greater than or * equal to the pushLevel of this * MemoryHandler, the {@link #push()} method will be * invoked for pushing the buffer contents to the target * Handler. * *

Most applications do not need to call this method directly. * Instead, they will use use a {@link Logger}, which will create * LogRecords and distribute them to registered handlers. * * @param record the log event to be published. */ public void publish(LogRecord record) { if (!isLoggable(record)) return; buffer[position] = record; position = (position + 1) % buffer.length; numPushed = numPushed + 1; if (record.getLevel().intValue() >= pushLevel.intValue()) push(); } /** * Pushes the contents of the memory buffer to the target * Handler and clears the buffer. Note that * the target handler will discard those records that do * not satisfy its own severity level threshold, or that are * not considered loggable by an installed {@link Filter}. * *

In case of an I/O failure, the {@link ErrorManager} of the * target Handler will be notified, but the caller of * this method will not receive an exception. */ public void push() { int i; if (numPushed < buffer.length) { for (i = 0; i < position; i++) target.publish(buffer[i]); } else { for (i = position; i < buffer.length; i++) target.publish(buffer[i]); for (i = 0; i < position; i++) target.publish(buffer[i]); } numPushed = 0; position = 0; } /** * Forces any data that may have been buffered by the target * Handler to the underlying output device, but * does not push the contents of the circular memory * buffer to the target handler. * *

In case of an I/O failure, the {@link ErrorManager} of the * target Handler will be notified, but the caller of * this method will not receive an exception. * * @see #push() */ public void flush() { target.flush(); } /** * Closes this MemoryHandler and its associated target * handler, discarding the contents of the memory buffer. However, * any data that may have been buffered by the target * Handler is forced to the underlying output device. * *

As soon as close has been called, * a Handler should not be used anymore. Attempts * to publish log records, to flush buffers, or to modify the * Handler in any other way may throw runtime * exceptions after calling close.

* *

In case of an I/O failure, the ErrorManager of * the associated target Handler will be informed, but * the caller of this method will not receive an exception.

* * @throws SecurityException if a security manager exists and * the caller is not granted the permission to control * the logging infrastructure. * * @see #push() */ public void close() throws SecurityException { push(); /* This will check for the SecurityPermission. If the * current security context does not grant this permission, * push() has been executed, but this does not impose a * security risk. */ target.close(); } /** * Returns the push level threshold for this Handler. * When a record is published whose severity level is greater * than or equal to the pushLevel of this * MemoryHandler, the {@link #push()} method will be * invoked for pushing the buffer contents to the target * Handler. * * @return the push level threshold for automatic pushing. */ public Level getPushLevel() { return pushLevel; } /** * Sets the push level threshold for this Handler. * When a record is published whose severity level is greater * than or equal to the pushLevel of this * MemoryHandler, the {@link #push()} method will be * invoked for pushing the buffer contents to the target * Handler. * @param pushLevel the push level threshold for automatic * pushing. * * @exception SecurityException if a security manager exists and * the caller is not granted the permission to control * the logging infrastructure. * * @exception NullPointerException if pushLevel is * null. */ public void setPushLevel(Level pushLevel) { LogManager.getLogManager().checkAccess(); /* Throws a NullPointerException if pushLevel is null. */ pushLevel.getClass(); this.pushLevel = pushLevel; } }