jdk/src/java.logging/share/classes/java/util/logging/MemoryHandler.java
author dfuchs
Thu, 06 Apr 2017 14:38:15 +0100
changeset 44538 a603c475d649
parent 37782 ad8fe7507ecc
permissions -rw-r--r--
8178139: Minor typo in API documentation of java.util.logging.Logger Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22110
diff changeset
     2
 * Copyright (c) 2000, 2013, 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
/**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    29
 * {@code Handler} that buffers requests in a circular buffer in memory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <p>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    31
 * Normally this {@code Handler} simply stores incoming {@code LogRecords}
2
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
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    34
 * conditions, the {@code MemoryHandler} will push out its current buffer
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    35
 * contents to a target {@code Handler}, which will typically publish
2
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>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    41
 * An incoming {@code LogRecord} has a type that is greater than
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    42
 * a pre-defined level, the {@code pushLevel}. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    44
 * An external class calls the {@code push} method explicitly. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    46
 * A subclass overrides the {@code log} method and scans each incoming
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    47
 * {@code LogRecord} and calls {@code push} 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>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    52
 * By default each {@code MemoryHandler} is initialized using the following
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    53
 * {@code LogManager} configuration properties where {@code <handler-name>}
14321
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
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    60
 *        specifies the level for the {@code Handler}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    61
 *        (defaults to {@code Level.ALL}). </li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    62
 * <li>   &lt;handler-name&gt;.filter
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    63
 *        specifies the name of a {@code Filter} class to use
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    64
 *        (defaults to no {@code Filter}). </li>
14321
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>
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14333
diff changeset
    67
 * <li>   &lt;handler-name&gt;.push
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    68
 *        defines the {@code pushLevel} (defaults to {@code level.SEVERE}). </li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    69
 * <li>   &lt;handler-name&gt;.target
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    70
 *        specifies the name of the target {@code Handler } 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>
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23010
diff changeset
    85
 *
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;
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
    91
    private volatile Level pushLevel;
2
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
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    97
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    98
     * Create a {@code MemoryHandler} and configure it based on
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
    99
     * {@code LogManager} configuration properties.
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   100
     */
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   101
    public MemoryHandler() {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   102
        // configure with specific defaults for MemoryHandler
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   103
        super(Level.ALL, new SimpleFormatter(), null);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String cname = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            size = DEFAULT_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   112
        String targetName = manager.getProperty(cname+".target");
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   113
        if (targetName == null) {
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   114
            throw new RuntimeException("The handler " + cname
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   115
                    + " does not specify a target");
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   116
        }
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   117
        Class<?> clz;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   119
            clz = ClassLoader.getSystemClassLoader().loadClass(targetName);
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32037
diff changeset
   120
            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32037
diff changeset
   121
            Object o = clz.newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32037
diff changeset
   122
            target = (Handler) o;
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   123
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
   124
            throw new RuntimeException("MemoryHandler can't load handler target \"" + targetName + "\"" , e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // Initialize.  Size is a count of LogRecords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        buffer = new LogRecord[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   137
     * Create a {@code MemoryHandler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   139
     * The {@code MemoryHandler} is configured based on {@code LogManager}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   140
     * properties (or their default values) except that the given {@code pushLevel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * argument and buffer size argument are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param target  the Handler to which to publish output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param size    the number of log records to buffer (must be greater than zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param pushLevel  message level to push on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14333
diff changeset
   147
     * @throws IllegalArgumentException if {@code size is <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public MemoryHandler(Handler target, int size, Level pushLevel) {
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   150
        // configure with specific defaults for MemoryHandler
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   151
        super(Level.ALL, new SimpleFormatter(), null);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (target == null || pushLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.pushLevel = pushLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   166
     * Store a {@code LogRecord} in an internal buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   168
     * If there is a {@code Filter}, its {@code isLoggable}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * method is called to check if the given log record is loggable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * If not we return.  Otherwise the given record is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * an internal circular buffer.  Then the record's level property is
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   172
     * compared with the {@code pushLevel}. If the given level is
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   173
     * greater than or equal to the {@code pushLevel} then {@code push}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * is called to write all buffered records to the target output
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   175
     * {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public synchronized void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (!isLoggable(record)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int ix = (start+count)%buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        buffer[ix] = record;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (count < buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            start %= buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (record.getLevel().intValue() >= pushLevel.intValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            push();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   199
     * Push any buffered output to the target {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * The buffer is then cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public synchronized void push() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int ix = (start+i)%buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            LogRecord record = buffer[ix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            target.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Empty the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   215
     * Causes a flush on the target {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   217
     * Note that the current contents of the {@code MemoryHandler}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * buffer are <b>not</b> written out.  That requires a "push".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   220
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        target.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   226
     * Close the {@code Handler} and free all associated resources.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   227
     * This will also close the target {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception  SecurityException  if a security manager exists and if
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   230
     *             the caller does not have {@code LoggingPermission("control")}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   232
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void close() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        target.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        setLevel(Level.OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   239
     * Set the {@code pushLevel}.  After a {@code LogRecord} is copied
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * into our internal buffer, if its level is greater than or equal to
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   241
     * the {@code pushLevel}, then {@code push} will be called.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   243
     * @param newLevel the new value of the {@code pushLevel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception  SecurityException  if a security manager exists and if
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   245
     *             the caller does not have {@code LoggingPermission("control")}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   247
    public synchronized void setPushLevel(Level newLevel) throws SecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (newLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   251
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        pushLevel = newLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   256
     * Get the {@code pushLevel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   258
     * @return the value of the {@code pushLevel}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   260
    public Level getPushLevel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return pushLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   265
     * Check if this {@code Handler} would actually log a given
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   266
     * {@code LogRecord} into its internal buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   268
     * This method checks if the {@code LogRecord} has an appropriate level and
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   269
     * whether it satisfies any {@code Filter}.  However it does <b>not</b>
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   270
     * check whether the {@code LogRecord} would result in a "push" of the
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   271
     * buffer contents. It will return false if the {@code LogRecord} is null.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23010
diff changeset
   272
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   273
     * @param record  a {@code LogRecord}
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 25859
diff changeset
   274
     * @return true if the {@code LogRecord} would be logged.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18156
diff changeset
   277
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public boolean isLoggable(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return super.isLoggable(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}