Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.logging/share/classes/java/util/logging/Handler.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
27
package java.util.logging;
28
29
import java.util.Objects;
30
import java.io.UnsupportedEncodingException;
31
import java.security.AccessController;
32
import java.security.PrivilegedAction;
33
34
/**
35
* A {@code Handler} object takes log messages from a {@code Logger} and
36
* exports them. It might for example, write them to a console
37
* or write them to a file, or send them to a network logging service,
38
* or forward them to an OS log, or whatever.
39
* <p>
40
* A {@code Handler} can be disabled by doing a {@code setLevel(Level.OFF)}
41
* and can be re-enabled by doing a {@code setLevel} with an appropriate level.
42
* <p>
43
* {@code Handler} classes typically use {@code LogManager} properties to set
44
* default values for the {@code Handler}'s {@code Filter}, {@code Formatter},
45
* and {@code Level}. See the specific documentation for each concrete
46
* {@code Handler} class.
47
*
48
*
49
* @since 1.4
50
*/
51
52
public abstract class Handler {
53
private static final int offValue = Level.OFF.intValue();
54
private final LogManager manager = LogManager.getLogManager();
55
56
// We're using volatile here to avoid synchronizing getters, which
57
// would prevent other threads from calling isLoggable()
58
// while publish() is executing.
59
// On the other hand, setters will be synchronized to exclude concurrent
60
// execution with more complex methods, such as StreamHandler.publish().
61
// We wouldn't want 'level' to be changed by another thread in the middle
62
// of the execution of a 'publish' call.
63
private volatile Filter filter;
64
private volatile Formatter formatter;
65
private volatile Level logLevel = Level.ALL;
66
private volatile ErrorManager errorManager = new ErrorManager();
67
private volatile String encoding;
68
69
/**
70
* Default constructor. The resulting {@code Handler} has a log
71
* level of {@code Level.ALL}, no {@code Formatter}, and no
72
* {@code Filter}. A default {@code ErrorManager} instance is installed
73
* as the {@code ErrorManager}.
74
*/
75
protected Handler() {
76
}
77
78
/**
79
* Package-private constructor for chaining from subclass constructors
80
* that wish to configure the handler with specific default and/or
81
* specified values.
82
*
83
* @param defaultLevel a default {@link Level} to configure if one is
84
* not found in LogManager configuration properties
85
* @param defaultFormatter a default {@link Formatter} to configure if one is
86
* not specified by {@code specifiedFormatter} parameter
87
* nor found in LogManager configuration properties
88
* @param specifiedFormatter if not null, this is the formatter to configure
89
*/
90
@SuppressWarnings("removal")
91
Handler(Level defaultLevel, Formatter defaultFormatter,
92
Formatter specifiedFormatter) {
93
94
LogManager manager = LogManager.getLogManager();
95
String cname = getClass().getName();
96
97
final Level level = manager.getLevelProperty(cname + ".level", defaultLevel);
98
final Filter filter = manager.getFilterProperty(cname + ".filter", null);
99
final Formatter formatter = specifiedFormatter == null
100
? manager.getFormatterProperty(cname + ".formatter", defaultFormatter)
101
: specifiedFormatter;
102
final String encoding = manager.getStringProperty(cname + ".encoding", null);
103
104
AccessController.doPrivileged(new PrivilegedAction<Void>() {
105
@Override
106
public Void run() {
107
setLevel(level);
108
setFilter(filter);
109
setFormatter(formatter);
110
try {
111
setEncoding(encoding);
112
} catch (Exception ex) {
113
try {
114
setEncoding(null);
115
} catch (Exception ex2) {
116
// doing a setEncoding with null should always work.
117
// assert false;
118
}
119
}
120
return null;
121
}
122
}, null, LogManager.controlPermission);
123
}
124
125
/**
126
* Publish a {@code LogRecord}.
127
* <p>
128
* The logging request was made initially to a {@code Logger} object,
129
* which initialized the {@code LogRecord} and forwarded it here.
130
* <p>
131
* The {@code Handler} is responsible for formatting the message, when and
132
* if necessary. The formatting should include localization.
133
*
134
* @param record description of the log event. A null record is
135
* silently ignored and is not published
136
*/
137
public abstract void publish(LogRecord record);
138
139
/**
140
* Flush any buffered output.
141
*/
142
public abstract void flush();
143
144
/**
145
* Close the {@code Handler} and free all associated resources.
146
* <p>
147
* The close method will perform a {@code flush} and then close the
148
* {@code Handler}. After close has been called this {@code Handler}
149
* should no longer be used. Method calls may either be silently
150
* ignored or may throw runtime exceptions.
151
*
152
* @throws SecurityException if a security manager exists and if
153
* the caller does not have {@code LoggingPermission("control")}.
154
*/
155
public abstract void close() throws SecurityException;
156
157
/**
158
* Set a {@code Formatter}. This {@code Formatter} will be used
159
* to format {@code LogRecords} for this {@code Handler}.
160
* <p>
161
* Some {@code Handlers} may not use {@code Formatters}, in
162
* which case the {@code Formatter} will be remembered, but not used.
163
*
164
* @param newFormatter the {@code Formatter} to use (may not be null)
165
* @throws SecurityException if a security manager exists and if
166
* the caller does not have {@code LoggingPermission("control")}.
167
*/
168
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
169
checkPermission();
170
formatter = Objects.requireNonNull(newFormatter);
171
}
172
173
/**
174
* Return the {@code Formatter} for this {@code Handler}.
175
* @return the {@code Formatter} (may be null).
176
*/
177
public Formatter getFormatter() {
178
return formatter;
179
}
180
181
/**
182
* Set the character encoding used by this {@code Handler}.
183
* <p>
184
* The encoding should be set before any {@code LogRecords} are written
185
* to the {@code Handler}.
186
*
187
* @param encoding The name of a supported character encoding.
188
* May be null, to indicate the default platform encoding.
189
* @throws SecurityException if a security manager exists and if
190
* the caller does not have {@code LoggingPermission("control")}.
191
* @throws UnsupportedEncodingException if the named encoding is
192
* not supported.
193
*/
194
public synchronized void setEncoding(String encoding)
195
throws SecurityException, java.io.UnsupportedEncodingException {
196
checkPermission();
197
if (encoding != null) {
198
try {
199
if(!java.nio.charset.Charset.isSupported(encoding)) {
200
throw new UnsupportedEncodingException(encoding);
201
}
202
} catch (java.nio.charset.IllegalCharsetNameException e) {
203
throw new UnsupportedEncodingException(encoding);
204
}
205
}
206
this.encoding = encoding;
207
}
208
209
/**
210
* Return the character encoding for this {@code Handler}.
211
*
212
* @return The encoding name. May be null, which indicates the
213
* default encoding should be used.
214
*/
215
public String getEncoding() {
216
return encoding;
217
}
218
219
/**
220
* Set a {@code Filter} to control output on this {@code Handler}.
221
* <P>
222
* For each call of {@code publish} the {@code Handler} will call
223
* this {@code Filter} (if it is non-null) to check if the
224
* {@code LogRecord} should be published or discarded.
225
*
226
* @param newFilter a {@code Filter} object (may be null)
227
* @throws SecurityException if a security manager exists and if
228
* the caller does not have {@code LoggingPermission("control")}.
229
*/
230
public synchronized void setFilter(Filter newFilter) throws SecurityException {
231
checkPermission();
232
filter = newFilter;
233
}
234
235
/**
236
* Get the current {@code Filter} for this {@code Handler}.
237
*
238
* @return a {@code Filter} object (may be null)
239
*/
240
public Filter getFilter() {
241
return filter;
242
}
243
244
/**
245
* Define an ErrorManager for this Handler.
246
* <p>
247
* The ErrorManager's "error" method will be invoked if any
248
* errors occur while using this Handler.
249
*
250
* @param em the new ErrorManager
251
* @throws SecurityException if a security manager exists and if
252
* the caller does not have {@code LoggingPermission("control")}.
253
*/
254
public synchronized void setErrorManager(ErrorManager em) {
255
checkPermission();
256
if (em == null) {
257
throw new NullPointerException();
258
}
259
errorManager = em;
260
}
261
262
/**
263
* Retrieves the ErrorManager for this Handler.
264
*
265
* @return the ErrorManager for this Handler
266
* @throws SecurityException if a security manager exists and if
267
* the caller does not have {@code LoggingPermission("control")}.
268
*/
269
public ErrorManager getErrorManager() {
270
checkPermission();
271
return errorManager;
272
}
273
274
/**
275
* Protected convenience method to report an error to this Handler's
276
* ErrorManager. Note that this method retrieves and uses the ErrorManager
277
* without doing a security check. It can therefore be used in
278
* environments where the caller may be non-privileged.
279
*
280
* @param msg a descriptive string (may be null)
281
* @param ex an exception (may be null)
282
* @param code an error code defined in ErrorManager
283
*/
284
protected void reportError(String msg, Exception ex, int code) {
285
try {
286
errorManager.error(msg, ex, code);
287
} catch (Exception ex2) {
288
System.err.println("Handler.reportError caught:");
289
ex2.printStackTrace();
290
}
291
}
292
293
/**
294
* Set the log level specifying which message levels will be
295
* logged by this {@code Handler}. Message levels lower than this
296
* value will be discarded.
297
* <p>
298
* The intention is to allow developers to turn on voluminous
299
* logging, but to limit the messages that are sent to certain
300
* {@code Handlers}.
301
*
302
* @param newLevel the new value for the log level
303
* @throws SecurityException if a security manager exists and if
304
* the caller does not have {@code LoggingPermission("control")}.
305
*/
306
public synchronized void setLevel(Level newLevel) throws SecurityException {
307
if (newLevel == null) {
308
throw new NullPointerException();
309
}
310
checkPermission();
311
logLevel = newLevel;
312
}
313
314
/**
315
* Get the log level specifying which messages will be
316
* logged by this {@code Handler}. Message levels lower
317
* than this level will be discarded.
318
* @return the level of messages being logged.
319
*/
320
public Level getLevel() {
321
return logLevel;
322
}
323
324
/**
325
* Check if this {@code Handler} would actually log a given {@code LogRecord}.
326
* <p>
327
* This method checks if the {@code LogRecord} has an appropriate
328
* {@code Level} and whether it satisfies any {@code Filter}. It also
329
* may make other {@code Handler} specific checks that might prevent a
330
* handler from logging the {@code LogRecord}. It will return false if
331
* the {@code LogRecord} is null.
332
*
333
* @param record a {@code LogRecord} (may be null).
334
* @return true if the {@code LogRecord} would be logged.
335
*
336
*/
337
public boolean isLoggable(LogRecord record) {
338
final int levelValue = getLevel().intValue();
339
if (record == null) return false;
340
if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
341
return false;
342
}
343
final Filter filter = getFilter();
344
if (filter == null) {
345
return true;
346
}
347
return filter.isLoggable(record);
348
}
349
350
// Package-private support method for security checks.
351
// We check that the caller has appropriate security privileges
352
// to update Handler state and if not throw a SecurityException.
353
void checkPermission() throws SecurityException {
354
manager.checkPermission();
355
}
356
}
357
358