001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ContinuationDebug.java 3811 2007-06-25 15:06:16Z gbevin $
007: */
008: package com.uwyn.rife.continuations.instrument;
009:
010: import com.uwyn.rife.tools.RawFormatter;
011: import java.util.logging.ConsoleHandler;
012: import java.util.logging.Handler;
013: import java.util.logging.Level;
014: import java.util.logging.Logger;
015:
016: /**
017: * Configures the debug output of the continuations engine.
018: * <p>Note that this has little use besides for developing on the
019: * continuations instrumentation itself.
020: *
021: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
022: * @version $Revision: 3811 $
023: * @since 1.6
024: */
025: public class ContinuationDebug {
026: static boolean sDebug = false;
027: static boolean sTrace = false;
028: static Level sLevel = Level.parse("FINEST");
029:
030: /**
031: * The logger instance that is used for the debugging.
032: * @since 1.6
033: */
034: public static final Logger LOGGER = Logger
035: .getLogger("com.uwyn.rife.continuations");
036:
037: private static Handler sHandler = null;
038:
039: static {
040: ConsoleHandler handler = new ConsoleHandler();
041: handler.setFormatter(new RawFormatter());
042: LOGGER.addHandler(handler);
043: sHandler = handler;
044: }
045:
046: /**
047: * Configures the tracing of the continuations instrumentation while it's
048: * executing.
049: *
050: * @param trace {@code true} if tracing should be enabled; or
051: * <p>{@code false} otherwise
052: * @since 1.6
053: */
054: public static void setTrace(boolean trace) {
055: sTrace = trace;
056: }
057:
058: /**
059: * Enables or disables debugging.
060: *
061: * @param debug {@code true} if debugging should be enabled; or
062: * <p>{@code false} otherwise
063: * @since 1.6
064: */
065: public static void setDebug(boolean debug) {
066: sDebug = debug;
067: reconfigure();
068: }
069:
070: private static void reconfigure() {
071: ///CLOVER:OFF
072: if (sDebug) {
073: sHandler.setLevel(sLevel);
074: LOGGER.setLevel(sLevel);
075: } else {
076: sHandler.setLevel(Level.OFF);
077: LOGGER.setLevel(Level.OFF);
078: }
079: ///CLOVER:ON
080: }
081:
082: /**
083: * An array with textual representations of the bytecode opcodes.
084: * This is mainly used during tracing.
085: */
086: public static final String[] OPCODES = { "NOP", "ACONST_NULL",
087: "ICONST_M1", "ICONST_0", "ICONST_1", "ICONST_2",
088: "ICONST_3", "ICONST_4", "ICONST_5", "LCONST_0", "LCONST_1",
089: "FCONST_0", "FCONST_1", "FCONST_2", "DCONST_0", "DCONST_1",
090: "BIPUSH", "SIPUSH", "LDC", null, null, "ILOAD", "LLOAD",
091: "FLOAD", "DLOAD", "ALOAD", null, null, null, null, null,
092: null, null, null, null, null, null, null, null, null, null,
093: null, null, null, null, null, "IALOAD", "LALOAD", "FALOAD",
094: "DALOAD", "AALOAD", "BALOAD", "CALOAD", "SALOAD", "ISTORE",
095: "LSTORE", "FSTORE", "DSTORE", "ASTORE", null, null, null,
096: null, null, null, null, null, null, null, null, null, null,
097: null, null, null, null, null, null, null, "IASTORE",
098: "LASTORE", "FASTORE", "DASTORE", "AASTORE", "BASTORE",
099: "CASTORE", "SASTORE", "POP", "POP2", "DUP", "DUP_X1",
100: "DUP_X2", "DUP2", "DUP2_X1", "DUP2_X2", "SWAP", "IADD",
101: "LADD", "FADD", "DADD", "ISUB", "LSUB", "FSUB", "DSUB",
102: "IMUL", "LMUL", "FMUL", "DMUL", "IDIV", "LDIV", "FDIV",
103: "DDIV", "IREM", "LREM", "FREM", "DREM", "INEG", "LNEG",
104: "FNEG", "DNEG", "ISHL", "LSHL", "ISHR", "LSHR", "IUSHR",
105: "LUSHR", "IAND", "LAND", "IOR", "LOR", "IXOR", "LXOR",
106: "IINC", "I2L", "I2F", "I2D", "L2I", "L2F", "L2D", "F2I",
107: "F2L", "F2D", "D2I", "D2L", "D2F", "I2B", "I2C", "I2S",
108: "LCMP", "FCMPL", "FCMPG", "DCMPL", "DCMPG", "IFEQ", "IFNE",
109: "IFLT", "IFGE", "IFGT", "IFLE", "IF_ICMPEQ", "IF_ICMPNE",
110: "IF_ICMPLT", "IF_ICMPGE", "IF_ICMPGT", "IF_ICMPLE",
111: "IF_ACMPEQ", "IF_ACMPNE", "GOTO", "JSR", "RET",
112: "TABLESWITCH", "LOOKUPSWITCH", "IRETURN", "LRETURN",
113: "FRETURN", "DRETURN", "ARETURN", "RETURN", "GETSTATIC",
114: "PUTSTATIC", "GETFIELD", "PUTFIELD", "INVOKEVIRTUAL",
115: "INVOKESPECIAL", "INVOKESTATIC", "INVOKEINTERFACE", null,
116: "NEW", "NEWARRAY", "ANEWARRAY", "ARRAYLENGTH", "ATHROW",
117: "CHECKCAST", "INSTANCEOF", "MONITORENTER", "MONITOREXIT",
118: null, "MULTIANEWARRAY", "IFNULL", "IFNONNULL", null, null };
119:
120: static String join(int[] array, String seperator) {
121: if (null == array) {
122: return null;
123: }
124:
125: if (null == seperator) {
126: seperator = "";
127: }
128:
129: if (0 == array.length) {
130: return "";
131: } else {
132: int current_index = 0;
133: String result = "";
134: while (current_index < array.length - 1) {
135: result = result + array[current_index] + seperator;
136: current_index++;
137: }
138:
139: result = result + array[current_index];
140: return result;
141: }
142: }
143:
144: static String join(Object[] array, String seperator) {
145: if (null == array) {
146: return null;
147: }
148:
149: if (null == seperator) {
150: seperator = "";
151: }
152:
153: if (0 == array.length) {
154: return "";
155: } else {
156: int current_index = 0;
157: String result = "";
158: while (current_index < array.length - 1) {
159: result = result + array[current_index] + seperator;
160: current_index++;
161: }
162:
163: result = result + array[current_index];
164: return result;
165: }
166:
167: }
168:
169: static String repeat(String source, int count) {
170: if (null == source) {
171: return null;
172: }
173:
174: StringBuilder new_string = new StringBuilder();
175: while (count > 0) {
176: new_string.append(source);
177: count--;
178: }
179:
180: return new_string.toString();
181: }
182: }
|