1 /* 2 * Copyright (c) 2015, 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 24 package jdk.internal.jshell.debug; 25 26 import java.io.PrintStream; 27 import java.util.HashMap; 28 import java.util.Map; 29 import jdk.jshell.JShell; 30 31 /** 32 /** 33 * This class is used to externally control output messages for debugging the 34 * implementation of the JShell API. 35 * <p> 36 * This is not part of the SPI, not API. 37 */ 38 public class InternalDebugControl { 39 40 /** 41 * This is a static only class; The constructor should never be called. 42 */ 43 private InternalDebugControl() { 44 } 45 46 /** 47 * General debugging. 48 */ 49 public static final int DBG_GEN = 0b0000001; 50 51 /** 52 * File manager debuging. 53 */ 54 public static final int DBG_FMGR = 0b0000010; 55 56 /** 57 * Completion analysis debugging. 58 */ 59 public static final int DBG_COMPA = 0b0000100; 60 61 /** 62 * Dependency debugging. 63 */ 64 public static final int DBG_DEP = 0b0001000; 65 66 /** 67 * Execution control channel debugging. 68 */ 69 public static final int DBG_EC = 0b0010000; 70 71 /** 72 * Event debugging. 73 */ 74 public static final int DBG_EVNT = 0b0010000; 75 76 private static Map<JShell, Integer> debugMap = null; 77 78 /** 79 * Sets which debug flags are enabled for a given JShell instance. The flags 80 * are or'ed bits as defined in {@code DBG_*}. 81 * 82 * @param state the JShell instance 83 * @param flags the or'ed debug bits 84 */ 85 public static void setDebugFlags(JShell state, int flags) { 86 if (debugMap == null) { 87 debugMap = new HashMap<>(); 88 } 89 debugMap.put(state, flags); 90 } 91 92 /** 93 * Tests if any of the specified debug flags are enabled. 94 * 95 * @param state the JShell instance 96 * @param flag the {@code DBG_*} bits to check 97 * @return true if any of the flags are enabled 98 */ 99 public static boolean isDebugEnabled(JShell state, int flag) { 100 if (debugMap == null) { 101 return false; 102 } 103 Integer flags = debugMap.get(state); 104 if (flags == null) { 105 return false; 106 } 107 return (flags & flag) != 0; 108 } 109 110 /** 111 * Displays debug info if the specified debug flags are enabled. 112 * 113 * @param state the current JShell instance 114 * @param err the {@code PrintStream} to report on 115 * @param flags {@code DBG_*} flag bits to check 116 * @param format format string for the output 117 * @param args args for the format string 118 */ 119 public static void debug(JShell state, PrintStream err, int flags, String format, Object... args) { 120 if (isDebugEnabled(state, flags)) { 121 err.printf(format, args); 122 } 123 } 124 125 /** 126 * Displays a fatal exception as debug info. 127 * 128 * @param state the current JShell instance 129 * @param err the {@code PrintStream} to report on 130 * @param ex the fatal Exception 131 * @param where additional context 132 */ 133 public static void debug(JShell state, PrintStream err, Throwable ex, String where) { 134 if (isDebugEnabled(state, 0xFFFFFFFF)) { 135 err.printf("Fatal error: %s: %s\n", where, ex.getMessage()); 136 ex.printStackTrace(err); 137 } 138 } 139 }