jdk/src/share/classes/java/util/logging/Handler.java
author dcubed
Tue, 15 Sep 2009 22:11:15 -0600
changeset 3853 9d2382b74894
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6882363: 4/4 typos in java.util.logging javadocs Summary: Fix typos, some grammar and some inconsistencies in phrasing. Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * exports them.  It might for example, write them to a console
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * or write them to a file, or send them to a network logging service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * or forward them to an OS log, or whatever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A <tt>Handler</tt> can be disabled by doing a <tt>setLevel(Level.OFF)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and can  be re-enabled by doing a <tt>setLevel</tt> with an appropriate level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <tt>Handler</tt> classes typically use <tt>LogManager</tt> properties to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * default values for the <tt>Handler</tt>'s <tt>Filter</tt>, <tt>Formatter</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and <tt>Level</tt>.  See the specific documentation for each concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <tt>Handler</tt> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int offValue = Level.OFF.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Filter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Formatter formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Level logLevel = Level.ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private ErrorManager errorManager = new ErrorManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Package private support for security checking.  When sealed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // is true, we access check updates to the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    boolean sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Default constructor.  The resulting <tt>Handler</tt> has a log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <tt>Filter</tt>.  A default <tt>ErrorManager</tt> instance is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * as the <tt>ErrorManager</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected Handler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Publish a <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The logging request was made initially to a <tt>Logger</tt> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * which initialized the <tt>LogRecord</tt> and forwarded it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The <tt>Handler</tt>  is responsible for formatting the message, when and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * if necessary.  The formatting should include localization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public abstract void publish(LogRecord record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Flush any buffered output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public abstract void flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Close the <tt>Handler</tt> and free all associated resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The close method will perform a <tt>flush</tt> and then close the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <tt>Handler</tt>.   After close has been called this <tt>Handler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * should no longer be used.  Method calls may either be silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * ignored or may throw runtime exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public abstract void close() throws SecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Set a <tt>Formatter</tt>.  This <tt>Formatter</tt> will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * to format <tt>LogRecords</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Some <tt>Handlers</tt> may not use <tt>Formatters</tt>, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * which case the <tt>Formatter</tt> will be remembered, but not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param newFormatter the <tt>Formatter</tt> to use (may not be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void setFormatter(Formatter newFormatter) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Check for a null pointer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        newFormatter.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        formatter = newFormatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Return the <tt>Formatter</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the <tt>Formatter</tt> (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Formatter getFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Set the character encoding used by this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The encoding should be set before any <tt>LogRecords</tt> are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * to the <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param encoding  The name of a supported character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *        May be null, to indicate the default platform encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception  UnsupportedEncodingException if the named encoding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *          not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void setEncoding(String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        throws SecurityException, java.io.UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (encoding != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if(!java.nio.charset.Charset.isSupported(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    throw new UnsupportedEncodingException(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (java.nio.charset.IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new UnsupportedEncodingException(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.encoding = encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Return the character encoding for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return  The encoding name.  May be null, which indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          default encoding should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public String getEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Set a <tt>Filter</tt> to control output on this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * For each call of <tt>publish</tt> the <tt>Handler</tt> will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * this <tt>Filter</tt> (if it is non-null) to check if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <tt>LogRecord</tt> should be published or discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param   newFilter  a <tt>Filter</tt> object (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void setFilter(Filter newFilter) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        filter = newFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Get the current <tt>Filter</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return  a <tt>Filter</tt> object (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Filter getFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Define an ErrorManager for this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * The ErrorManager's "error" method will be invoked if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * errors occur while using this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param em  the new ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public void setErrorManager(ErrorManager em) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (em == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
           throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        errorManager = em;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Retrieves the ErrorManager for this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public ErrorManager getErrorManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return errorManager;
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
     * Protected convenience method to report an error to this Handler's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * ErrorManager.  Note that this method retrieves and uses the ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * without doing a security check.  It can therefore be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * environments where the caller may be non-privileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param msg    a descriptive string (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param ex     an exception (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param code   an error code defined in ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    protected void reportError(String msg, Exception ex, int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            errorManager.error(msg, ex, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } catch (Exception ex2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            System.err.println("Handler.reportError caught:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            ex2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Set the log level specifying which message levels will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * logged by this <tt>Handler</tt>.  Message levels lower than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * value will be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * The intention is to allow developers to turn on voluminous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * logging, but to limit the messages that are sent to certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <tt>Handlers</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param newLevel   the new value for the log level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized void setLevel(Level newLevel) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (newLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        logLevel = newLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Get the log level specifying which messages will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * logged by this <tt>Handler</tt>.  Message levels lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * than this level will be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return  the level of messages being logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public synchronized Level getLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return logLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * This method checks if the <tt>LogRecord</tt> has an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <tt>Level</tt> and  whether it satisfies any <tt>Filter</tt>.  It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * may make other <tt>Handler</tt> specific checks that might prevent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * handler from logging the <tt>LogRecord</tt>. It will return false if
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   277
     * the <tt>LogRecord</tt> is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param record  a <tt>LogRecord</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return true if the <tt>LogRecord</tt> would be logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public boolean isLoggable(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int levelValue = getLevel().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Filter filter = getFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (filter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return filter.isLoggable(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // Package-private support method for security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // If "sealed" is true, we check that the caller has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // appropriate security privileges to update Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // state and if not throw a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    void checkAccess() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (sealed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            manager.checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}