Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/DebugSettings.java
38827 views
/*1* Copyright (c) 1999, 2013, 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*/73final 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 DebugSettings instance = null;9091private Properties props = new Properties();9293static void init() {94if (instance != null) {95return;96}9798NativeLibLoader.loadLibraries();99instance = new DebugSettings();100instance.loadNativeSettings();101}102103private DebugSettings() {104java.security.AccessController.doPrivileged(105new java.security.PrivilegedAction<Void>() {106public Void run() {107loadProperties();108return null;109}110});111}112113/*114* Load debug properties from file, then override115* with any command line specified properties116*/117private synchronized void loadProperties() {118// setup initial properties119java.security.AccessController.doPrivileged(120new java.security.PrivilegedAction<Void>() {121public Void run() {122loadDefaultProperties();123loadFileProperties();124loadSystemProperties();125return null;126}127});128129// echo the initial property settings to stdout130if (log.isLoggable(PlatformLogger.Level.FINE)) {131log.fine("DebugSettings:\n{0}", this);132}133}134135public String toString() {136ByteArrayOutputStream bout = new ByteArrayOutputStream();137PrintStream pout = new PrintStream(bout);138for (String key : props.stringPropertyNames()) {139String value = props.getProperty(key, "");140pout.println(key + " = " + value);141}142return new String(bout.toByteArray());143}144145/*146* Sets up default property values147*/148private void loadDefaultProperties() {149// is there a more inefficient way to setup default properties?150// maybe, but this has got to be close to 100% non-optimal151try {152for ( int nprop = 0; nprop < DEFAULT_PROPS.length; nprop++ ) {153StringBufferInputStream in = new StringBufferInputStream(DEFAULT_PROPS[nprop]);154props.load(in);155in.close();156}157} catch(IOException ioe) {158}159}160161/*162* load properties from file, overriding defaults163*/164private void loadFileProperties() {165String propPath;166Properties fileProps;167168// check if the user specified a particular settings file169propPath = System.getProperty(PREFIX + "." + PROP_FILE, "");170if (propPath.equals("")) {171// otherwise get it from the user's home directory172propPath = System.getProperty("user.home", "") +173File.separator +174PREFIX + "." + PROP_FILE;175}176177File propFile = new File(propPath);178try {179println("Reading debug settings from '" + propFile.getCanonicalPath() + "'...");180FileInputStream fin = new FileInputStream(propFile);181props.load(fin);182fin.close();183} catch ( FileNotFoundException fne ) {184println("Did not find settings file.");185} catch ( IOException ioe ) {186println("Problem reading settings, IOException: " + ioe.getMessage());187}188}189190/*191* load properties from system props (command line spec'd usually),192* overriding default or file properties193*/194private void loadSystemProperties() {195// override file properties with system properties196Properties sysProps = System.getProperties();197for (String key : sysProps.stringPropertyNames()) {198String value = sysProps.getProperty(key,"");199// copy any "awtdebug" properties over200if ( key.startsWith(PREFIX) ) {201props.setProperty(key, value);202}203}204}205206/**207* Gets named boolean property208* @param key Name of property209* @param defval Default value if property does not exist210* @return boolean value of the named property211*/212public synchronized boolean getBoolean(String key, boolean defval) {213String value = getString(key, String.valueOf(defval));214return value.equalsIgnoreCase("true");215}216217/**218* Gets named integer property219* @param key Name of property220* @param defval Default value if property does not exist221* @return integer value of the named property222*/223public synchronized int getInt(String key, int defval) {224String value = getString(key, String.valueOf(defval));225return Integer.parseInt(value);226}227228/**229* Gets named String property230* @param key Name of property231* @param defval Default value if property does not exist232* @return string value of the named property233*/234public synchronized String getString(String key, String defval) {235String actualKeyName = PREFIX + "." + key;236String value = props.getProperty(actualKeyName, defval);237//println(actualKeyName+"="+value);238return value;239}240241private synchronized List<String> getPropertyNames() {242List<String> propNames = new LinkedList<>();243// remove global prefix from property names244for (String propName : props.stringPropertyNames()) {245propName = propName.substring(PREFIX.length()+1);246propNames.add(propName);247}248return propNames;249}250251private void println(Object object) {252if (log.isLoggable(PlatformLogger.Level.FINER)) {253log.finer(object.toString());254}255}256257private static final String PROP_CTRACE = "ctrace";258private static final int PROP_CTRACE_LEN = PROP_CTRACE.length();259260private native synchronized void setCTracingOn(boolean enabled);261private native synchronized void setCTracingOn(boolean enabled, String file);262private native synchronized void setCTracingOn(boolean enabled, String file, int line);263264private void loadNativeSettings() {265boolean ctracingOn;266267ctracingOn = getBoolean(PROP_CTRACE, false);268setCTracingOn(ctracingOn);269270//271// Filter out file/line ctrace properties from debug settings272//273List<String> traces = new LinkedList<>();274275for (String key : getPropertyNames()) {276if (key.startsWith(PROP_CTRACE) && key.length() > PROP_CTRACE_LEN) {277traces.add(key);278}279}280281// sort traces list so file-level traces will be before line-level ones282Collections.sort(traces);283284//285// Setup the trace points286//287for (String key : traces) {288String trace = key.substring(PROP_CTRACE_LEN+1);289String filespec;290String linespec;291int delim= trace.indexOf('@');292boolean enabled;293294// parse out the filename and linenumber from the property name295filespec = delim != -1 ? trace.substring(0, delim) : trace;296linespec = delim != -1 ? trace.substring(delim+1) : "";297enabled = getBoolean(key, false);298//System.out.println("Key="+key+", File="+filespec+", Line="+linespec+", Enabled="+enabled);299300if ( linespec.length() == 0 ) {301// set file specific trace setting302setCTracingOn(enabled, filespec);303} else {304// set line specific trace setting305int linenum = Integer.parseInt(linespec, 10);306setCTracingOn(enabled, filespec, linenum);307}308}309}310}311312313