1 package org
.eclipse
.linuxtools
.lttng
.ui
;
4 import java
.io
.FileWriter
;
5 import java
.io
.IOException
;
6 import java
.io
.PrintWriter
;
7 import java
.text
.SimpleDateFormat
;
10 import org
.eclipse
.core
.runtime
.ILog
;
11 import org
.eclipse
.core
.runtime
.IStatus
;
12 import org
.eclipse
.core
.runtime
.Platform
;
13 import org
.eclipse
.core
.runtime
.Plugin
;
14 import org
.eclipse
.core
.runtime
.Status
;
16 @SuppressWarnings("nls")
17 public class TraceDebug
{
18 static boolean DEBUG
= false;
19 static boolean INFO
= false;
20 static boolean WARN
= false;
22 static boolean CFV
= false;
23 static boolean RV
= false;
24 static boolean SV
= false;
26 private static Plugin plugin
= LTTngUiPlugin
.getDefault();
27 private static String pluginID
= LTTngUiPlugin
.PLUGIN_ID
;
28 private static SimpleDateFormat stimeformat
= new SimpleDateFormat("HH:mm:ss:SSS");
30 // Note: files are created in $HOME
31 static private PrintWriter fCFVfile
= null;
32 static private PrintWriter fRVfile
= null;
33 static private PrintWriter fSVfile
= null;
35 public static void init() {
36 // Update Trace configuration options
37 String debugTrace
= Platform
.getDebugOption(pluginID
+ "/debug");
38 String infoTrace
= Platform
.getDebugOption(pluginID
+ "/info");
39 String warnTrace
= Platform
.getDebugOption(pluginID
+ "/warn");
41 if (debugTrace
!= null) {
42 DEBUG
= (new Boolean(debugTrace
)).booleanValue();
45 if (infoTrace
!= null) {
46 INFO
= (new Boolean(infoTrace
)).booleanValue();
49 if (warnTrace
!= null) {
50 WARN
= (new Boolean(warnTrace
)).booleanValue();
53 String cfvTrace
= Platform
.getDebugOption(pluginID
+ "/cfv");
54 if (cfvTrace
!= null) {
55 CFV
= (new Boolean(cfvTrace
)).booleanValue();
58 fCFVfile
= new PrintWriter(new FileWriter("CFVTrace.txt"));
59 } catch (IOException e
) {
65 String rvTrace
= Platform
.getDebugOption(pluginID
+ "/rv");
66 if (rvTrace
!= null) {
67 RV
= (new Boolean(rvTrace
)).booleanValue();
70 fRVfile
= new PrintWriter(new FileWriter("RVTrace.txt"));
71 } catch (IOException e
) {
77 String svTrace
= Platform
.getDebugOption(pluginID
+ "/sv");
78 if (svTrace
!= null) {
79 SV
= (new Boolean(svTrace
)).booleanValue();
82 fSVfile
= new PrintWriter(new FileWriter("SVTrace.txt"));
83 } catch (IOException e
) {
90 public static void stop() {
91 if (fCFVfile
!= null) {
96 if (fRVfile
!= null) {
101 if (fSVfile
!= null) {
107 public static void traceCFV(String trace
) {
108 if (CFV
&& fCFVfile
!= null) {
109 fCFVfile
.println(trace
);
114 public static void traceRV(String trace
) {
115 if (RV
&& fRVfile
!= null) {
116 fRVfile
.println(trace
);
121 public static void traceSV(String trace
) {
122 if (SV
&& fSVfile
!= null) {
123 fSVfile
.println(trace
);
128 public static void info(String message
) {
130 ILog logger
= plugin
.getLog();
131 logger
.log(new Status(IStatus
.INFO
, LTTngUiPlugin
.PLUGIN_ID
, IStatus
.OK
, message
, null));
135 public static void warn(String message
) {
137 ILog logger
= plugin
.getLog();
138 logger
.log(new Status(IStatus
.WARNING
, LTTngUiPlugin
.PLUGIN_ID
, IStatus
.WARNING
, message
, null));
142 public static void debug(String message
) {
144 String location
= getCallingLocation();
145 System
.out
.println(location
+ "\n\t-> " + message
);
150 public static void debug(String message
, int additionalStackLines
) {
152 String location
= getCallingLocation(additionalStackLines
);
153 System
.out
.println(location
+ "\n\t-> " + message
);
157 public static void throwException(String message
) {
160 triggerException(message
);
161 } catch (Exception e
) {
167 private static void triggerException(String message
) throws Exception
{
168 throw new Exception(message
);
171 private static String
getCallingLocation() {
172 StringBuilder sb
= new StringBuilder();
173 sb
.append(trace(Thread
.currentThread().getStackTrace(), 4));
174 sb
.append("\n" + trace(Thread
.currentThread().getStackTrace(), 3));
175 return sb
.toString();
178 private static String
getCallingLocation(int numOfStackLines
) {
179 int stackCalledFromIdx
= 3;
180 int earliestRequested
= numOfStackLines
> 0 ? stackCalledFromIdx
+ numOfStackLines
: stackCalledFromIdx
;
181 StringBuilder sb
= new StringBuilder();
182 for (int i
= earliestRequested
; i
>= stackCalledFromIdx
; i
--) {
183 sb
.append(trace(Thread
.currentThread().getStackTrace(), i
) + "\n");
185 return sb
.toString();
188 private static String
trace(StackTraceElement e
[], int level
) {
190 level
= level
>= e
.length ? e
.length
- 1 : level
;
191 StackTraceElement s
= e
[level
];
193 String simpleClassName
= s
.getClassName();
194 String
[] clsNameSegs
= simpleClassName
.split("\\.");
195 if (clsNameSegs
.length
> 0)
196 simpleClassName
= clsNameSegs
[clsNameSegs
.length
- 1];
197 return stimeformat
.format(new Date()) + " " + simpleClassName
+ "." + s
.getLineNumber() + "." + s
.getMethodName();
204 public static boolean isDEBUG() {
208 public static boolean isINFO() {
212 public static boolean isWARN() {
216 public static boolean isCFV() {
220 public static boolean isRV() {
224 public static boolean isSV() {