/* * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util.logging; /** * Handler that buffers requests in a circular buffer in memory. *
* Normally this Handler simply stores incoming LogRecords * into its memory buffer and discards earlier records. This buffering * is very cheap and avoids formatting costs. On certain trigger * conditions, the MemoryHandler will push out its current buffer * contents to a target Handler, which will typically publish * them to the outside world. *
* There are three main models for triggering a push of the buffer: *
* Configuration: * By default each MemoryHandler is initialized using the following * LogManager configuration properties. If properties are not defined * (or have invalid values) then the specified default values are used. * If no default value is defined then a RuntimeException is thrown. *
* The MemoryHandler is configured based on LogManager * properties (or their default values) except that the given pushLevel * argument and buffer size argument are used. * * @param target the Handler to which to publish output. * @param size the number of log records to buffer (must be greater than zero) * @param pushLevel message level to push on * * @throws IllegalArgumentException if size is <= 0 */ public MemoryHandler(Handler target, int size, Level pushLevel) { if (target == null || pushLevel == null) { throw new NullPointerException(); } if (size <= 0) { throw new IllegalArgumentException(); } sealed = false; configure(); sealed = true; this.target = target; this.pushLevel = pushLevel; this.size = size; init(); } /** * Store a LogRecord in an internal buffer. *
* If there is a Filter, its isLoggable * method is called to check if the given log record is loggable. * If not we return. Otherwise the given record is copied into * an internal circular buffer. Then the record's level property is * compared with the pushLevel. If the given level is * greater than or equal to the pushLevel then push * is called to write all buffered records to the target output * Handler. * * @param record description of the log event. A null record is * silently ignored and is not published */ public synchronized void publish(LogRecord record) { if (!isLoggable(record)) { return; } int ix = (start+count)%buffer.length; buffer[ix] = record; if (count < buffer.length) { count++; } else { start++; start %= buffer.length; } if (record.getLevel().intValue() >= pushLevel.intValue()) { push(); } } /** * Push any buffered output to the target Handler. *
* The buffer is then cleared. */ public synchronized void push() { for (int i = 0; i < count; i++) { int ix = (start+i)%buffer.length; LogRecord record = buffer[ix]; target.publish(record); } // Empty the buffer. start = 0; count = 0; } /** * Causes a flush on the target Handler. *
* Note that the current contents of the MemoryHandler * buffer are not written out. That requires a "push". */ public void flush() { target.flush(); } /** * Close the Handler and free all associated resources. * This will also close the target Handler. * * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public void close() throws SecurityException { target.close(); setLevel(Level.OFF); } /** * Set the pushLevel. After a LogRecord is copied * into our internal buffer, if its level is greater than or equal to * the pushLevel, then push will be called. * * @param newLevel the new value of the pushLevel * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public void setPushLevel(Level newLevel) throws SecurityException { if (newLevel == null) { throw new NullPointerException(); } LogManager manager = LogManager.getLogManager(); checkPermission(); pushLevel = newLevel; } /** * Get the pushLevel. * * @return the value of the pushLevel */ public synchronized Level getPushLevel() { return pushLevel; } /** * Check if this Handler would actually log a given * LogRecord into its internal buffer. *
* This method checks if the LogRecord has an appropriate level and * whether it satisfies any Filter. However it does not * check whether the LogRecord would result in a "push" of the * buffer contents. It will return false if the LogRecord is null. *
* @param record a LogRecord * @return true if the LogRecord would be logged. * */ public boolean isLoggable(LogRecord record) { return super.isLoggable(record); } }