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