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 | |
550d787e | 29 | DEBUG = (Boolean.valueOf(debugTrace)).booleanValue();\r |
5d10d135 ASL |
30 | }\r |
31 | \r | |
32 | if (infoTrace != null) {\r | |
550d787e | 33 | INFO = (Boolean.valueOf(infoTrace)).booleanValue();\r |
5d10d135 ASL |
34 | }\r |
35 | \r | |
36 | if (warnTrace != null) {\r | |
550d787e | 37 | WARN = (Boolean.valueOf(warnTrace)).booleanValue();\r |
5d10d135 ASL |
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 | |
8827c197 FC |
98 | int max = Thread.currentThread().getStackTrace().length - 1;\r |
99 | earliestRequested = earliestRequested > max ? max : earliestRequested;\r | |
5d10d135 ASL |
100 | for (int i = earliestRequested; i >= stackCalledFromIdx; i--) {\r |
101 | sb.append(trace(Thread.currentThread().getStackTrace(), i) + "\n");\r | |
102 | }\r | |
103 | return sb.toString();\r | |
104 | }\r | |
105 | \r | |
106 | private static String trace(StackTraceElement e[], int level) {\r | |
8827c197 FC |
107 | if (e != null) {\r |
108 | level = level >= e.length ? e.length - 1 : level;\r | |
5d10d135 ASL |
109 | StackTraceElement s = e[level];\r |
110 | if (s != null) {\r | |
111 | String simpleClassName = s.getClassName();\r | |
112 | String[] clsNameSegs = simpleClassName.split("\\.");\r | |
113 | if (clsNameSegs.length > 0)\r | |
114 | simpleClassName = clsNameSegs[clsNameSegs.length - 1];\r | |
115 | return stimeformat.format(new Date()) + " " + simpleClassName\r | |
116 | + "." + s.getLineNumber() + "." + s.getMethodName();\r | |
117 | }\r | |
118 | }\r | |
119 | return null;\r | |
120 | }\r | |
121 | \r | |
a74d2fde FC |
122 | public static boolean setDEBUG(boolean newValue) {\r |
123 | boolean oldValue = DEBUG;\r | |
124 | DEBUG = newValue;\r | |
125 | return oldValue;\r | |
126 | }\r | |
127 | \r | |
128 | public static boolean setINFO(boolean newValue) {\r | |
129 | boolean oldValue = INFO;\r | |
130 | INFO = newValue;\r | |
131 | return oldValue;\r | |
132 | }\r | |
133 | \r | |
134 | public static boolean setWARN(boolean newValue) {\r | |
135 | boolean oldValue = WARN;\r | |
136 | WARN = newValue;\r | |
137 | return oldValue;\r | |
138 | }\r | |
139 | \r | |
5d10d135 ASL |
140 | public static boolean isDEBUG() {\r |
141 | return DEBUG;\r | |
142 | }\r | |
143 | \r | |
144 | public static boolean isINFO() {\r | |
145 | return INFO;\r | |
146 | }\r | |
147 | \r | |
148 | public static boolean isWARN() {\r | |
149 | return WARN;\r | |
150 | }\r | |
151 | }\r |