jdk/src/share/classes/java/util/logging/MemoryHandler.java
author mchung
Thu, 07 Feb 2013 09:41:47 -0800
changeset 16112 fc9eb3e70734
parent 14333 65fe875afb41
child 18156 edb590d448c5
permissions -rw-r--r--
8007611: logging behavior in applet changed Reviewed-by: alanb, jgish
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <tt>Handler</tt> that buffers requests in a circular buffer in memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Normally this <tt>Handler</tt> simply stores incoming <tt>LogRecords</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * into its memory buffer and discards earlier records.  This buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * is very cheap and avoids formatting costs.  On certain trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * conditions, the <tt>MemoryHandler</tt> will push out its current buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * contents to a target <tt>Handler</tt>, which will typically publish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * them to the outside world.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * There are three main models for triggering a push of the buffer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An incoming <tt>LogRecord</tt> has a type that is greater than
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    42
 * a pre-defined level, the <tt>pushLevel</tt>. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    44
 * An external class calls the <tt>push</tt> method explicitly. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A subclass overrides the <tt>log</tt> method and scans each incoming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <tt>LogRecord</tt> and calls <tt>push</tt> if a record matches some
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    48
 * desired criteria. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <b>Configuration:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * By default each <tt>MemoryHandler</tt> is initialized using the following
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    53
 * <tt>LogManager</tt> configuration properties where <tt>&lt;handler-name&gt;</tt>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    54
 * refers to the fully-qualified class name of the handler.
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    55
 * If properties are not defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * (or have invalid values) then the specified default values are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * If no default value is defined then a RuntimeException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <ul>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    59
 * <li>   &lt;handler-name&gt;.level
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *        specifies the level for the <tt>Handler</tt>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    61
 *        (defaults to <tt>Level.ALL</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    62
 * <li>   &lt;handler-name&gt;.filter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *        specifies the name of a <tt>Filter</tt> class to use
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    64
 *        (defaults to no <tt>Filter</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    65
 * <li>   &lt;handler-name&gt;.size
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    66
 *        defines the buffer size (defaults to 1000). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    67
 * <li>   &lt;handler-name>.push
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    68
 *        defines the <tt>pushLevel</tt> (defaults to <tt>level.SEVERE</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    69
 * <li>   &lt;handler-name&gt;.target
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *        specifies the name of the target <tt>Handler </tt> class.
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    71
 *        (no default). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    72
 * </ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    73
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    74
 * For example, the properties for {@code MemoryHandler} would be:
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    75
 * <ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    76
 * <li>   java.util.logging.MemoryHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    77
 * <li>   java.util.logging.MemoryHandler.formatter=java.util.logging.SimpleFormatter </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </ul>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    79
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    80
 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    81
 * <ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    82
 * <li>   com.foo.MyHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    83
 * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    84
 * </ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    85
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
public class MemoryHandler extends Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final static int DEFAULT_SIZE = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private Level pushLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private Handler target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private LogRecord buffer[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    int start, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    97
    // Private method to configure a MemoryHandler from LogManager
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // properties and/or default values as specified in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // javadoc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private void configure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String cname = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            size = DEFAULT_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        setFilter(manager.getFilterProperty(cname +".filter", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Create a <tt>MemoryHandler</tt> and configure it based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <tt>LogManager</tt> configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public MemoryHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        sealed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   123
        LogManager manager = LogManager.getLogManager();
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   124
        String handlerName = getClass().getName();
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   125
        String targetName = manager.getProperty(handlerName+".target");
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   126
        if (targetName == null) {
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   127
            throw new RuntimeException("The handler " + handlerName
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   128
                    + " does not specify a target");
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   129
        }
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   130
        Class<?> clz;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   132
            clz = ClassLoader.getSystemClassLoader().loadClass(targetName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            target = (Handler) clz.newInstance();
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   134
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   135
            throw new RuntimeException("MemoryHandler can't load handler target \"" + targetName + "\"" , e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // Initialize.  Size is a count of LogRecords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        buffer = new LogRecord[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Create a <tt>MemoryHandler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * The <tt>MemoryHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * properties (or their default values) except that the given <tt>pushLevel</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * argument and buffer size argument are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param target  the Handler to which to publish output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param size    the number of log records to buffer (must be greater than zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param pushLevel  message level to push on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   158
     * @throws IllegalArgumentException if size is <= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public MemoryHandler(Handler target, int size, Level pushLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (target == null || pushLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        sealed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.pushLevel = pushLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Store a <tt>LogRecord</tt> in an internal buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * method is called to check if the given log record is loggable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * If not we return.  Otherwise the given record is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * an internal circular buffer.  Then the record's level property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * compared with the <tt>pushLevel</tt>. If the given level is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * is called to write all buffered records to the target output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public synchronized void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!isLoggable(record)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int ix = (start+count)%buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        buffer[ix] = record;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (count < buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            start %= buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (record.getLevel().intValue() >= pushLevel.intValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            push();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Push any buffered output to the target <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * The buffer is then cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public synchronized void push() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            int ix = (start+i)%buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            LogRecord record = buffer[ix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            target.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // Empty the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Causes a flush on the target <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Note that the current contents of the <tt>MemoryHandler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * buffer are <b>not</b> written out.  That requires a "push".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        target.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Close the <tt>Handler</tt> and free all associated resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * This will also close the target <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void close() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        target.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        setLevel(Level.OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Set the <tt>pushLevel</tt>.  After a <tt>LogRecord</tt> is copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * into our internal buffer, if its level is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the <tt>pushLevel</tt>, then <tt>push</tt> will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param newLevel the new value of the <tt>pushLevel</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public void setPushLevel(Level newLevel) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (newLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        LogManager manager = LogManager.getLogManager();
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   260
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        pushLevel = newLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Get the <tt>pushLevel</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the value of the <tt>pushLevel</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public synchronized Level getPushLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return pushLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Check if this <tt>Handler</tt> would actually log a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <tt>LogRecord</tt> into its internal buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * This method checks if the <tt>LogRecord</tt> has an appropriate level and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * whether it satisfies any <tt>Filter</tt>.  However it does <b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * check whether the <tt>LogRecord</tt> would result in a "push" of the
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   280
     * buffer contents. It will return false if the <tt>LogRecord</tt> is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param record  a <tt>LogRecord</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return true if the <tt>LogRecord</tt> would be logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public boolean isLoggable(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return super.isLoggable(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
}