1 /* 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.rmi.server; 26 27 import java.io.*; 28 import java.util.*; 29 30 /** 31 * <code>LogStream</code> provides a mechanism for logging errors that are 32 * of possible interest to those monitoring a system. 33 * 34 * @author Ann Wollrath (lots of code stolen from Ken Arnold) 35 * @since JDK1.1 36 * @deprecated no replacement 37 */ 38 @Deprecated 39 public class LogStream extends PrintStream { 40 41 /** table mapping known log names to log stream objects */ 42 private static Map<String,LogStream> known = new HashMap<>(5); 43 /** default output stream for new logs */ 44 private static PrintStream defaultStream = System.err; 45 46 /** log name for this log */ 47 private String name; 48 49 /** stream where output of this log is sent to */ 50 private OutputStream logOut; 51 52 /** string writer for writing message prefixes to log stream */ 53 private OutputStreamWriter logWriter; 54 55 /** string buffer used for constructing log message prefixes */ 56 private StringBuffer buffer = new StringBuffer(); 57 58 /** stream used for buffering lines */ 59 private ByteArrayOutputStream bufOut; 60 61 /** 62 * Create a new LogStream object. Since this only constructor is 63 * private, users must have a LogStream created through the "log" 64 * method. 65 * @param name string identifying messages from this log 66 * @out output stream that log messages will be sent to 67 * @since JDK1.1 68 * @deprecated no replacement 69 */ 70 @Deprecated LogStream(String name, OutputStream out)71 private LogStream(String name, OutputStream out) 72 { 73 super(new ByteArrayOutputStream()); 74 bufOut = (ByteArrayOutputStream) super.out; 75 76 this.name = name; 77 setOutputStream(out); 78 } 79 80 /** 81 * Return the LogStream identified by the given name. If 82 * a log corresponding to "name" does not exist, a log using 83 * the default stream is created. 84 * @param name name identifying the desired LogStream 85 * @return log associated with given name 86 * @since JDK1.1 87 * @deprecated no replacement 88 */ 89 @Deprecated log(String name)90 public static LogStream log(String name) { 91 LogStream stream; 92 synchronized (known) { 93 stream = known.get(name); 94 if (stream == null) { 95 stream = new LogStream(name, defaultStream); 96 } 97 known.put(name, stream); 98 } 99 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 getDefaultStream()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 setDefaultStream(PrintStream newDefault)122 public static synchronized void setDefaultStream(PrintStream newDefault) { 123 SecurityManager sm = System.getSecurityManager(); 124 125 if (sm != null) { 126 sm.checkPermission( 127 new java.util.logging.LoggingPermission("control", null)); 128 } 129 130 defaultStream = newDefault; 131 } 132 133 /** 134 * Return the current stream to which output from this log is sent. 135 * @return output stream for this log 136 * @see #setOutputStream 137 * @since JDK1.1 138 * @deprecated no replacement 139 */ 140 @Deprecated getOutputStream()141 public synchronized OutputStream getOutputStream() 142 { 143 return logOut; 144 } 145 146 /** 147 * Set the stream to which output from this log is sent. 148 * @param out new output stream for this log 149 * @see #getOutputStream 150 * @since JDK1.1 151 * @deprecated no replacement 152 */ 153 @Deprecated setOutputStream(OutputStream out)154 public synchronized void setOutputStream(OutputStream out) 155 { 156 logOut = out; 157 // Maintain an OutputStreamWriter with default CharToByteConvertor 158 // (just like new PrintStream) for writing log message prefixes. 159 logWriter = new OutputStreamWriter(logOut); 160 } 161 162 /** 163 * Write a byte of data to the stream. If it is not a newline, then 164 * the byte is appended to the internal buffer. If it is a newline, 165 * then the currently buffered line is sent to the log's output 166 * stream, prefixed with the appropriate logging information. 167 * @since JDK1.1 168 * @deprecated no replacement 169 */ 170 @Deprecated write(int b)171 public void write(int b) 172 { 173 if (b == '\n') { 174 // synchronize on "this" first to avoid potential deadlock 175 synchronized (this) { 176 synchronized (logOut) { 177 // construct prefix for log messages: 178 buffer.setLength(0);; 179 buffer.append( // date/time stamp... 180 (new Date()).toString()); 181 buffer.append(':'); 182 buffer.append(name); // ...log name... 183 buffer.append(':'); 184 buffer.append(Thread.currentThread().getName()); 185 buffer.append(':'); // ...and thread name 186 187 try { 188 // write prefix through to underlying byte stream 189 logWriter.write(buffer.toString()); 190 logWriter.flush(); 191 192 // finally, write the already converted bytes of 193 // the log message 194 bufOut.writeTo(logOut); 195 logOut.write(b); 196 logOut.flush(); 197 } catch (IOException e) { 198 setError(); 199 } finally { 200 bufOut.reset(); 201 } 202 } 203 } 204 } 205 else 206 super.write(b); 207 } 208 209 /** 210 * Write a subarray of bytes. Pass each through write byte method. 211 * @since JDK1.1 212 * @deprecated no replacement 213 */ 214 @Deprecated write(byte b[], int off, int len)215 public void write(byte b[], int off, int len) 216 { 217 if (len < 0) 218 throw new ArrayIndexOutOfBoundsException(len); 219 for (int i = 0; i < len; ++ i) 220 write(b[off + i]); 221 } 222 223 /** 224 * Return log name as string representation. 225 * @return log name 226 * @since JDK1.1 227 * @deprecated no replacement 228 */ 229 @Deprecated toString()230 public String toString() 231 { 232 return name; 233 } 234 235 /** log level constant (no logging). */ 236 public static final int SILENT = 0; 237 /** log level constant (brief logging). */ 238 public static final int BRIEF = 10; 239 /** log level constant (verbose logging). */ 240 public static final int VERBOSE = 20; 241 242 /** 243 * Convert a string name of a logging level to its internal 244 * integer representation. 245 * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE') 246 * @return corresponding integer log level 247 * @since JDK1.1 248 * @deprecated no replacement 249 */ 250 @Deprecated parseLevel(String s)251 public static int parseLevel(String s) 252 { 253 if ((s == null) || (s.length() < 1)) 254 return -1; 255 256 try { 257 return Integer.parseInt(s); 258 } catch (NumberFormatException e) { 259 } 260 if (s.length() < 1) 261 return -1; 262 263 if ("SILENT".startsWith(s.toUpperCase())) 264 return SILENT; 265 else if ("BRIEF".startsWith(s.toUpperCase())) 266 return BRIEF; 267 else if ("VERBOSE".startsWith(s.toUpperCase())) 268 return VERBOSE; 269 270 return -1; 271 } 272 } 273