Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/sun/awt/DebugSettings.java
41152 views
1
/*
2
* Copyright (c) 1999, 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
package sun.awt;
27
28
import java.io.*;
29
30
import java.util.*;
31
import sun.util.logging.PlatformLogger;
32
33
/*
34
* Internal class that manages sun.awt.Debug settings.
35
* Settings can be specified on a global, per-package,
36
* or per-class level.
37
*
38
* Properties affecting the behaviour of the Debug class are
39
* loaded from the awtdebug.properties file at class load
40
* time. The properties file is assumed to be in the
41
* user.home directory. A different file can be used
42
* by setting the awtdebug.properties system property.
43
* e.g. java -Dawtdebug.properties=foo.properties
44
*
45
* Only properties beginning with 'awtdebug' have any
46
* meaning-- all other properties are ignored.
47
*
48
* You can override the properties file by specifying
49
* 'awtdebug' props as system properties on the command line.
50
* e.g. java -Dawtdebug.trace=true
51
* Properties specific to a package or a class can be set
52
* by qualifying the property names as follows:
53
* awtdebug.<property name>.<class or package name>
54
* So for example, turning on tracing in the com.acme.Fubar
55
* class would be done as follows:
56
* awtdebug.trace.com.acme.Fubar=true
57
*
58
* Class settings always override package settings, which in
59
* turn override global settings.
60
*
61
* Addition from July, 2007.
62
*
63
* After the fix for 4638447 all the usage of DebugHelper
64
* classes in Java code are replaced with the corresponding
65
* Java Logging API calls. This file is now used only to
66
* control native logging.
67
*
68
* To enable native logging you should set the following
69
* system property to 'true': sun.awt.nativedebug. After
70
* the native logging is enabled, the actual debug settings
71
* are read the same way as described above (as before
72
* the fix for 4638447).
73
*/
74
public final class DebugSettings {
75
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.debug.DebugSettings");
76
77
/* standard debug property key names */
78
static final String PREFIX = "awtdebug";
79
static final String PROP_FILE = "properties";
80
81
/* default property settings */
82
private static final String[] DEFAULT_PROPS = {
83
"awtdebug.assert=true",
84
"awtdebug.trace=false",
85
"awtdebug.on=true",
86
"awtdebug.ctrace=false"
87
};
88
89
/* global instance of the settings object */
90
private static final DebugSettings instance = new DebugSettings();
91
92
private final Properties props = new Properties();
93
94
static synchronized void init() {
95
if (!instance.props.isEmpty()) {
96
return;
97
}
98
NativeLibLoader.loadLibraries();
99
instance.loadProperties();
100
instance.loadNativeSettings();
101
}
102
103
public static DebugSettings getInstance() {
104
return instance;
105
}
106
107
/*
108
* Load debug properties from file, then override
109
* with any command line specified properties
110
*/
111
@SuppressWarnings("removal")
112
private synchronized void loadProperties() {
113
// setup initial properties
114
java.security.AccessController.doPrivileged(
115
new java.security.PrivilegedAction<Void>() {
116
public Void run() {
117
loadDefaultProperties();
118
loadFileProperties();
119
loadSystemProperties();
120
return null;
121
}
122
});
123
124
// echo the initial property settings to stdout
125
if (log.isLoggable(PlatformLogger.Level.FINE)) {
126
log.fine("DebugSettings:\n{0}", this);
127
}
128
}
129
130
public String toString() {
131
ByteArrayOutputStream bout = new ByteArrayOutputStream();
132
PrintStream pout = new PrintStream(bout);
133
for (String key : props.stringPropertyNames()) {
134
String value = props.getProperty(key, "");
135
pout.println(key + " = " + value);
136
}
137
return new String(bout.toByteArray());
138
}
139
140
/*
141
* Sets up default property values
142
*/
143
@SuppressWarnings("deprecation")
144
private void loadDefaultProperties() {
145
// is there a more inefficient way to setup default properties?
146
// maybe, but this has got to be close to 100% non-optimal
147
try {
148
for ( int nprop = 0; nprop < DEFAULT_PROPS.length; nprop++ ) {
149
StringBufferInputStream in = new StringBufferInputStream(DEFAULT_PROPS[nprop]);
150
props.load(in);
151
in.close();
152
}
153
} catch(IOException ioe) {
154
}
155
}
156
157
/*
158
* load properties from file, overriding defaults
159
*/
160
private void loadFileProperties() {
161
String propPath;
162
Properties fileProps;
163
164
// check if the user specified a particular settings file
165
propPath = System.getProperty(PREFIX + "." + PROP_FILE, "");
166
if (propPath.isEmpty()) {
167
// otherwise get it from the user's home directory
168
propPath = System.getProperty("user.home", "") +
169
File.separator +
170
PREFIX + "." + PROP_FILE;
171
}
172
173
File propFile = new File(propPath);
174
try {
175
println("Reading debug settings from '" + propFile.getCanonicalPath() + "'...");
176
FileInputStream fin = new FileInputStream(propFile);
177
props.load(fin);
178
fin.close();
179
} catch ( FileNotFoundException fne ) {
180
println("Did not find settings file.");
181
} catch ( IOException ioe ) {
182
println("Problem reading settings, IOException: " + ioe.getMessage());
183
}
184
}
185
186
/*
187
* load properties from system props (command line spec'd usually),
188
* overriding default or file properties
189
*/
190
private void loadSystemProperties() {
191
// override file properties with system properties
192
Properties sysProps = System.getProperties();
193
for (String key : sysProps.stringPropertyNames()) {
194
String value = sysProps.getProperty(key,"");
195
// copy any "awtdebug" properties over
196
if ( key.startsWith(PREFIX) ) {
197
props.setProperty(key, value);
198
}
199
}
200
}
201
202
/**
203
* Gets named boolean property
204
* @param key Name of property
205
* @param defval Default value if property does not exist
206
* @return boolean value of the named property
207
*/
208
public synchronized boolean getBoolean(String key, boolean defval) {
209
String value = getString(key, String.valueOf(defval));
210
return value.equalsIgnoreCase("true");
211
}
212
213
/**
214
* Gets named integer property
215
* @param key Name of property
216
* @param defval Default value if property does not exist
217
* @return integer value of the named property
218
*/
219
public synchronized int getInt(String key, int defval) {
220
String value = getString(key, String.valueOf(defval));
221
return Integer.parseInt(value);
222
}
223
224
/**
225
* Gets named String property
226
* @param key Name of property
227
* @param defval Default value if property does not exist
228
* @return string value of the named property
229
*/
230
public synchronized String getString(String key, String defval) {
231
String actualKeyName = PREFIX + "." + key;
232
String value = props.getProperty(actualKeyName, defval);
233
//println(actualKeyName+"="+value);
234
return value;
235
}
236
237
private synchronized List<String> getPropertyNames() {
238
List<String> propNames = new LinkedList<>();
239
// remove global prefix from property names
240
for (String propName : props.stringPropertyNames()) {
241
propName = propName.substring(PREFIX.length()+1);
242
propNames.add(propName);
243
}
244
return propNames;
245
}
246
247
private void println(Object object) {
248
if (log.isLoggable(PlatformLogger.Level.FINER)) {
249
log.finer(object.toString());
250
}
251
}
252
253
private static final String PROP_CTRACE = "ctrace";
254
private static final int PROP_CTRACE_LEN = PROP_CTRACE.length();
255
256
private synchronized native void setCTracingOn(boolean enabled);
257
private synchronized native void setCTracingOn(boolean enabled, String file);
258
private synchronized native void setCTracingOn(boolean enabled, String file, int line);
259
260
private void loadNativeSettings() {
261
boolean ctracingOn;
262
263
ctracingOn = getBoolean(PROP_CTRACE, false);
264
setCTracingOn(ctracingOn);
265
266
//
267
// Filter out file/line ctrace properties from debug settings
268
//
269
List<String> traces = new LinkedList<>();
270
271
for (String key : getPropertyNames()) {
272
if (key.startsWith(PROP_CTRACE) && key.length() > PROP_CTRACE_LEN) {
273
traces.add(key);
274
}
275
}
276
277
// sort traces list so file-level traces will be before line-level ones
278
Collections.sort(traces);
279
280
//
281
// Setup the trace points
282
//
283
for (String key : traces) {
284
String trace = key.substring(PROP_CTRACE_LEN+1);
285
String filespec;
286
String linespec;
287
int delim= trace.indexOf('@');
288
boolean enabled;
289
290
// parse out the filename and linenumber from the property name
291
filespec = delim != -1 ? trace.substring(0, delim) : trace;
292
linespec = delim != -1 ? trace.substring(delim+1) : "";
293
enabled = getBoolean(key, false);
294
//System.out.println("Key="+key+", File="+filespec+", Line="+linespec+", Enabled="+enabled);
295
296
if ( linespec.length() == 0 ) {
297
// set file specific trace setting
298
setCTracingOn(enabled, filespec);
299
} else {
300
// set line specific trace setting
301
int linenum = Integer.parseInt(linespec, 10);
302
setCTracingOn(enabled, filespec, linenum);
303
}
304
}
305
}
306
}
307
308