| 1 | package org.eclipse.linuxtools.lttng;\r |
| 2 | \r |
| 3 | import java.text.SimpleDateFormat;\r |
| 4 | import java.util.Date;\r |
| 5 | \r |
| 6 | import org.eclipse.core.runtime.ILog;\r |
| 7 | import org.eclipse.core.runtime.IStatus;\r |
| 8 | import org.eclipse.core.runtime.Platform;\r |
| 9 | import org.eclipse.core.runtime.Plugin;\r |
| 10 | import org.eclipse.core.runtime.Status;\r |
| 11 | \r |
| 12 | @SuppressWarnings("nls")\r |
| 13 | public class TraceDebug {\r |
| 14 | static boolean DEBUG = false;\r |
| 15 | static boolean INFO = false;\r |
| 16 | static boolean WARN = false;\r |
| 17 | \r |
| 18 | private static Plugin plugin = LTTngCorePlugin.getDefault();\r |
| 19 | private static String pluginID = LTTngCorePlugin.PLUGIN_ID;\r |
| 20 | private static SimpleDateFormat stimeformat = new SimpleDateFormat(\r |
| 21 | "HH:mm:ss:SSS");\r |
| 22 | \r |
| 23 | public static void init() {\r |
| 24 | // Update JniTrace configuration options\r |
| 25 | String debugTrace = Platform.getDebugOption(pluginID + "/debug");\r |
| 26 | String infoTrace = Platform.getDebugOption(pluginID + "/info");\r |
| 27 | String warnTrace = Platform.getDebugOption(pluginID + "/warn");\r |
| 28 | \r |
| 29 | if (debugTrace != null) {\r |
| 30 | DEBUG = (Boolean.valueOf(debugTrace)).booleanValue();\r |
| 31 | }\r |
| 32 | \r |
| 33 | if (infoTrace != null) {\r |
| 34 | INFO = (Boolean.valueOf(infoTrace)).booleanValue();\r |
| 35 | }\r |
| 36 | \r |
| 37 | if (warnTrace != null) {\r |
| 38 | WARN = (Boolean.valueOf(warnTrace)).booleanValue();\r |
| 39 | }\r |
| 40 | }\r |
| 41 | \r |
| 42 | public static void info(String message) {\r |
| 43 | if (INFO) {\r |
| 44 | ILog logger = plugin.getLog();\r |
| 45 | logger.log(new Status(IStatus.INFO, LTTngCorePlugin.PLUGIN_ID,\r |
| 46 | IStatus.OK, message, null));\r |
| 47 | }\r |
| 48 | }\r |
| 49 | \r |
| 50 | public static void warn(String message) {\r |
| 51 | if (WARN) {\r |
| 52 | ILog logger = plugin.getLog();\r |
| 53 | logger.log(new Status(IStatus.WARNING, LTTngCorePlugin.PLUGIN_ID,\r |
| 54 | IStatus.WARNING, message, null));\r |
| 55 | }\r |
| 56 | }\r |
| 57 | \r |
| 58 | public static void debug(String message) {\r |
| 59 | if (DEBUG) {\r |
| 60 | String location = getCallingLocation();\r |
| 61 | System.out.println(location + "\n\t-> " + message);\r |
| 62 | \r |
| 63 | }\r |
| 64 | }\r |
| 65 | \r |
| 66 | public static void debug(String message, int additionalStackLines) {\r |
| 67 | if (DEBUG) {\r |
| 68 | String location = getCallingLocation(additionalStackLines);\r |
| 69 | System.out.println(location + "\n\t-> " + message);\r |
| 70 | }\r |
| 71 | }\r |
| 72 | \r |
| 73 | public static void throwException(String message) {\r |
| 74 | if (DEBUG) {\r |
| 75 | try {\r |
| 76 | triggerException(message);\r |
| 77 | } catch (Exception e) {\r |
| 78 | e.printStackTrace();\r |
| 79 | }\r |
| 80 | }\r |
| 81 | }\r |
| 82 | \r |
| 83 | private static void triggerException(String message) throws Exception {\r |
| 84 | throw new Exception(message);\r |
| 85 | }\r |
| 86 | \r |
| 87 | private static String getCallingLocation() {\r |
| 88 | StringBuilder sb = new StringBuilder();\r |
| 89 | sb.append(trace(Thread.currentThread().getStackTrace(), 4));\r |
| 90 | sb.append("\n" + trace(Thread.currentThread().getStackTrace(), 3));\r |
| 91 | return sb.toString();\r |
| 92 | }\r |
| 93 | \r |
| 94 | private static String getCallingLocation(int numOfStackLines) {\r |
| 95 | int stackCalledFromIdx = 3;\r |
| 96 | int earliestRequested = numOfStackLines > 0 ? stackCalledFromIdx\r |
| 97 | + numOfStackLines : stackCalledFromIdx;\r |
| 98 | StringBuilder sb = new StringBuilder();\r |
| 99 | int max = Thread.currentThread().getStackTrace().length - 1;\r |
| 100 | earliestRequested = earliestRequested > max ? max : earliestRequested;\r |
| 101 | for (int i = earliestRequested; i >= stackCalledFromIdx; i--) {\r |
| 102 | sb.append(trace(Thread.currentThread().getStackTrace(), i) + "\n");\r |
| 103 | }\r |
| 104 | return sb.toString();\r |
| 105 | }\r |
| 106 | \r |
| 107 | private static String trace(StackTraceElement e[], int level) {\r |
| 108 | if (e != null) {\r |
| 109 | level = level >= e.length ? e.length - 1 : level;\r |
| 110 | StackTraceElement s = e[level];\r |
| 111 | if (s != null) {\r |
| 112 | String simpleClassName = s.getClassName();\r |
| 113 | String[] clsNameSegs = simpleClassName.split("\\.");\r |
| 114 | if (clsNameSegs.length > 0)\r |
| 115 | simpleClassName = clsNameSegs[clsNameSegs.length - 1];\r |
| 116 | return stimeformat.format(new Date()) + " " + simpleClassName\r |
| 117 | + "." + s.getLineNumber() + "." + s.getMethodName();\r |
| 118 | }\r |
| 119 | }\r |
| 120 | return null;\r |
| 121 | }\r |
| 122 | \r |
| 123 | public static boolean setDEBUG(boolean newValue) {\r |
| 124 | boolean oldValue = DEBUG;\r |
| 125 | DEBUG = newValue;\r |
| 126 | return oldValue;\r |
| 127 | }\r |
| 128 | \r |
| 129 | public static boolean setINFO(boolean newValue) {\r |
| 130 | boolean oldValue = INFO;\r |
| 131 | INFO = newValue;\r |
| 132 | return oldValue;\r |
| 133 | }\r |
| 134 | \r |
| 135 | public static boolean setWARN(boolean newValue) {\r |
| 136 | boolean oldValue = WARN;\r |
| 137 | WARN = newValue;\r |
| 138 | return oldValue;\r |
| 139 | }\r |
| 140 | \r |
| 141 | public static boolean isDEBUG() {\r |
| 142 | return DEBUG;\r |
| 143 | }\r |
| 144 | \r |
| 145 | public static boolean isINFO() {\r |
| 146 | return INFO;\r |
| 147 | }\r |
| 148 | \r |
| 149 | public static boolean isWARN() {\r |
| 150 | return WARN;\r |
| 151 | }\r |
| 152 | }\r |