📄 streamhandler.java
字号:
/* StreamHandler.java -- a class for publishing log messages to instances of java.io.OutputStreamCopyright (C) 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version.*/package java.util.logging;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;/** * A <code>StreamHandler</code> publishes <code>LogRecords</code> to * a instances of <code>java.io.OutputStream</code>. * * @author Sascha Brawer (brawer@acm.org) */public class StreamHandler extends Handler{ private OutputStream out; private Writer writer; /** * Indicates the current state of this StreamHandler. The value * should be one of STATE_FRESH, STATE_PUBLISHED, or STATE_CLOSED. */ private int streamState = STATE_FRESH; /** * streamState having this value indicates that the StreamHandler * has been created, but the publish(LogRecord) method has not been * called yet. If the StreamHandler has been constructed without an * OutputStream, writer will be null, otherwise it is set to a * freshly created OutputStreamWriter. */ private static final int STATE_FRESH = 0; /** * streamState having this value indicates that the publish(LocRecord) * method has been called at least once. */ private static final int STATE_PUBLISHED = 1; /** * streamState having this value indicates that the close() method * has been called. */ private static final int STATE_CLOSED = 2; /** * Creates a <code>StreamHandler</code> without an output stream. * Subclasses can later use {@link * #setOutputStream(java.io.OutputStream)} to associate an output * stream with this StreamHandler. */ public StreamHandler() { this(null, null); } /** * Creates a <code>StreamHandler</code> that formats log messages * with the specified Formatter and publishes them to the specified * output stream. * * @param out the output stream to which the formatted log messages * are published. * * @param formatter the <code>Formatter</code> that will be used * to format log messages. */ public StreamHandler(OutputStream out, Formatter formatter) { this(out, "java.util.logging.StreamHandler", Level.INFO, formatter, SimpleFormatter.class); } StreamHandler( OutputStream out, String propertyPrefix, Level defaultLevel, Formatter formatter, Class defaultFormatterClass) { this.level = LogManager.getLevelProperty(propertyPrefix + ".level", defaultLevel); this.filter = (Filter) LogManager.getInstanceProperty( propertyPrefix + ".filter", /* must be instance of */ Filter.class, /* default: new instance of */ null); if (formatter != null) this.formatter = formatter; else this.formatter = (Formatter) LogManager.getInstanceProperty( propertyPrefix + ".formatter", /* must be instance of */ Formatter.class, /* default: new instance of */ defaultFormatterClass); try { String enc = LogManager.getLogManager().getProperty(propertyPrefix + ".encoding"); /* make sure enc actually is a valid encoding */ if ((enc != null) && (enc.length() > 0)) new String(new byte[0], enc); this.encoding = enc; } catch (Exception _) { } if (out != null) { try { changeWriter(out, getEncoding()); } catch (UnsupportedEncodingException uex) { /* This should never happen, since the validity of the encoding * name has been checked above. */ throw new RuntimeException(uex.getMessage()); } } } private void checkOpen() { if (streamState == STATE_CLOSED) throw new IllegalStateException(this.toString() + " has been closed"); } private void checkFresh() { checkOpen(); if (streamState != STATE_FRESH) throw new IllegalStateException("some log records have been published to " + this); } private void changeWriter(OutputStream out, String encoding) throws UnsupportedEncodingException { OutputStreamWriter writer; /* The logging API says that a null encoding means the default * platform encoding. However, java.io.OutputStreamWriter needs * another constructor for the default platform encoding, * passing null would throw an exception. */ if (encoding == null) writer = new OutputStreamWriter(out); else writer = new OutputStreamWriter(out, encoding); /* Closing the stream has side effects -- do this only after * creating a new writer has been successful. */ if ((streamState != STATE_FRESH) || (this.writer != null)) close(); this.writer = writer; this.out = out; this.encoding = encoding; streamState = STATE_FRESH; } /** * Sets the character encoding which this handler uses for publishing * log records. The encoding of a <code>StreamHandler</code> must be * set before any log records have been published. * * @param encoding the name of a character encoding, or <code>null</code> * for the default encoding. * * @throws SecurityException if a security manager exists and * the caller is not granted the permission to control the * the logging infrastructure. * * @exception IllegalStateException if any log records have been * published to this <code>StreamHandler</code> before. Please * be aware that this is a pecularity of the GNU implementation. * While the API specification indicates that it is an error * if the encoding is set after records have been published, * it does not mandate any specific behavior for that case. */ public void setEncoding(String encoding) throws SecurityException, UnsupportedEncodingException { /* The inherited implementation first checks whether the invoking * code indeed has the permission to control the logging infra- * structure, and throws a SecurityException if this was not the * case. * * Next, it verifies that the encoding is supported and throws * an UnsupportedEncodingExcpetion otherwise. Finally, it remembers * the name of the encoding. */ super.setEncoding(encoding); checkFresh(); /* If out is null, setEncoding is being called before an output * stream has been set. In that case, we need to check that the * encoding is valid, and remember it if this is the case. Since * this is exactly what the inherited implementation of * Handler.setEncoding does, we can delegate. */ if (out != null) { /* The logging API says that a null encoding means the default
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -