001 /*
002 * Copyright 1996-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.rmi.server;
026
027 import java.io.*;
028 import java.util.*;
029
030 /**
031 * <code>LogStream</code> provides a mechanism for logging errors that are
032 * of possible interest to those monitoring a system.
033 *
034 * @version 1.27, 05/05/07
035 * @author Ann Wollrath (lots of code stolen from Ken Arnold)
036 * @since JDK1.1
037 * @deprecated no replacement
038 */
039 @Deprecated
040 public class LogStream extends PrintStream {
041
042 /** table mapping known log names to log stream objects */
043 private static Hashtable known = new Hashtable(5);
044 /** default output stream for new logs */
045 private static PrintStream defaultStream = System.err;
046
047 /** log name for this log */
048 private String name;
049
050 /** stream where output of this log is sent to */
051 private OutputStream logOut;
052
053 /** string writer for writing message prefixes to log stream */
054 private OutputStreamWriter logWriter;
055
056 /** string buffer used for constructing log message prefixes */
057 private StringBuffer buffer = new StringBuffer();
058
059 /** stream used for buffering lines */
060 private ByteArrayOutputStream bufOut;
061
062 /**
063 * Create a new LogStream object. Since this only constructor is
064 * private, users must have a LogStream created through the "log"
065 * method.
066 * @param name string identifying messages from this log
067 * @out output stream that log messages will be sent to
068 * @since JDK1.1
069 * @deprecated no replacement
070 */
071 @Deprecated
072 private LogStream(String name, OutputStream out) {
073 super (new ByteArrayOutputStream());
074 bufOut = (ByteArrayOutputStream) super .out;
075
076 this .name = name;
077 setOutputStream(out);
078 }
079
080 /**
081 * Return the LogStream identified by the given name. If
082 * a log corresponding to "name" does not exist, a log using
083 * the default stream is created.
084 * @param name name identifying the desired LogStream
085 * @return log associated with given name
086 * @since JDK1.1
087 * @deprecated no replacement
088 */
089 @Deprecated
090 public static LogStream log(String name) {
091 LogStream stream;
092 synchronized (known) {
093 stream = (LogStream) known.get(name);
094 if (stream == null) {
095 stream = new LogStream(name, defaultStream);
096 }
097 known.put(name, stream);
098 }
099 return stream;
100 }
101
102 /**
103 * Return the current default stream for new logs.
104 * @return default log stream
105 * @see #setDefaultStream
106 * @since JDK1.1
107 * @deprecated no replacement
108 */
109 @Deprecated
110 public static synchronized PrintStream getDefaultStream() {
111 return defaultStream;
112 }
113
114 /**
115 * Set the default stream for new logs.
116 * @param newDefault new default log stream
117 * @see #getDefaultStream
118 * @since JDK1.1
119 * @deprecated no replacement
120 */
121 @Deprecated
122 public static synchronized void setDefaultStream(
123 PrintStream newDefault) {
124 defaultStream = newDefault;
125 }
126
127 /**
128 * Return the current stream to which output from this log is sent.
129 * @return output stream for this log
130 * @see #setOutputStream
131 * @since JDK1.1
132 * @deprecated no replacement
133 */
134 @Deprecated
135 public synchronized OutputStream getOutputStream() {
136 return logOut;
137 }
138
139 /**
140 * Set the stream to which output from this log is sent.
141 * @param out new output stream for this log
142 * @see #getOutputStream
143 * @since JDK1.1
144 * @deprecated no replacement
145 */
146 @Deprecated
147 public synchronized void setOutputStream(OutputStream out) {
148 logOut = out;
149 // Maintain an OutputStreamWriter with default CharToByteConvertor
150 // (just like new PrintStream) for writing log message prefixes.
151 logWriter = new OutputStreamWriter(logOut);
152 }
153
154 /**
155 * Write a byte of data to the stream. If it is not a newline, then
156 * the byte is appended to the internal buffer. If it is a newline,
157 * then the currently buffered line is sent to the log's output
158 * stream, prefixed with the appropriate logging information.
159 * @since JDK1.1
160 * @deprecated no replacement
161 */
162 @Deprecated
163 public void write(int b) {
164 if (b == '\n') {
165 // synchronize on "this" first to avoid potential deadlock
166 synchronized (this ) {
167 synchronized (logOut) {
168 // construct prefix for log messages:
169 buffer.setLength(0);
170 ;
171 buffer.append( // date/time stamp...
172 (new Date()).toString());
173 buffer.append(':');
174 buffer.append(name); // ...log name...
175 buffer.append(':');
176 buffer.append(Thread.currentThread().getName());
177 buffer.append(':'); // ...and thread name
178
179 try {
180 // write prefix through to underlying byte stream
181 logWriter.write(buffer.toString());
182 logWriter.flush();
183
184 // finally, write the already converted bytes of
185 // the log message
186 bufOut.writeTo(logOut);
187 logOut.write(b);
188 logOut.flush();
189 } catch (IOException e) {
190 setError();
191 } finally {
192 bufOut.reset();
193 }
194 }
195 }
196 } else
197 super .write(b);
198 }
199
200 /**
201 * Write a subarray of bytes. Pass each through write byte method.
202 * @since JDK1.1
203 * @deprecated no replacement
204 */
205 @Deprecated
206 public void write(byte b[], int off, int len) {
207 if (len < 0)
208 throw new ArrayIndexOutOfBoundsException(len);
209 for (int i = 0; i < len; ++i)
210 write(b[off + i]);
211 }
212
213 /**
214 * Return log name as string representation.
215 * @return log name
216 * @since JDK1.1
217 * @deprecated no replacement
218 */
219 @Deprecated
220 public String toString() {
221 return name;
222 }
223
224 /** log level constant (no logging). */
225 public static final int SILENT = 0;
226 /** log level constant (brief logging). */
227 public static final int BRIEF = 10;
228 /** log level constant (verbose logging). */
229 public static final int VERBOSE = 20;
230
231 /**
232 * Convert a string name of a logging level to its internal
233 * integer representation.
234 * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
235 * @return corresponding integer log level
236 * @since JDK1.1
237 * @deprecated no replacement
238 */
239 @Deprecated
240 public static int parseLevel(String s) {
241 if ((s == null) || (s.length() < 1))
242 return -1;
243
244 try {
245 return Integer.parseInt(s);
246 } catch (NumberFormatException e) {
247 }
248 if (s.length() < 1)
249 return -1;
250
251 if ("SILENT".startsWith(s.toUpperCase()))
252 return SILENT;
253 else if ("BRIEF".startsWith(s.toUpperCase()))
254 return BRIEF;
255 else if ("VERBOSE".startsWith(s.toUpperCase()))
256 return VERBOSE;
257
258 return -1;
259 }
260 }
|