1. /*
  2. * @(#)LogStream.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.rmi.server;
  8. import java.io.*;
  9. import java.util.*;
  10. /**
  11. * <code>LogStream</code> provides a mechanism for logging errors that are
  12. * of possible interest to those monitoring a system.
  13. *
  14. * @version 1.12, 11/29/01
  15. * @author Ann Wollrath (lots of code stolen from Ken Arnold)
  16. * @since JDK1.1
  17. * @deprecated no replacement
  18. */
  19. public class LogStream extends PrintStream {
  20. /** table mapping known log names to log stream objects */
  21. private static Hashtable known = new Hashtable(5);
  22. /** default output stream for new logs */
  23. private static PrintStream defaultStream = System.err;
  24. /** log name for this log */
  25. private String name;
  26. /** stream where output of this log is sent to */
  27. private OutputStream logOut;
  28. /** string writer for writing message prefixes to log stream */
  29. private OutputStreamWriter logWriter;
  30. /** string buffer used for constructing log message prefixes */
  31. private StringBuffer buffer = new StringBuffer();
  32. /** stream used for buffering lines */
  33. private ByteArrayOutputStream bufOut;
  34. /**
  35. * Create a new LogStream object. Since this only constructor is
  36. * private, users must have a LogStream created through the "log"
  37. * method.
  38. * @param name string identifying messages from this log
  39. * @out output stream that log messages will be sent to
  40. * @since JDK1.1
  41. * @deprecated no replacement
  42. */
  43. private LogStream(String name, OutputStream out)
  44. {
  45. super(new ByteArrayOutputStream());
  46. bufOut = (ByteArrayOutputStream) super.out;
  47. this.name = name;
  48. setOutputStream(out);
  49. }
  50. /**
  51. * Return the LogStream identified by the given name. If
  52. * a log corresponding to "name" does not exist, a log using
  53. * the default stream is created.
  54. * @since JDK1.1
  55. * @deprecated no replacement
  56. */
  57. public static LogStream log(String name) {
  58. LogStream stream;
  59. synchronized (known) {
  60. stream = (LogStream)known.get(name);
  61. if (stream == null) {
  62. stream = new LogStream(name, defaultStream);
  63. }
  64. known.put(name, stream);
  65. }
  66. return stream;
  67. }
  68. /**
  69. * Return the current default stream for new logs.
  70. * @since JDK1.1
  71. * @deprecated no replacement
  72. */
  73. public static synchronized PrintStream getDefaultStream() {
  74. return defaultStream;
  75. }
  76. /**
  77. * Set the default stream for new logs.
  78. * @since JDK1.1
  79. * @deprecated no replacement
  80. */
  81. public static synchronized void setDefaultStream(PrintStream newDefault) {
  82. defaultStream = newDefault;
  83. }
  84. /**
  85. * Return the current stream to which output from this log is sent.
  86. * @since JDK1.1
  87. * @deprecated no replacement
  88. */
  89. public synchronized OutputStream getOutputStream()
  90. {
  91. return logOut;
  92. }
  93. /**
  94. * Set the stream to which output from this log is sent.
  95. * @since JDK1.1
  96. * @deprecated no replacement
  97. */
  98. public synchronized void setOutputStream(OutputStream out)
  99. {
  100. logOut = out;
  101. // Maintain an OutputStreamWriter with default CharToByteConvertor
  102. // (just like new PrintStream) for writing log message prefixes.
  103. logWriter = new OutputStreamWriter(logOut);
  104. }
  105. /**
  106. * Write a byte of data to the stream. If it is not a newline, then
  107. * the byte is appended to the internal buffer. If it is a newline,
  108. * then the currently buffered line is sent to the log's output
  109. * stream, prefixed with the appropriate logging information.
  110. * @since JDK1.1
  111. * @deprecated no replacement
  112. */
  113. public void write(int b)
  114. {
  115. if (b == '\n') {
  116. // synchronize on "this" first to avoid potential deadlock
  117. synchronized (this) {
  118. synchronized (logOut) {
  119. // construct prefix for log messages:
  120. buffer.setLength(0);;
  121. buffer.append( // date/time stamp...
  122. (new Date()).toString());
  123. buffer.append(':');
  124. buffer.append(name); // ...log name...
  125. buffer.append(':');
  126. buffer.append(Thread.currentThread().getName());
  127. buffer.append(':'); // ...and thread name
  128. try {
  129. // write prefix through to underlying byte stream
  130. logWriter.write(buffer.toString());
  131. logWriter.flush();
  132. // finally, write the already converted bytes of
  133. // the log message
  134. bufOut.writeTo(logOut);
  135. logOut.write(b);
  136. logOut.flush();
  137. } catch (IOException e) {
  138. setError();
  139. } finally {
  140. bufOut.reset();
  141. }
  142. }
  143. }
  144. }
  145. else
  146. super.write(b);
  147. }
  148. /**
  149. * Write a subarray of bytes. Pass each through write byte method.
  150. * @since JDK1.1
  151. * @deprecated no replacement
  152. */
  153. public void write(byte b[], int off, int len)
  154. {
  155. if (len < 0)
  156. throw new ArrayIndexOutOfBoundsException(len);
  157. for (int i = 0; i < len; ++ i)
  158. write(b[off + i]);
  159. }
  160. /**
  161. * Return log name as string representation
  162. * @since JDK1.1
  163. * @deprecated no replacement
  164. */
  165. public String toString()
  166. {
  167. return name;
  168. }
  169. /** log level constant (no logging) */
  170. public static final int SILENT = 0;
  171. /** log level constant (brief logging) */
  172. public static final int BRIEF = 10;
  173. /** log level constant (verbose logging) */
  174. public static final int VERBOSE = 20;
  175. /**
  176. * Convert a string name of a logging level to its internal
  177. * integer representation.
  178. * @since JDK1.1
  179. * @deprecated no replacement
  180. */
  181. public static int parseLevel(String s)
  182. {
  183. if ((s == null) || (s.length() < 1))
  184. return -1;
  185. try {
  186. return Integer.parseInt(s);
  187. } catch (NumberFormatException e) {
  188. }
  189. if (s.length() < 1)
  190. return -1;
  191. if ("SILENT".startsWith(s.toUpperCase()))
  192. return SILENT;
  193. else if ("BRIEF".startsWith(s.toUpperCase()))
  194. return BRIEF;
  195. else if ("VERBOSE".startsWith(s.toUpperCase()))
  196. return VERBOSE;
  197. return -1;
  198. }
  199. }