0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.util.logging;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * Stream based logging <tt>Handler</tt>.
0N/A * <p>
0N/A * This is primarily intended as a base class or support class to
0N/A * be used in implementing other logging <tt>Handlers</tt>.
0N/A * <p>
0N/A * <tt>LogRecords</tt> are published to a given <tt>java.io.OutputStream</tt>.
0N/A * <p>
0N/A * <b>Configuration:</b>
0N/A * By default each <tt>StreamHandler</tt> is initialized using the following
0N/A * <tt>LogManager</tt> configuration properties. If properties are not defined
0N/A * (or have invalid values) then the specified default values are used.
0N/A * <ul>
0N/A * <li> java.util.logging.StreamHandler.level
0N/A * specifies the default level for the <tt>Handler</tt>
0N/A * (defaults to <tt>Level.INFO</tt>).
0N/A * <li> java.util.logging.StreamHandler.filter
0N/A * specifies the name of a <tt>Filter</tt> class to use
0N/A * (defaults to no <tt>Filter</tt>).
0N/A * <li> java.util.logging.StreamHandler.formatter
0N/A * specifies the name of a <tt>Formatter</tt> class to use
0N/A * (defaults to <tt>java.util.logging.SimpleFormatter</tt>).
0N/A * <li> java.util.logging.StreamHandler.encoding
0N/A * the name of the character set encoding to use (defaults to
0N/A * the default platform encoding).
0N/A * </ul>
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class StreamHandler extends Handler {
0N/A private LogManager manager = LogManager.getLogManager();
0N/A private OutputStream output;
0N/A private boolean doneHeader;
0N/A private Writer writer;
0N/A
0N/A // Private method to configure a StreamHandler from LogManager
0N/A // properties and/or default values as specified in the class
0N/A // javadoc.
0N/A private void configure() {
0N/A LogManager manager = LogManager.getLogManager();
0N/A String cname = getClass().getName();
0N/A
0N/A setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
0N/A setFilter(manager.getFilterProperty(cname +".filter", null));
0N/A setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
0N/A try {
0N/A setEncoding(manager.getStringProperty(cname +".encoding", null));
0N/A } catch (Exception ex) {
0N/A try {
0N/A setEncoding(null);
0N/A } catch (Exception ex2) {
0N/A // doing a setEncoding with null should always work.
0N/A // assert false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create a <tt>StreamHandler</tt>, with no current output stream.
0N/A */
0N/A public StreamHandler() {
0N/A sealed = false;
0N/A configure();
0N/A sealed = true;
0N/A }
0N/A
0N/A /**
0N/A * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt>
0N/A * and output stream.
0N/A * <p>
0N/A * @param out the target output stream
0N/A * @param formatter Formatter to be used to format output
0N/A */
0N/A public StreamHandler(OutputStream out, Formatter formatter) {
0N/A sealed = false;
0N/A configure();
0N/A setFormatter(formatter);
0N/A setOutputStream(out);
0N/A sealed = true;
0N/A }
0N/A
0N/A /**
0N/A * Change the output stream.
0N/A * <P>
0N/A * If there is a current output stream then the <tt>Formatter</tt>'s
0N/A * tail string is written and the stream is flushed and closed.
0N/A * Then the output stream is replaced with the new output stream.
0N/A *
0N/A * @param out New output stream. May not be null.
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A */
0N/A protected synchronized void setOutputStream(OutputStream out) throws SecurityException {
0N/A if (out == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A flushAndClose();
0N/A output = out;
0N/A doneHeader = false;
0N/A String encoding = getEncoding();
0N/A if (encoding == null) {
0N/A writer = new OutputStreamWriter(output);
0N/A } else {
0N/A try {
0N/A writer = new OutputStreamWriter(output, encoding);
0N/A } catch (UnsupportedEncodingException ex) {
0N/A // This shouldn't happen. The setEncoding method
0N/A // should have validated that the encoding is OK.
0N/A throw new Error("Unexpected exception " + ex);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set (or change) the character encoding used by this <tt>Handler</tt>.
0N/A * <p>
0N/A * The encoding should be set before any <tt>LogRecords</tt> are written
0N/A * to the <tt>Handler</tt>.
0N/A *
0N/A * @param encoding The name of a supported character encoding.
0N/A * May be null, to indicate the default platform encoding.
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A * @exception UnsupportedEncodingException if the named encoding is
0N/A * not supported.
0N/A */
0N/A public void setEncoding(String encoding)
0N/A throws SecurityException, java.io.UnsupportedEncodingException {
0N/A super.setEncoding(encoding);
0N/A if (output == null) {
0N/A return;
0N/A }
0N/A // Replace the current writer with a writer for the new encoding.
0N/A flush();
0N/A if (encoding == null) {
0N/A writer = new OutputStreamWriter(output);
0N/A } else {
0N/A writer = new OutputStreamWriter(output, encoding);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Format and publish a <tt>LogRecord</tt>.
0N/A * <p>
0N/A * The <tt>StreamHandler</tt> first checks if there is an <tt>OutputStream</tt>
0N/A * and if the given <tt>LogRecord</tt> has at least the required log level.
0N/A * If not it silently returns. If so, it calls any associated
0N/A * <tt>Filter</tt> to check if the record should be published. If so,
0N/A * it calls its <tt>Formatter</tt> to format the record and then writes
0N/A * the result to the current output stream.
0N/A * <p>
0N/A * If this is the first <tt>LogRecord</tt> to be written to a given
0N/A * <tt>OutputStream</tt>, the <tt>Formatter</tt>'s "head" string is
0N/A * written to the stream before the <tt>LogRecord</tt> is written.
0N/A *
0N/A * @param record description of the log event. A null record is
0N/A * silently ignored and is not published
0N/A */
0N/A public synchronized void publish(LogRecord record) {
0N/A if (!isLoggable(record)) {
0N/A return;
0N/A }
0N/A String msg;
0N/A try {
0N/A msg = getFormatter().format(record);
0N/A } catch (Exception ex) {
0N/A // We don't want to throw an exception here, but we
0N/A // report the exception to any registered ErrorManager.
0N/A reportError(null, ex, ErrorManager.FORMAT_FAILURE);
0N/A return;
0N/A }
0N/A
0N/A try {
0N/A if (!doneHeader) {
0N/A writer.write(getFormatter().getHead(this));
0N/A doneHeader = true;
0N/A }
0N/A writer.write(msg);
0N/A } catch (Exception ex) {
0N/A // We don't want to throw an exception here, but we
0N/A // report the exception to any registered ErrorManager.
0N/A reportError(null, ex, ErrorManager.WRITE_FAILURE);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
0N/A * <p>
0N/A * This method checks if the <tt>LogRecord</tt> has an appropriate level and
0N/A * whether it satisfies any <tt>Filter</tt>. It will also return false if
1664N/A * no output stream has been assigned yet or the LogRecord is null.
0N/A * <p>
0N/A * @param record a <tt>LogRecord</tt>
0N/A * @return true if the <tt>LogRecord</tt> would be logged.
0N/A *
0N/A */
0N/A public boolean isLoggable(LogRecord record) {
0N/A if (writer == null || record == null) {
0N/A return false;
0N/A }
0N/A return super.isLoggable(record);
0N/A }
0N/A
0N/A /**
0N/A * Flush any buffered messages.
0N/A */
0N/A public synchronized void flush() {
0N/A if (writer != null) {
0N/A try {
0N/A writer.flush();
0N/A } catch (Exception ex) {
0N/A // We don't want to throw an exception here, but we
0N/A // report the exception to any registered ErrorManager.
0N/A reportError(null, ex, ErrorManager.FLUSH_FAILURE);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private synchronized void flushAndClose() throws SecurityException {
5430N/A checkPermission();
0N/A if (writer != null) {
0N/A try {
0N/A if (!doneHeader) {
0N/A writer.write(getFormatter().getHead(this));
0N/A doneHeader = true;
0N/A }
0N/A writer.write(getFormatter().getTail(this));
0N/A writer.flush();
0N/A writer.close();
0N/A } catch (Exception ex) {
0N/A // We don't want to throw an exception here, but we
0N/A // report the exception to any registered ErrorManager.
0N/A reportError(null, ex, ErrorManager.CLOSE_FAILURE);
0N/A }
0N/A writer = null;
0N/A output = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Close the current output stream.
0N/A * <p>
0N/A * The <tt>Formatter</tt>'s "tail" string is written to the stream before it
0N/A * is closed. In addition, if the <tt>Formatter</tt>'s "head" string has not
0N/A * yet been written to the stream, it will be written before the
0N/A * "tail" string.
0N/A *
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have LoggingPermission("control").
0N/A */
0N/A public synchronized void close() throws SecurityException {
0N/A flushAndClose();
0N/A }
0N/A}