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