0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.logging;
0N/A
0N/A/**
0N/A * <tt>Handler</tt> that buffers requests in a circular buffer in memory.
0N/A * <p>
0N/A * Normally this <tt>Handler</tt> simply stores incoming <tt>LogRecords</tt>
0N/A * into its memory buffer and discards earlier records. This buffering
0N/A * is very cheap and avoids formatting costs. On certain trigger
0N/A * conditions, the <tt>MemoryHandler</tt> will push out its current buffer
0N/A * contents to a target <tt>Handler</tt>, which will typically publish
0N/A * them to the outside world.
0N/A * <p>
0N/A * There are three main models for triggering a push of the buffer:
0N/A * <ul>
0N/A * <li>
0N/A * An incoming <tt>LogRecord</tt> has a type that is greater than
0N/A * a pre-defined level, the <tt>pushLevel</tt>.
0N/A * <li>
0N/A * An external class calls the <tt>push</tt> method explicitly.
0N/A * <li>
0N/A * A subclass overrides the <tt>log</tt> method and scans each incoming
0N/A * <tt>LogRecord</tt> and calls <tt>push</tt> if a record matches some
0N/A * desired criteria.
0N/A * </ul>
0N/A * <p>
0N/A * <b>Configuration:</b>
0N/A * By default each <tt>MemoryHandler</tt> is initialized using the following
0N/A * LogManager configuration properties. If properties are not defined
0N/A * (or have invalid values) then the specified default values are used.
0N/A * If no default value is defined then a RuntimeException is thrown.
0N/A * <ul>
0N/A * <li> java.util.logging.MemoryHandler.level
0N/A * specifies the level for the <tt>Handler</tt>
0N/A * (defaults to <tt>Level.ALL</tt>).
0N/A * <li> java.util.logging.MemoryHandler.filter
0N/A * specifies the name of a <tt>Filter</tt> class to use
0N/A * (defaults to no <tt>Filter</tt>).
0N/A * <li> java.util.logging.MemoryHandler.size
0N/A * defines the buffer size (defaults to 1000).
0N/A * <li> java.util.logging.MemoryHandler.push
0N/A * defines the <tt>pushLevel</tt> (defaults to <tt>level.SEVERE</tt>).
0N/A * <li> java.util.logging.MemoryHandler.target
0N/A * specifies the name of the target <tt>Handler </tt> class.
0N/A * (no default).
0N/A * </ul>
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class MemoryHandler extends Handler {
0N/A private final static int DEFAULT_SIZE = 1000;
0N/A private Level pushLevel;
0N/A private int size;
0N/A private Handler target;
0N/A private LogRecord buffer[];
0N/A int start, count;
0N/A
0N/A // Private method to configure a ConsoleHandler from LogManager
0N/A // properties and/or default values as specified in the class
0N/A // javadoc.
0N/A private void configure() {
0N/A LogManager manager = LogManager.getLogManager();
0N/A String cname = getClass().getName();
0N/A
0N/A pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
0N/A size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
0N/A if (size <= 0) {
0N/A size = DEFAULT_SIZE;
0N/A }
0N/A setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
0N/A setFilter(manager.getFilterProperty(cname +".filter", null));
0N/A setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
0N/A }
0N/A
0N/A /**
0N/A * Create a <tt>MemoryHandler</tt> and configure it based on
0N/A * <tt>LogManager</tt> configuration properties.
0N/A */
0N/A public MemoryHandler() {
0N/A sealed = false;
0N/A configure();
0N/A sealed = true;
0N/A
0N/A String name = "???";
0N/A try {
0N/A LogManager manager = LogManager.getLogManager();
0N/A name = manager.getProperty("java.util.logging.MemoryHandler.target");
0N/A Class clz = ClassLoader.getSystemClassLoader().loadClass(name);
0N/A target = (Handler) clz.newInstance();
0N/A } catch (Exception ex) {
0N/A throw new RuntimeException("MemoryHandler can't load handler \"" + name + "\"" , ex);
0N/A }
0N/A init();
0N/A }
0N/A
0N/A // Initialize. Size is a count of LogRecords.
0N/A private void init() {
0N/A buffer = new LogRecord[size];
0N/A start = 0;
0N/A count = 0;
0N/A }
0N/A
0N/A /**
0N/A * Create a <tt>MemoryHandler</tt>.
0N/A * <p>
0N/A * The <tt>MemoryHandler</tt> is configured based on <tt>LogManager</tt>
0N/A * properties (or their default values) except that the given <tt>pushLevel</tt>
0N/A * argument and buffer size argument are used.
0N/A *
0N/A * @param target the Handler to which to publish output.
0N/A * @param size the number of log records to buffer (must be greater than zero)
0N/A * @param pushLevel message level to push on
0N/A *
1664N/A * @throws IllegalArgumentException if size is <= 0
0N/A */
0N/A public MemoryHandler(Handler target, int size, Level pushLevel) {
0N/A if (target == null || pushLevel == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (size <= 0) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A sealed = false;
0N/A configure();
0N/A sealed = true;
0N/A this.target = target;
0N/A this.pushLevel = pushLevel;
0N/A this.size = size;
0N/A init();
0N/A }
0N/A
0N/A /**
0N/A * Store a <tt>LogRecord</tt> in an internal buffer.
0N/A * <p>
0N/A * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt>
0N/A * method is called to check if the given log record is loggable.
0N/A * If not we return. Otherwise the given record is copied into
0N/A * an internal circular buffer. Then the record's level property is
0N/A * compared with the <tt>pushLevel</tt>. If the given level is
0N/A * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt>
0N/A * is called to write all buffered records to the target output
0N/A * <tt>Handler</tt>.
0N/A *
0N/A * @param record description of the log event. A null record is
0N/A * silently ignored and is not published
0N/A */
0N/A public synchronized void publish(LogRecord record) {
0N/A if (!isLoggable(record)) {
0N/A return;
0N/A }
0N/A int ix = (start+count)%buffer.length;
0N/A buffer[ix] = record;
0N/A if (count < buffer.length) {
0N/A count++;
0N/A } else {
0N/A start++;
0N/A start %= buffer.length;
0N/A }
0N/A if (record.getLevel().intValue() >= pushLevel.intValue()) {
0N/A push();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Push any buffered output to the target <tt>Handler</tt>.
0N/A * <p>
0N/A * The buffer is then cleared.
0N/A */
0N/A public synchronized void push() {
0N/A for (int i = 0; i < count; i++) {
0N/A int ix = (start+i)%buffer.length;
0N/A LogRecord record = buffer[ix];
0N/A target.publish(record);
0N/A }
0N/A // Empty the buffer.
0N/A start = 0;
0N/A count = 0;
0N/A }
0N/A
0N/A /**
0N/A * Causes a flush on the target <tt>Handler</tt>.
0N/A * <p>
0N/A * Note that the current contents of the <tt>MemoryHandler</tt>
0N/A * buffer are <b>not</b> written out. That requires a "push".
0N/A */
0N/A public void flush() {
0N/A target.flush();
0N/A }
0N/A
0N/A /**
0N/A * Close the <tt>Handler</tt> and free all associated resources.
0N/A * This will also close the target <tt>Handler</tt>.
0N/A *
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A */
0N/A public void close() throws SecurityException {
0N/A target.close();
0N/A setLevel(Level.OFF);
0N/A }
0N/A
0N/A /**
0N/A * Set the <tt>pushLevel</tt>. After a <tt>LogRecord</tt> is copied
0N/A * into our internal buffer, if its level is greater than or equal to
0N/A * the <tt>pushLevel</tt>, then <tt>push</tt> will be called.
0N/A *
0N/A * @param newLevel the new value of the <tt>pushLevel</tt>
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A */
0N/A public void setPushLevel(Level newLevel) throws SecurityException {
0N/A if (newLevel == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A LogManager manager = LogManager.getLogManager();
5430N/A checkPermission();
0N/A pushLevel = newLevel;
0N/A }
0N/A
0N/A /**
0N/A * Get the <tt>pushLevel</tt>.
0N/A *
0N/A * @return the value of the <tt>pushLevel</tt>
0N/A */
0N/A public synchronized Level getPushLevel() {
0N/A return pushLevel;
0N/A }
0N/A
0N/A /**
0N/A * Check if this <tt>Handler</tt> would actually log a given
0N/A * <tt>LogRecord</tt> into its internal buffer.
0N/A * <p>
0N/A * This method checks if the <tt>LogRecord</tt> has an appropriate level and
0N/A * whether it satisfies any <tt>Filter</tt>. However it does <b>not</b>
0N/A * check whether the <tt>LogRecord</tt> would result in a "push" of the
1664N/A * buffer contents. It will return false if the <tt>LogRecord</tt> is null.
0N/A * <p>
0N/A * @param record a <tt>LogRecord</tt>
0N/A * @return true if the <tt>LogRecord</tt> would be logged.
0N/A *
0N/A */
0N/A public boolean isLoggable(LogRecord record) {
0N/A return super.isLoggable(record);
0N/A }
0N/A}