1. /*
  2. * @(#)LogStream.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 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.18, 01/23/03
  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. * @param name name identifying the desired LogStream
  55. * @return log associated with given name
  56. * @since JDK1.1
  57. * @deprecated no replacement
  58. */
  59. public static LogStream log(String name) {
  60. LogStream stream;
  61. synchronized (known) {
  62. stream = (LogStream)known.get(name);
  63. if (stream == null) {
  64. stream = new LogStream(name, defaultStream);
  65. }
  66. known.put(name, stream);
  67. }
  68. return stream;
  69. }
  70. /**
  71. * Return the current default stream for new logs.
  72. * @return default log stream
  73. * @see #setDefaultStream
  74. * @since JDK1.1
  75. * @deprecated no replacement
  76. */
  77. public static synchronized PrintStream getDefaultStream() {
  78. return defaultStream;
  79. }
  80. /**
  81. * Set the default stream for new logs.
  82. * @param newDefault new default log stream
  83. * @see #getDefaultStream
  84. * @since JDK1.1
  85. * @deprecated no replacement
  86. */
  87. public static synchronized void setDefaultStream(PrintStream newDefault) {
  88. defaultStream = newDefault;
  89. }
  90. /**
  91. * Return the current stream to which output from this log is sent.
  92. * @return output stream for this log
  93. * @see #setOutputStream
  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. * @see #getOutputStream
  105. * @since JDK1.1
  106. * @deprecated no replacement
  107. */
  108. public synchronized void setOutputStream(OutputStream out)
  109. {
  110. logOut = out;
  111. // Maintain an OutputStreamWriter with default CharToByteConvertor
  112. // (just like new PrintStream) for writing log message prefixes.
  113. logWriter = new OutputStreamWriter(logOut);
  114. }
  115. /**
  116. * Write a byte of data to the stream. If it is not a newline, then
  117. * the byte is appended to the internal buffer. If it is a newline,
  118. * then the currently buffered line is sent to the log's output
  119. * stream, prefixed with the appropriate logging information.
  120. * @since JDK1.1
  121. * @deprecated no replacement
  122. */
  123. public void write(int b)
  124. {
  125. if (b == '\n') {
  126. // synchronize on "this" first to avoid potential deadlock
  127. synchronized (this) {
  128. synchronized (logOut) {
  129. // construct prefix for log messages:
  130. buffer.setLength(0);;
  131. buffer.append( // date/time stamp...
  132. (new Date()).toString());
  133. buffer.append(':');
  134. buffer.append(name); // ...log name...
  135. buffer.append(':');
  136. buffer.append(Thread.currentThread().getName());
  137. buffer.append(':'); // ...and thread name
  138. try {
  139. // write prefix through to underlying byte stream
  140. logWriter.write(buffer.toString());
  141. logWriter.flush();
  142. // finally, write the already converted bytes of
  143. // the log message
  144. bufOut.writeTo(logOut);
  145. logOut.write(b);
  146. logOut.flush();
  147. } catch (IOException e) {
  148. setError();
  149. } finally {
  150. bufOut.reset();
  151. }
  152. }
  153. }
  154. }
  155. else
  156. super.write(b);
  157. }
  158. /**
  159. * Write a subarray of bytes. Pass each through write byte method.
  160. * @since JDK1.1
  161. * @deprecated no replacement
  162. */
  163. public void write(byte b[], int off, int len)
  164. {
  165. if (len < 0)
  166. throw new ArrayIndexOutOfBoundsException(len);
  167. for (int i = 0; i < len; ++ i)
  168. write(b[off + i]);
  169. }
  170. /**
  171. * Return log name as string representation.
  172. * @return log name
  173. * @since JDK1.1
  174. * @deprecated no replacement
  175. */
  176. public String toString()
  177. {
  178. return name;
  179. }
  180. /** log level constant (no logging). */
  181. public static final int SILENT = 0;
  182. /** log level constant (brief logging). */
  183. public static final int BRIEF = 10;
  184. /** log level constant (verbose logging). */
  185. public static final int VERBOSE = 20;
  186. /**
  187. * Convert a string name of a logging level to its internal
  188. * integer representation.
  189. * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
  190. * @return corresponding integer log level
  191. * @since JDK1.1
  192. * @deprecated no replacement
  193. */
  194. public static int parseLevel(String s)
  195. {
  196. if ((s == null) || (s.length() < 1))
  197. return -1;
  198. try {
  199. return Integer.parseInt(s);
  200. } catch (NumberFormatException e) {
  201. }
  202. if (s.length() < 1)
  203. return -1;
  204. if ("SILENT".startsWith(s.toUpperCase()))
  205. return SILENT;
  206. else if ("BRIEF".startsWith(s.toUpperCase()))
  207. return BRIEF;
  208. else if ("VERBOSE".startsWith(s.toUpperCase()))
  209. return VERBOSE;
  210. return -1;
  211. }
  212. }