2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / TraceDebug.java
CommitLineData
6e512b93
ASL
1package org.eclipse.linuxtools.lttng.ui;\r
2\r
0c2a2e08
FC
3import java.io.FileWriter;\r
4import java.io.IOException;\r
5import java.io.PrintWriter;\r
6e512b93
ASL
6import java.text.SimpleDateFormat;\r
7import java.util.Date;\r
8\r
9import org.eclipse.core.runtime.ILog;\r
10import org.eclipse.core.runtime.IStatus;\r
11import org.eclipse.core.runtime.Platform;\r
12import org.eclipse.core.runtime.Plugin;\r
13import org.eclipse.core.runtime.Status;\r
14\r
3b38ea61 15@SuppressWarnings("nls")\r
6e512b93
ASL
16public class TraceDebug {\r
17 static boolean DEBUG = false;\r
18 static boolean INFO = false;\r
19 static boolean WARN = false;\r
20\r
0c2a2e08
FC
21 static boolean CFV = false;\r
22 static boolean RV = false;\r
23\r
6e512b93
ASL
24 private static Plugin plugin = LTTngUiPlugin.getDefault();\r
25 private static String pluginID = LTTngUiPlugin.PLUGIN_ID;\r
3b38ea61 26 private static SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss:SSS");\r
6e512b93 27\r
0c2a2e08
FC
28 // Note: files are created in $HOME\r
29 static private PrintWriter fCFVfile = null;\r
30 static private PrintWriter fRVfile = null;\r
31\r
6e512b93
ASL
32 public static void init() {\r
33 // Update Trace configuration options\r
34 String debugTrace = Platform.getDebugOption(pluginID + "/debug");\r
35 String infoTrace = Platform.getDebugOption(pluginID + "/info");\r
36 String warnTrace = Platform.getDebugOption(pluginID + "/warn");\r
37\r
38 if (debugTrace != null) {\r
39 DEBUG = (new Boolean(debugTrace)).booleanValue();\r
40 }\r
41\r
42 if (infoTrace != null) {\r
43 INFO = (new Boolean(infoTrace)).booleanValue();\r
44 }\r
45\r
46 if (warnTrace != null) {\r
47 WARN = (new Boolean(warnTrace)).booleanValue();\r
48 }\r
0c2a2e08
FC
49\r
50 String cfvTrace = Platform.getDebugOption(pluginID + "/cfv");\r
51 if (cfvTrace != null) {\r
52 CFV = (new Boolean(cfvTrace)).booleanValue();\r
53 if (CFV) {\r
54 try {\r
55 fCFVfile = new PrintWriter(new FileWriter("CFVTrace.txt"));\r
56 } catch (IOException e) {\r
57 e.printStackTrace();\r
58 }\r
59 }\r
60 }\r
61\r
62 String rvTrace = Platform.getDebugOption(pluginID + "/rv");\r
63 if (rvTrace != null) {\r
64 RV = (new Boolean(rvTrace)).booleanValue();\r
65 if (RV) {\r
66 try {\r
67 fRVfile = new PrintWriter(new FileWriter("RVTrace.txt"));\r
68 } catch (IOException e) {\r
69 e.printStackTrace();\r
70 }\r
71 }\r
72 }\r
73 }\r
74\r
75 public static void stop() {\r
76 if (fCFVfile != null) {\r
77 fCFVfile.close();\r
78 fCFVfile = null;\r
79 }\r
80\r
81 if (fRVfile != null) {\r
82 fRVfile.close();\r
83 fRVfile = null;\r
84 }\r
85 }\r
86\r
87 public static void traceCFV(String trace) {\r
88 if (CFV && fCFVfile != null) {\r
89 fCFVfile.println(trace);\r
90 fCFVfile.flush();\r
91 }\r
92 }\r
93\r
94 public static void traceRV(String trace) {\r
95 if (RV && fRVfile != null) {\r
96 fRVfile.println(trace);\r
97 fRVfile.flush();\r
98 }\r
6e512b93
ASL
99 }\r
100\r
101 public static void info(String message) {\r
102 if (INFO) {\r
103 ILog logger = plugin.getLog();\r
104 logger.log(new Status(IStatus.INFO, LTTngUiPlugin.PLUGIN_ID,\r
105 IStatus.OK, message, null));\r
106 }\r
107 }\r
108\r
109 public static void warn(String message) {\r
110 if (WARN) {\r
111 ILog logger = plugin.getLog();\r
112 logger.log(new Status(IStatus.WARNING, LTTngUiPlugin.PLUGIN_ID,\r
113 IStatus.WARNING, message, null));\r
114 }\r
115 }\r
116\r
117 public static void debug(String message) {\r
118 if (DEBUG) {\r
119 String location = getCallingLocation();\r
120 System.out.println(location + "\n\t-> " + message);\r
121\r
122 }\r
123 }\r
124\r
125 public static void debug(String message, int additionalStackLines) {\r
126 if (DEBUG) {\r
127 String location = getCallingLocation(additionalStackLines);\r
128 System.out.println(location + "\n\t-> " + message);\r
129 }\r
130 }\r
131\r
132 public static void throwException(String message) {\r
133 if (DEBUG) {\r
134 try {\r
135 triggerException(message);\r
136 } catch (Exception e) {\r
137 e.printStackTrace();\r
138 }\r
139 }\r
140 }\r
141\r
142 private static void triggerException(String message) throws Exception {\r
143 throw new Exception(message);\r
144 }\r
145\r
146 private static String getCallingLocation() {\r
147 StringBuilder sb = new StringBuilder();\r
148 sb.append(trace(Thread.currentThread().getStackTrace(), 4));\r
149 sb.append("\n" + trace(Thread.currentThread().getStackTrace(), 3));\r
150 return sb.toString();\r
151 }\r
152\r
153 private static String getCallingLocation(int numOfStackLines) {\r
154 int stackCalledFromIdx = 3;\r
155 int earliestRequested = numOfStackLines > 0 ? stackCalledFromIdx\r
156 + numOfStackLines : stackCalledFromIdx;\r
157 StringBuilder sb = new StringBuilder();\r
158 for (int i = earliestRequested; i >= stackCalledFromIdx; i--) {\r
159 sb.append(trace(Thread.currentThread().getStackTrace(), i) + "\n");\r
160 }\r
161 return sb.toString();\r
162 }\r
163\r
164 private static String trace(StackTraceElement e[], int level) {\r
8827c197
FC
165 if (e != null) {\r
166 level = level >= e.length ? e.length - 1 : level;\r
6e512b93
ASL
167 StackTraceElement s = e[level];\r
168 if (s != null) {\r
169 String simpleClassName = s.getClassName();\r
170 String[] clsNameSegs = simpleClassName.split("\\.");\r
171 if (clsNameSegs.length > 0)\r
172 simpleClassName = clsNameSegs[clsNameSegs.length - 1];\r
173 return stimeformat.format(new Date()) + " " + simpleClassName\r
174 + "." + s.getLineNumber() + "." + s.getMethodName();\r
175 }\r
176 }\r
8827c197 177\r
6e512b93
ASL
178 return null;\r
179 }\r
180\r
181 public static boolean isDEBUG() {\r
182 return DEBUG;\r
183 }\r
184\r
185 public static boolean isINFO() {\r
186 return INFO;\r
187 }\r
188\r
189 public static boolean isWARN() {\r
190 return WARN;\r
191 }\r
0c2a2e08
FC
192\r
193 public static boolean isCFV() {\r
194 return CFV;\r
195 }\r
196\r
197 public static boolean isRV() {\r
198 return RV;\r
199 }\r
6e512b93 200}\r
This page took 0.035226 seconds and 5 git commands to generate.