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