0N/A/*
5880N/A * Copyright (c) 1996, 2013, 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/Apackage java.rmi.server;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * <code>LogStream</code> provides a mechanism for logging errors that are
0N/A * of possible interest to those monitoring a system.
0N/A *
0N/A * @author Ann Wollrath (lots of code stolen from Ken Arnold)
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A@Deprecated
0N/Apublic class LogStream extends PrintStream {
0N/A
0N/A /** table mapping known log names to log stream objects */
0N/A private static Hashtable known = new Hashtable(5);
0N/A /** default output stream for new logs */
0N/A private static PrintStream defaultStream = System.err;
0N/A
0N/A /** log name for this log */
0N/A private String name;
0N/A
0N/A /** stream where output of this log is sent to */
0N/A private OutputStream logOut;
0N/A
0N/A /** string writer for writing message prefixes to log stream */
0N/A private OutputStreamWriter logWriter;
0N/A
0N/A /** string buffer used for constructing log message prefixes */
0N/A private StringBuffer buffer = new StringBuffer();
0N/A
0N/A /** stream used for buffering lines */
0N/A private ByteArrayOutputStream bufOut;
0N/A
0N/A /**
0N/A * Create a new LogStream object. Since this only constructor is
0N/A * private, users must have a LogStream created through the "log"
0N/A * method.
0N/A * @param name string identifying messages from this log
0N/A * @out output stream that log messages will be sent to
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A private LogStream(String name, OutputStream out)
0N/A {
0N/A super(new ByteArrayOutputStream());
0N/A bufOut = (ByteArrayOutputStream) super.out;
0N/A
0N/A this.name = name;
0N/A setOutputStream(out);
0N/A }
0N/A
0N/A /**
0N/A * Return the LogStream identified by the given name. If
0N/A * a log corresponding to "name" does not exist, a log using
0N/A * the default stream is created.
0N/A * @param name name identifying the desired LogStream
0N/A * @return log associated with given name
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public static LogStream log(String name) {
0N/A LogStream stream;
0N/A synchronized (known) {
0N/A stream = (LogStream)known.get(name);
0N/A if (stream == null) {
0N/A stream = new LogStream(name, defaultStream);
0N/A }
0N/A known.put(name, stream);
0N/A }
0N/A return stream;
0N/A }
0N/A
0N/A /**
0N/A * Return the current default stream for new logs.
0N/A * @return default log stream
0N/A * @see #setDefaultStream
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public static synchronized PrintStream getDefaultStream() {
0N/A return defaultStream;
0N/A }
0N/A
0N/A /**
0N/A * Set the default stream for new logs.
0N/A * @param newDefault new default log stream
0N/A * @see #getDefaultStream
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public static synchronized void setDefaultStream(PrintStream newDefault) {
5880N/A SecurityManager sm = System.getSecurityManager();
5880N/A
5880N/A if (sm != null) {
5880N/A sm.checkPermission(
5880N/A new java.util.logging.LoggingPermission("control", null));
5880N/A }
5880N/A
0N/A defaultStream = newDefault;
0N/A }
0N/A
0N/A /**
0N/A * Return the current stream to which output from this log is sent.
0N/A * @return output stream for this log
0N/A * @see #setOutputStream
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public synchronized OutputStream getOutputStream()
0N/A {
0N/A return logOut;
0N/A }
0N/A
0N/A /**
0N/A * Set the stream to which output from this log is sent.
0N/A * @param out new output stream for this log
0N/A * @see #getOutputStream
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public synchronized void setOutputStream(OutputStream out)
0N/A {
0N/A logOut = out;
0N/A // Maintain an OutputStreamWriter with default CharToByteConvertor
0N/A // (just like new PrintStream) for writing log message prefixes.
0N/A logWriter = new OutputStreamWriter(logOut);
0N/A }
0N/A
0N/A /**
0N/A * Write a byte of data to the stream. If it is not a newline, then
0N/A * the byte is appended to the internal buffer. If it is a newline,
0N/A * then the currently buffered line is sent to the log's output
0N/A * stream, prefixed with the appropriate logging information.
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public void write(int b)
0N/A {
0N/A if (b == '\n') {
0N/A // synchronize on "this" first to avoid potential deadlock
0N/A synchronized (this) {
0N/A synchronized (logOut) {
0N/A // construct prefix for log messages:
0N/A buffer.setLength(0);;
0N/A buffer.append( // date/time stamp...
0N/A (new Date()).toString());
0N/A buffer.append(':');
0N/A buffer.append(name); // ...log name...
0N/A buffer.append(':');
0N/A buffer.append(Thread.currentThread().getName());
0N/A buffer.append(':'); // ...and thread name
0N/A
0N/A try {
0N/A // write prefix through to underlying byte stream
0N/A logWriter.write(buffer.toString());
0N/A logWriter.flush();
0N/A
0N/A // finally, write the already converted bytes of
0N/A // the log message
0N/A bufOut.writeTo(logOut);
0N/A logOut.write(b);
0N/A logOut.flush();
0N/A } catch (IOException e) {
0N/A setError();
0N/A } finally {
0N/A bufOut.reset();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A else
0N/A super.write(b);
0N/A }
0N/A
0N/A /**
0N/A * Write a subarray of bytes. Pass each through write byte method.
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public void write(byte b[], int off, int len)
0N/A {
0N/A if (len < 0)
0N/A throw new ArrayIndexOutOfBoundsException(len);
0N/A for (int i = 0; i < len; ++ i)
0N/A write(b[off + i]);
0N/A }
0N/A
0N/A /**
0N/A * Return log name as string representation.
0N/A * @return log name
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public String toString()
0N/A {
0N/A return name;
0N/A }
0N/A
0N/A /** log level constant (no logging). */
0N/A public static final int SILENT = 0;
0N/A /** log level constant (brief logging). */
0N/A public static final int BRIEF = 10;
0N/A /** log level constant (verbose logging). */
0N/A public static final int VERBOSE = 20;
0N/A
0N/A /**
0N/A * Convert a string name of a logging level to its internal
0N/A * integer representation.
0N/A * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
0N/A * @return corresponding integer log level
0N/A * @since JDK1.1
0N/A * @deprecated no replacement
0N/A */
0N/A @Deprecated
0N/A public static int parseLevel(String s)
0N/A {
0N/A if ((s == null) || (s.length() < 1))
0N/A return -1;
0N/A
0N/A try {
0N/A return Integer.parseInt(s);
0N/A } catch (NumberFormatException e) {
0N/A }
0N/A if (s.length() < 1)
0N/A return -1;
0N/A
0N/A if ("SILENT".startsWith(s.toUpperCase()))
0N/A return SILENT;
0N/A else if ("BRIEF".startsWith(s.toUpperCase()))
0N/A return BRIEF;
0N/A else if ("VERBOSE".startsWith(s.toUpperCase()))
0N/A return VERBOSE;
0N/A
0N/A return -1;
0N/A }
0N/A}