1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.debug;
  24 
  25 import java.io.PrintStream;
  26 import java.util.Collection;
  27 import java.util.EnumMap;
  28 import java.util.Map;
  29 
  30 import org.graalvm.compiler.debug.internal.DebugScope;
  31 import org.graalvm.compiler.options.OptionValues;
  32 
  33 public class DelegatingDebugConfig implements DebugConfig {
  34 
  35     protected final DebugConfig delegate;
  36 
  37     /**
  38      * The features of a {@link DelegatingDebugConfig} that can be force
  39      * {@linkplain DelegatingDebugConfig#enable(Feature) enabled}/
  40      * {@linkplain DelegatingDebugConfig#disable(Feature) disabled} or
  41      * {@linkplain DelegatingDebugConfig#delegate(Feature) delegated}.
  42      */
  43     public enum Feature {
  44         /**
  45          * @see Debug#isLogEnabledForMethod()
  46          */
  47         LOG_METHOD,
  48         /**
  49          * @see Debug#isDumpEnabledForMethod()
  50          */
  51         DUMP_METHOD,
  52         /**
  53          * @see Debug#isVerifyEnabled()
  54          */
  55         VERIFY,
  56         /**
  57          * @see Debug#isVerifyEnabledForMethod()
  58          */
  59         VERIFY_METHOD,
  60         /**
  61          * @see Debug#isCountEnabled()
  62          */
  63         COUNT,
  64         /**
  65          * @see Debug#isMethodMeterEnabled()
  66          */
  67         METHOD_METRICS,
  68         /**
  69          * @see Debug#isMemUseTrackingEnabled()
  70          */
  71         TRACK_MEM_USE,
  72         /**
  73          * @see Debug#isTimeEnabled()
  74          */
  75         TIME,
  76         /**
  77          * @see DebugConfig#interceptException(Throwable)
  78          */
  79         INTERCEPT
  80     }
  81 
  82     private final Map<Feature, Boolean> featureState = new EnumMap<>(Feature.class);
  83 
  84     /**
  85      * The debug levels of a {@link DelegatingDebugConfig} than can be
  86      * {@linkplain DelegatingDebugConfig#override(Level, int) overridden} or
  87      * {@linkplain DelegatingDebugConfig#delegate(Level) delegated}.
  88      */
  89     public enum Level {
  90         LOG,
  91         DUMP
  92     }
  93 
  94     private final Map<Level, Integer> levelState = new EnumMap<>(Level.class);
  95 
  96     /**
  97      * Creates a config that delegates to the {@link DebugScope#getConfig() current config}.
  98      */
  99     public DelegatingDebugConfig() {
 100         this(DebugScope.getConfig());
 101     }
 102 
 103     /**
 104      * Creates a config that delegates to a given config.
 105      */
 106     public DelegatingDebugConfig(DebugConfig delegate) {
 107         this.delegate = delegate;
 108     }
 109 
 110     public DelegatingDebugConfig enable(Feature feature) {
 111         featureState.put(feature, Boolean.TRUE);
 112         return this;
 113     }
 114 
 115     public DelegatingDebugConfig disable(Feature feature) {
 116         featureState.put(feature, Boolean.FALSE);
 117         return this;
 118     }
 119 
 120     public DelegatingDebugConfig override(Level level, int newLevel) {
 121         levelState.put(level, newLevel);
 122         return this;
 123     }
 124 
 125     public DelegatingDebugConfig delegate(Feature feature) {
 126         featureState.put(feature, null);
 127         return this;
 128     }
 129 
 130     public DelegatingDebugConfig delegate(Level level) {
 131         levelState.put(level, null);
 132         return this;
 133     }
 134 
 135     @Override
 136     public OptionValues getOptions() {
 137         return delegate.getOptions();
 138     }
 139 
 140     @Override
 141     public int getLogLevel() {
 142         Integer ls = levelState.get(Level.LOG);
 143         if (ls == null) {
 144             return delegate.getLogLevel();
 145         }
 146         return ls.intValue();
 147     }
 148 
 149     @Override
 150     public boolean isLogEnabledForMethod() {
 151         Boolean fs = featureState.get(Feature.LOG_METHOD);
 152         if (fs == null) {
 153             return delegate.isLogEnabledForMethod();
 154         }
 155         return fs.booleanValue();
 156     }
 157 
 158     @Override
 159     public boolean isCountEnabled() {
 160         Boolean fs = featureState.get(Feature.COUNT);
 161         if (fs == null) {
 162             return delegate.isCountEnabled();
 163         }
 164         return fs.booleanValue();
 165     }
 166 
 167     @Override
 168     public boolean isMemUseTrackingEnabled() {
 169         Boolean fs = featureState.get(Feature.TRACK_MEM_USE);
 170         if (fs == null) {
 171             return delegate.isMemUseTrackingEnabled();
 172         }
 173         return fs.booleanValue();
 174     }
 175 
 176     @Override
 177     public int getDumpLevel() {
 178         Integer ls = levelState.get(Level.DUMP);
 179         if (ls == null) {
 180             return delegate.getDumpLevel();
 181         }
 182         return ls.intValue();
 183     }
 184 
 185     @Override
 186     public boolean isDumpEnabledForMethod() {
 187         Boolean fs = featureState.get(Feature.DUMP_METHOD);
 188         if (fs == null) {
 189             return delegate.isDumpEnabledForMethod();
 190         }
 191         return fs.booleanValue();
 192     }
 193 
 194     @Override
 195     public boolean isVerifyEnabled() {
 196         Boolean fs = featureState.get(Feature.VERIFY);
 197         if (fs == null) {
 198             return delegate.isVerifyEnabled();
 199         }
 200         return fs.booleanValue();
 201     }
 202 
 203     @Override
 204     public boolean isVerifyEnabledForMethod() {
 205         Boolean fs = featureState.get(Feature.VERIFY_METHOD);
 206         if (fs == null) {
 207             return delegate.isVerifyEnabledForMethod();
 208         }
 209         return fs.booleanValue();
 210     }
 211 
 212     @Override
 213     public boolean isTimeEnabled() {
 214         Boolean fs = featureState.get(Feature.TIME);
 215         if (fs == null) {
 216             return delegate.isTimeEnabled();
 217         }
 218         return fs.booleanValue();
 219     }
 220 
 221     @Override
 222     public boolean isMethodMeterEnabled() {
 223         Boolean fs = featureState.get(Feature.METHOD_METRICS);
 224         if (fs == null) {
 225             return delegate.isMethodMeterEnabled();
 226         }
 227         return fs.booleanValue();
 228     }
 229 
 230     @Override
 231     public RuntimeException interceptException(Throwable e) {
 232         Boolean fs = featureState.get(Feature.INTERCEPT);
 233         if (fs == null || fs) {
 234             return delegate.interceptException(e);
 235         }
 236         return null;
 237     }
 238 
 239     @Override
 240     public Collection<DebugDumpHandler> dumpHandlers() {
 241         return delegate.dumpHandlers();
 242     }
 243 
 244     @Override
 245     public Collection<DebugVerifyHandler> verifyHandlers() {
 246         return delegate.verifyHandlers();
 247     }
 248 
 249     @Override
 250     public PrintStream output() {
 251         return delegate.output();
 252     }
 253 
 254     @Override
 255     public void addToContext(Object o) {
 256         delegate.addToContext(o);
 257     }
 258 
 259     @Override
 260     public void removeFromContext(Object o) {
 261         delegate.removeFromContext(o);
 262     }
 263 
 264 }