author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16874 | 2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.rmi.server; |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.util.*; |
|
29 |
||
30 |
/** |
|
31 |
* <code>LogStream</code> provides a mechanism for logging errors that are |
|
32 |
* of possible interest to those monitoring a system. |
|
33 |
* |
|
34 |
* @author Ann Wollrath (lots of code stolen from Ken Arnold) |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
35 |
* @since 1.1 |
2 | 36 |
* @deprecated no replacement |
37 |
*/ |
|
38 |
@Deprecated |
|
39 |
public class LogStream extends PrintStream { |
|
40 |
||
41 |
/** table mapping known log names to log stream objects */ |
|
11117
b6e68b1344d4
7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents:
5506
diff
changeset
|
42 |
private static Map<String,LogStream> known = new HashMap<>(5); |
2 | 43 |
/** default output stream for new logs */ |
44 |
private static PrintStream defaultStream = System.err; |
|
45 |
||
46 |
/** log name for this log */ |
|
47 |
private String name; |
|
48 |
||
49 |
/** stream where output of this log is sent to */ |
|
50 |
private OutputStream logOut; |
|
51 |
||
52 |
/** string writer for writing message prefixes to log stream */ |
|
53 |
private OutputStreamWriter logWriter; |
|
54 |
||
55 |
/** string buffer used for constructing log message prefixes */ |
|
56 |
private StringBuffer buffer = new StringBuffer(); |
|
57 |
||
58 |
/** stream used for buffering lines */ |
|
59 |
private ByteArrayOutputStream bufOut; |
|
60 |
||
61 |
/** |
|
62 |
* Create a new LogStream object. Since this only constructor is |
|
63 |
* private, users must have a LogStream created through the "log" |
|
64 |
* method. |
|
65 |
* @param name string identifying messages from this log |
|
66 |
* @out output stream that log messages will be sent to |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
67 |
* @since 1.1 |
2 | 68 |
* @deprecated no replacement |
69 |
*/ |
|
70 |
@Deprecated |
|
71 |
private LogStream(String name, OutputStream out) |
|
72 |
{ |
|
73 |
super(new ByteArrayOutputStream()); |
|
74 |
bufOut = (ByteArrayOutputStream) super.out; |
|
75 |
||
76 |
this.name = name; |
|
77 |
setOutputStream(out); |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Return the LogStream identified by the given name. If |
|
82 |
* a log corresponding to "name" does not exist, a log using |
|
83 |
* the default stream is created. |
|
84 |
* @param name name identifying the desired LogStream |
|
85 |
* @return log associated with given name |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
86 |
* @since 1.1 |
2 | 87 |
* @deprecated no replacement |
88 |
*/ |
|
89 |
@Deprecated |
|
90 |
public static LogStream log(String name) { |
|
91 |
LogStream stream; |
|
92 |
synchronized (known) { |
|
11117
b6e68b1344d4
7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents:
5506
diff
changeset
|
93 |
stream = known.get(name); |
2 | 94 |
if (stream == null) { |
95 |
stream = new LogStream(name, defaultStream); |
|
96 |
} |
|
97 |
known.put(name, stream); |
|
98 |
} |
|
99 |
return stream; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Return the current default stream for new logs. |
|
104 |
* @return default log stream |
|
105 |
* @see #setDefaultStream |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
106 |
* @since 1.1 |
2 | 107 |
* @deprecated no replacement |
108 |
*/ |
|
109 |
@Deprecated |
|
110 |
public static synchronized PrintStream getDefaultStream() { |
|
111 |
return defaultStream; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Set the default stream for new logs. |
|
116 |
* @param newDefault new default log stream |
|
117 |
* @see #getDefaultStream |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
118 |
* @since 1.1 |
2 | 119 |
* @deprecated no replacement |
120 |
*/ |
|
121 |
@Deprecated |
|
122 |
public static synchronized void setDefaultStream(PrintStream newDefault) { |
|
16874 | 123 |
SecurityManager sm = System.getSecurityManager(); |
124 |
||
125 |
if (sm != null) { |
|
126 |
sm.checkPermission( |
|
127 |
new java.util.logging.LoggingPermission("control", null)); |
|
128 |
} |
|
129 |
||
2 | 130 |
defaultStream = newDefault; |
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Return the current stream to which output from this log is sent. |
|
135 |
* @return output stream for this log |
|
136 |
* @see #setOutputStream |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
137 |
* @since 1.1 |
2 | 138 |
* @deprecated no replacement |
139 |
*/ |
|
140 |
@Deprecated |
|
141 |
public synchronized OutputStream getOutputStream() |
|
142 |
{ |
|
143 |
return logOut; |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Set the stream to which output from this log is sent. |
|
148 |
* @param out new output stream for this log |
|
149 |
* @see #getOutputStream |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
150 |
* @since 1.1 |
2 | 151 |
* @deprecated no replacement |
152 |
*/ |
|
153 |
@Deprecated |
|
154 |
public synchronized void setOutputStream(OutputStream out) |
|
155 |
{ |
|
156 |
logOut = out; |
|
157 |
// Maintain an OutputStreamWriter with default CharToByteConvertor |
|
158 |
// (just like new PrintStream) for writing log message prefixes. |
|
159 |
logWriter = new OutputStreamWriter(logOut); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Write a byte of data to the stream. If it is not a newline, then |
|
164 |
* the byte is appended to the internal buffer. If it is a newline, |
|
165 |
* then the currently buffered line is sent to the log's output |
|
166 |
* stream, prefixed with the appropriate logging information. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
167 |
* @since 1.1 |
2 | 168 |
* @deprecated no replacement |
169 |
*/ |
|
170 |
@Deprecated |
|
171 |
public void write(int b) |
|
172 |
{ |
|
173 |
if (b == '\n') { |
|
174 |
// synchronize on "this" first to avoid potential deadlock |
|
175 |
synchronized (this) { |
|
176 |
synchronized (logOut) { |
|
177 |
// construct prefix for log messages: |
|
178 |
buffer.setLength(0);; |
|
179 |
buffer.append( // date/time stamp... |
|
180 |
(new Date()).toString()); |
|
181 |
buffer.append(':'); |
|
182 |
buffer.append(name); // ...log name... |
|
183 |
buffer.append(':'); |
|
184 |
buffer.append(Thread.currentThread().getName()); |
|
185 |
buffer.append(':'); // ...and thread name |
|
186 |
||
187 |
try { |
|
188 |
// write prefix through to underlying byte stream |
|
189 |
logWriter.write(buffer.toString()); |
|
190 |
logWriter.flush(); |
|
191 |
||
192 |
// finally, write the already converted bytes of |
|
193 |
// the log message |
|
194 |
bufOut.writeTo(logOut); |
|
195 |
logOut.write(b); |
|
196 |
logOut.flush(); |
|
197 |
} catch (IOException e) { |
|
198 |
setError(); |
|
199 |
} finally { |
|
200 |
bufOut.reset(); |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
else |
|
206 |
super.write(b); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* Write a subarray of bytes. Pass each through write byte method. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
211 |
* @since 1.1 |
2 | 212 |
* @deprecated no replacement |
213 |
*/ |
|
214 |
@Deprecated |
|
215 |
public void write(byte b[], int off, int len) |
|
216 |
{ |
|
217 |
if (len < 0) |
|
218 |
throw new ArrayIndexOutOfBoundsException(len); |
|
219 |
for (int i = 0; i < len; ++ i) |
|
220 |
write(b[off + i]); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Return log name as string representation. |
|
225 |
* @return log name |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
226 |
* @since 1.1 |
2 | 227 |
* @deprecated no replacement |
228 |
*/ |
|
229 |
@Deprecated |
|
230 |
public String toString() |
|
231 |
{ |
|
232 |
return name; |
|
233 |
} |
|
234 |
||
235 |
/** log level constant (no logging). */ |
|
236 |
public static final int SILENT = 0; |
|
237 |
/** log level constant (brief logging). */ |
|
238 |
public static final int BRIEF = 10; |
|
239 |
/** log level constant (verbose logging). */ |
|
240 |
public static final int VERBOSE = 20; |
|
241 |
||
242 |
/** |
|
243 |
* Convert a string name of a logging level to its internal |
|
244 |
* integer representation. |
|
245 |
* @param s name of logging level (e.g., 'SILENT', 'BRIEF', 'VERBOSE') |
|
246 |
* @return corresponding integer log level |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
16874
diff
changeset
|
247 |
* @since 1.1 |
2 | 248 |
* @deprecated no replacement |
249 |
*/ |
|
250 |
@Deprecated |
|
251 |
public static int parseLevel(String s) |
|
252 |
{ |
|
253 |
if ((s == null) || (s.length() < 1)) |
|
254 |
return -1; |
|
255 |
||
256 |
try { |
|
257 |
return Integer.parseInt(s); |
|
258 |
} catch (NumberFormatException e) { |
|
259 |
} |
|
260 |
if (s.length() < 1) |
|
261 |
return -1; |
|
262 |
||
263 |
if ("SILENT".startsWith(s.toUpperCase())) |
|
264 |
return SILENT; |
|
265 |
else if ("BRIEF".startsWith(s.toUpperCase())) |
|
266 |
return BRIEF; |
|
267 |
else if ("VERBOSE".startsWith(s.toUpperCase())) |
|
268 |
return VERBOSE; |
|
269 |
||
270 |
return -1; |
|
271 |
} |
|
272 |
} |