KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > rmi > server > LogStream


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
9 import java.io.*;
10 import java.util.*;
11
12 /**
13  * <code>LogStream</code> provides a mechanism for logging errors that are
14  * of possible interest to those monitoring a system.
15  *
16  * @version 1.20, 05/18/04
17  * @author Ann Wollrath (lots of code stolen from Ken Arnold)
18  * @since JDK1.1
19  * @deprecated no replacement
20  */

21 @Deprecated JavaDoc
22 public class LogStream extends PrintStream {
23
24     /** table mapping known log names to log stream objects */
25     private static Hashtable known = new Hashtable(5);
26     /** default output stream for new logs */
27     private static PrintStream defaultStream = System.err;
28
29     /** log name for this log */
30     private String JavaDoc name;
31
32     /** stream where output of this log is sent to */
33     private OutputStream logOut;
34
35     /** string writer for writing message prefixes to log stream */
36     private OutputStreamWriter logWriter;
37
38     /** string buffer used for constructing log message prefixes */
39     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
40
41     /** stream used for buffering lines */
42     private ByteArrayOutputStream bufOut;
43
44     /**
45      * Create a new LogStream object. Since this only constructor is
46      * private, users must have a LogStream created through the "log"
47      * method.
48      * @param name string identifying messages from this log
49      * @out output stream that log messages will be sent to
50      * @since JDK1.1
51      * @deprecated no replacement
52      */

53     @Deprecated JavaDoc
54     private LogStream(String JavaDoc name, OutputStream out)
55     {
56     super(new ByteArrayOutputStream());
57     bufOut = (ByteArrayOutputStream) super.out;
58
59     this.name = name;
60     setOutputStream(out);
61     }
62
63     /**
64      * Return the LogStream identified by the given name. If
65      * a log corresponding to "name" does not exist, a log using
66      * the default stream is created.
67      * @param name name identifying the desired LogStream
68      * @return log associated with given name
69      * @since JDK1.1
70      * @deprecated no replacement
71      */

72     @Deprecated JavaDoc
73     public static LogStream JavaDoc log(String JavaDoc name) {
74     LogStream JavaDoc stream;
75     synchronized (known) {
76         stream = (LogStream JavaDoc)known.get(name);
77         if (stream == null) {
78         stream = new LogStream JavaDoc(name, defaultStream);
79         }
80         known.put(name, stream);
81     }
82     return stream;
83     }
84
85     /**
86      * Return the current default stream for new logs.
87      * @return default log stream
88      * @see #setDefaultStream
89      * @since JDK1.1
90      * @deprecated no replacement
91      */

92     @Deprecated JavaDoc
93     public static synchronized PrintStream getDefaultStream() {
94     return defaultStream;
95     }
96
97     /**
98      * Set the default stream for new logs.
99      * @param newDefault new default log stream
100      * @see #getDefaultStream
101      * @since JDK1.1
102      * @deprecated no replacement
103      */

104     @Deprecated JavaDoc
105     public static synchronized void setDefaultStream(PrintStream newDefault) {
106     defaultStream = newDefault;
107     }
108
109     /**
110      * Return the current stream to which output from this log is sent.
111      * @return output stream for this log
112      * @see #setOutputStream
113      * @since JDK1.1
114      * @deprecated no replacement
115      */

116     @Deprecated JavaDoc
117     public synchronized OutputStream getOutputStream()
118     {
119     return logOut;
120     }
121     
122     /**
123      * Set the stream to which output from this log is sent.
124      * @param out new output stream for this log
125      * @see #getOutputStream
126      * @since JDK1.1
127      * @deprecated no replacement
128      */

129     @Deprecated JavaDoc
130     public synchronized void setOutputStream(OutputStream out)
131     {
132     logOut = out;
133     // Maintain an OutputStreamWriter with default CharToByteConvertor
134
// (just like new PrintStream) for writing log message prefixes.
135
logWriter = new OutputStreamWriter(logOut);
136     }
137     
138     /**
139      * Write a byte of data to the stream. If it is not a newline, then
140      * the byte is appended to the internal buffer. If it is a newline,
141      * then the currently buffered line is sent to the log's output
142      * stream, prefixed with the appropriate logging information.
143      * @since JDK1.1
144      * @deprecated no replacement
145      */

146     @Deprecated JavaDoc
147     public void write(int b)
148     {
149     if (b == '\n') {
150         // synchronize on "this" first to avoid potential deadlock
151
synchronized (this) {
152         synchronized (logOut) {
153             // construct prefix for log messages:
154
buffer.setLength(0);;
155             buffer.append( // date/time stamp...
156
(new Date()).toString());
157             buffer.append(':');
158             buffer.append(name); // ...log name...
159
buffer.append(':');
160             buffer.append(Thread.currentThread().getName());
161             buffer.append(':'); // ...and thread name
162

163             try {
164             // write prefix through to underlying byte stream
165
logWriter.write(buffer.toString());
166             logWriter.flush();
167
168             // finally, write the already converted bytes of
169
// the log message
170
bufOut.writeTo(logOut);
171             logOut.write(b);
172             logOut.flush();
173             } catch (IOException e) {
174             setError();
175             } finally {
176             bufOut.reset();
177             }
178         }
179         }
180     }
181     else
182         super.write(b);
183     }
184
185     /**
186      * Write a subarray of bytes. Pass each through write byte method.
187      * @since JDK1.1
188      * @deprecated no replacement
189      */

190     @Deprecated JavaDoc
191     public void write(byte b[], int off, int len)
192     {
193     if (len < 0)
194         throw new ArrayIndexOutOfBoundsException JavaDoc(len);
195     for (int i = 0; i < len; ++ i)
196         write(b[off + i]);
197     }
198
199     /**
200      * Return log name as string representation.
201      * @return log name
202      * @since JDK1.1
203      * @deprecated no replacement
204      */

205     @Deprecated JavaDoc
206     public String JavaDoc toString()
207     {
208     return name;
209     }
210
211     /** log level constant (no logging). */
212     public static final int SILENT = 0;
213     /** log level constant (brief logging). */
214     public static final int BRIEF = 10;
215     /** log level constant (verbose logging). */
216     public static final int VERBOSE = 20;
217
218     /**
219      * Convert a string name of a logging level to its internal
220      * integer representation.
221      * @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE')
222      * @return corresponding integer log level
223      * @since JDK1.1
224      * @deprecated no replacement
225      */

226     @Deprecated JavaDoc
227     public static int parseLevel(String JavaDoc s)
228     {
229     if ((s == null) || (s.length() < 1))
230         return -1;
231
232     try {
233         return Integer.parseInt(s);
234     } catch (NumberFormatException JavaDoc e) {
235     }
236     if (s.length() < 1)
237         return -1;
238
239     if ("SILENT".startsWith(s.toUpperCase()))
240         return SILENT;
241     else if ("BRIEF".startsWith(s.toUpperCase()))
242         return BRIEF;
243     else if ("VERBOSE".startsWith(s.toUpperCase()))
244         return VERBOSE;
245
246     return -1;
247     }
248 }
249
Popular Tags