Path: blob/master/src/java.desktop/share/classes/sun/awt/DebugSettings.java
41152 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.io.*;2829import java.util.*;30import sun.util.logging.PlatformLogger;3132/*33* Internal class that manages sun.awt.Debug settings.34* Settings can be specified on a global, per-package,35* or per-class level.36*37* Properties affecting the behaviour of the Debug class are38* loaded from the awtdebug.properties file at class load39* time. The properties file is assumed to be in the40* user.home directory. A different file can be used41* by setting the awtdebug.properties system property.42* e.g. java -Dawtdebug.properties=foo.properties43*44* Only properties beginning with 'awtdebug' have any45* meaning-- all other properties are ignored.46*47* You can override the properties file by specifying48* 'awtdebug' props as system properties on the command line.49* e.g. java -Dawtdebug.trace=true50* Properties specific to a package or a class can be set51* by qualifying the property names as follows:52* awtdebug.<property name>.<class or package name>53* So for example, turning on tracing in the com.acme.Fubar54* class would be done as follows:55* awtdebug.trace.com.acme.Fubar=true56*57* Class settings always override package settings, which in58* turn override global settings.59*60* Addition from July, 2007.61*62* After the fix for 4638447 all the usage of DebugHelper63* classes in Java code are replaced with the corresponding64* Java Logging API calls. This file is now used only to65* control native logging.66*67* To enable native logging you should set the following68* system property to 'true': sun.awt.nativedebug. After69* the native logging is enabled, the actual debug settings70* are read the same way as described above (as before71* the fix for 4638447).72*/73public final class DebugSettings {74private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.debug.DebugSettings");7576/* standard debug property key names */77static final String PREFIX = "awtdebug";78static final String PROP_FILE = "properties";7980/* default property settings */81private static final String[] DEFAULT_PROPS = {82"awtdebug.assert=true",83"awtdebug.trace=false",84"awtdebug.on=true",85"awtdebug.ctrace=false"86};8788/* global instance of the settings object */89private static final DebugSettings instance = new DebugSettings();9091private final Properties props = new Properties();9293static synchronized void init() {94if (!instance.props.isEmpty()) {95return;96}97NativeLibLoader.loadLibraries();98instance.loadProperties();99instance.loadNativeSettings();100}101102public static DebugSettings getInstance() {103return instance;104}105106/*107* Load debug properties from file, then override108* with any command line specified properties109*/110@SuppressWarnings("removal")111private synchronized void loadProperties() {112// setup initial properties113java.security.AccessController.doPrivileged(114new java.security.PrivilegedAction<Void>() {115public Void run() {116loadDefaultProperties();117loadFileProperties();118loadSystemProperties();119return null;120}121});122123// echo the initial property settings to stdout124if (log.isLoggable(PlatformLogger.Level.FINE)) {125log.fine("DebugSettings:\n{0}", this);126}127}128129public String toString() {130ByteArrayOutputStream bout = new ByteArrayOutputStream();131PrintStream pout = new PrintStream(bout);132for (String key : props.stringPropertyNames()) {133String value = props.getProperty(key, "");134pout.println(key + " = " + value);135}136return new String(bout.toByteArray());137}138139/*140* Sets up default property values141*/142@SuppressWarnings("deprecation")143private void loadDefaultProperties() {144// is there a more inefficient way to setup default properties?145// maybe, but this has got to be close to 100% non-optimal146try {147for ( int nprop = 0; nprop < DEFAULT_PROPS.length; nprop++ ) {148StringBufferInputStream in = new StringBufferInputStream(DEFAULT_PROPS[nprop]);149props.load(in);150in.close();151}152} catch(IOException ioe) {153}154}155156/*157* load properties from file, overriding defaults158*/159private void loadFileProperties() {160String propPath;161Properties fileProps;162163// check if the user specified a particular settings file164propPath = System.getProperty(PREFIX + "." + PROP_FILE, "");165if (propPath.isEmpty()) {166// otherwise get it from the user's home directory167propPath = System.getProperty("user.home", "") +168File.separator +169PREFIX + "." + PROP_FILE;170}171172File propFile = new File(propPath);173try {174println("Reading debug settings from '" + propFile.getCanonicalPath() + "'...");175FileInputStream fin = new FileInputStream(propFile);176props.load(fin);177fin.close();178} catch ( FileNotFoundException fne ) {179println("Did not find settings file.");180} catch ( IOException ioe ) {181println("Problem reading settings, IOException: " + ioe.getMessage());182}183}184185/*186* load properties from system props (command line spec'd usually),187* overriding default or file properties188*/189private void loadSystemProperties() {190// override file properties with system properties191Properties sysProps = System.getProperties();192for (String key : sysProps.stringPropertyNames()) {193String value = sysProps.getProperty(key,"");194// copy any "awtdebug" properties over195if ( key.startsWith(PREFIX) ) {196props.setProperty(key, value);197}198}199}200201/**202* Gets named boolean property203* @param key Name of property204* @param defval Default value if property does not exist205* @return boolean value of the named property206*/207public synchronized boolean getBoolean(String key, boolean defval) {208String value = getString(key, String.valueOf(defval));209return value.equalsIgnoreCase("true");210}211212/**213* Gets named integer property214* @param key Name of property215* @param defval Default value if property does not exist216* @return integer value of the named property217*/218public synchronized int getInt(String key, int defval) {219String value = getString(key, String.valueOf(defval));220return Integer.parseInt(value);221}222223/**224* Gets named String property225* @param key Name of property226* @param defval Default value if property does not exist227* @return string value of the named property228*/229public synchronized String getString(String key, String defval) {230String actualKeyName = PREFIX + "." + key;231String value = props.getProperty(actualKeyName, defval);232//println(actualKeyName+"="+value);233return value;234}235236private synchronized List<String> getPropertyNames() {237List<String> propNames = new LinkedList<>();238// remove global prefix from property names239for (String propName : props.stringPropertyNames()) {240propName = propName.substring(PREFIX.length()+1);241propNames.add(propName);242}243return propNames;244}245246private void println(Object object) {247if (log.isLoggable(PlatformLogger.Level.FINER)) {248log.finer(object.toString());249}250}251252private static final String PROP_CTRACE = "ctrace";253private static final int PROP_CTRACE_LEN = PROP_CTRACE.length();254255private synchronized native void setCTracingOn(boolean enabled);256private synchronized native void setCTracingOn(boolean enabled, String file);257private synchronized native void setCTracingOn(boolean enabled, String file, int line);258259private void loadNativeSettings() {260boolean ctracingOn;261262ctracingOn = getBoolean(PROP_CTRACE, false);263setCTracingOn(ctracingOn);264265//266// Filter out file/line ctrace properties from debug settings267//268List<String> traces = new LinkedList<>();269270for (String key : getPropertyNames()) {271if (key.startsWith(PROP_CTRACE) && key.length() > PROP_CTRACE_LEN) {272traces.add(key);273}274}275276// sort traces list so file-level traces will be before line-level ones277Collections.sort(traces);278279//280// Setup the trace points281//282for (String key : traces) {283String trace = key.substring(PROP_CTRACE_LEN+1);284String filespec;285String linespec;286int delim= trace.indexOf('@');287boolean enabled;288289// parse out the filename and linenumber from the property name290filespec = delim != -1 ? trace.substring(0, delim) : trace;291linespec = delim != -1 ? trace.substring(delim+1) : "";292enabled = getBoolean(key, false);293//System.out.println("Key="+key+", File="+filespec+", Line="+linespec+", Enabled="+enabled);294295if ( linespec.length() == 0 ) {296// set file specific trace setting297setCTracingOn(enabled, filespec);298} else {299// set line specific trace setting300int linenum = Integer.parseInt(linespec, 10);301setCTracingOn(enabled, filespec, linenum);302}303}304}305}306307308