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