src/java.logging/share/classes/java/util/logging/StreamHandler.java
author jjg
Fri, 12 Jan 2018 11:41:32 -0800
changeset 48521 30243cf1503e
parent 47216 71c04702a3d5
child 53876 8bc3d3eeaa53
permissions -rw-r--r--
8194955: Warn when default HTML version is used Reviewed-by: ksrini, bpatel
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    30
import java.security.AccessController;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    31
import java.security.PrivilegedAction;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    32
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
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
    35
 * Stream based logging {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This is primarily intended as a base class or support class 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
    38
 * be used in implementing other logging {@code Handlers}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <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
    40
 * {@code LogRecords} are published to a given {@code java.io.OutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <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
    43
 * By default each {@code StreamHandler} 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
    44
 * {@code LogManager} configuration properties where {@code <handler-name>}
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    45
 * refers to the fully-qualified class name of the handler.
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    46
 * If properties are not defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (or have invalid values) then the specified default values are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <ul>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    49
 * <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
    50
 *        specifies the default 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
    51
 *        (defaults to {@code Level.INFO}). </li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    52
 * <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
    53
 *        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
    54
 *         (defaults to no {@code Filter}). </li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    55
 * <li>   &lt;handler-name&gt;.formatter
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
    56
 *        specifies the name of a {@code Formatter} 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
    57
 *        (defaults to {@code java.util.logging.SimpleFormatter}). </li>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    58
 * <li>   &lt;handler-name&gt;.encoding
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *        the name of the character set encoding to use (defaults to
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    60
 *        the default platform encoding). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    61
 * </ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    62
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    63
 * For example, the properties for {@code StreamHandler} would be:
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    64
 * <ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    65
 * <li>   java.util.logging.StreamHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    66
 * <li>   java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </ul>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    68
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    69
 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    70
 * <ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    71
 * <li>   com.foo.MyHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    72
 * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    73
 * </ul>
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23010
diff changeset
    74
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class StreamHandler extends Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean doneHeader;
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
    81
    private volatile Writer writer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
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
    84
     * Create a {@code StreamHandler}, with no current output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public StreamHandler() {
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    87
        // configure with specific defaults for StreamHandler
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    88
        super(Level.INFO, new SimpleFormatter(), null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
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
    92
     * Create a {@code StreamHandler} with a given {@code Formatter}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * and output stream.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23010
diff changeset
    94
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param out         the target output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param formatter   Formatter to be used to format output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public StreamHandler(OutputStream out, Formatter formatter) {
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    99
        // configure with default level but use specified formatter
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   100
        super(Level.INFO, null, Objects.requireNonNull(formatter));
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   101
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   102
        setOutputStreamPrivileged(out);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   103
    }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   104
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   105
    /**
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   106
     * @see Handler#Handler(Level, Formatter, Formatter)
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   107
     */
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   108
    StreamHandler(Level defaultLevel,
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   109
                  Formatter defaultFormatter,
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   110
                  Formatter specifiedFormatter) {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   111
        super(defaultLevel, defaultFormatter, specifiedFormatter);
2
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
     * Change the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <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
   117
     * If there is a current output stream then the {@code Formatter}'s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * tail string is written and the stream is flushed and closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Then the output stream is replaced with the new output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param out   New output stream.  May not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @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
   123
     *             the caller does not have {@code LoggingPermission("control")}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected synchronized void setOutputStream(OutputStream out) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        flushAndClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        output = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        doneHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String encoding = getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            writer = new OutputStreamWriter(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                writer = new OutputStreamWriter(output, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } catch (UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // This shouldn't happen.  The setEncoding method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // should have validated that the encoding is OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new Error("Unexpected exception " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
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
   147
     * Set (or change) the character encoding used by this {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <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
   149
     * The encoding should be set before any {@code LogRecords} are written
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
   150
     * to the {@code Handler}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param encoding  The name of a supported character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *        May be null, to indicate the default platform encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @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
   155
     *             the caller does not have {@code LoggingPermission("control")}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception  UnsupportedEncodingException if the named encoding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   159
    @Override
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   160
    public synchronized void setEncoding(String encoding)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        throws SecurityException, java.io.UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        super.setEncoding(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (output == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Replace the current writer with a writer for the new encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            writer = new OutputStreamWriter(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            writer = new OutputStreamWriter(output, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
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
   176
     * Format and publish a {@code LogRecord}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <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
   178
     * The {@code StreamHandler} first checks if there is an {@code OutputStream}
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
   179
     * and if the given {@code LogRecord} has at least the required log level.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If not it silently returns.  If so, it calls any associated
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
   181
     * {@code Filter} to check if the record should be published.  If so,
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
   182
     * it calls its {@code Formatter} to format the record and then writes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the result to the current output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <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
   185
     * If this is the first {@code LogRecord} to be written to 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
   186
     * {@code OutputStream}, the {@code Formatter}'s "head" string 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
   187
     * written to the stream before the {@code LogRecord} is written.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   192
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public synchronized void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (!isLoggable(record)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            msg = getFormatter().format(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // We don't want to throw an exception here, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // report the exception to any registered ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            reportError(null, ex, ErrorManager.FORMAT_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (!doneHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                writer.write(getFormatter().getHead(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                doneHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            writer.write(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // We don't want to throw an exception here, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // report the exception to any registered ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            reportError(null, ex, ErrorManager.WRITE_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
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
   222
     * Check if this {@code Handler} would actually log a given {@code LogRecord}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <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
   224
     * 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
   225
     * whether it satisfies any {@code Filter}.  It will also return false if
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   226
     * no output stream has been assigned yet or the 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
   227
     *
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
   228
     * @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
   229
     * @return true if the {@code LogRecord} would be logged.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   232
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public boolean isLoggable(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (writer == null || record == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return super.isLoggable(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Flush any buffered messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   243
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public synchronized void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (writer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                writer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // We don't want to throw an exception here, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // report the exception to any registered ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                reportError(null, ex, ErrorManager.FLUSH_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private synchronized void flushAndClose() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   257
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (writer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (!doneHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    writer.write(getFormatter().getHead(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    doneHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                writer.write(getFormatter().getTail(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                writer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                writer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // We don't want to throw an exception here, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                // report the exception to any registered ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                reportError(null, ex, ErrorManager.CLOSE_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            writer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            output = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Close the current output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <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
   280
     * The {@code Formatter}'s "tail" string is written to the stream before it
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
   281
     * is closed.  In addition, if the {@code Formatter}'s "head" string has not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * yet been written to the stream, it will be written before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * "tail" string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 14333
diff changeset
   288
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public synchronized void close() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        flushAndClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   292
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   293
    // Package-private support for setting OutputStream
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   294
    // with elevated privilege.
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   295
    final void setOutputStreamPrivileged(final OutputStream out) {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   296
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   297
            @Override
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   298
            public Void run() {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   299
                setOutputStream(out);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   300
                return null;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   301
            }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   302
        }, null, LogManager.controlPermission);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   303
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}