1 package org
.eclipse
.linuxtools
.lttng
.ui
;
3 import java
.text
.SimpleDateFormat
;
6 import org
.eclipse
.core
.runtime
.ILog
;
7 import org
.eclipse
.core
.runtime
.IStatus
;
8 import org
.eclipse
.core
.runtime
.Platform
;
9 import org
.eclipse
.core
.runtime
.Plugin
;
10 import org
.eclipse
.core
.runtime
.Status
;
12 public class TraceDebug
{
13 static boolean DEBUG
= false;
14 static boolean INFO
= false;
15 static boolean WARN
= false;
17 private static Plugin plugin
= LTTngUiPlugin
.getDefault();
18 private static String pluginID
= LTTngUiPlugin
.PLUGIN_ID
;
19 private static SimpleDateFormat stimeformat
= new SimpleDateFormat(
22 public static void init() {
23 // Update Trace configuration options
24 String debugTrace
= Platform
.getDebugOption(pluginID
+ "/debug");
25 String infoTrace
= Platform
.getDebugOption(pluginID
+ "/info");
26 String warnTrace
= Platform
.getDebugOption(pluginID
+ "/warn");
28 if (debugTrace
!= null) {
29 DEBUG
= (new Boolean(debugTrace
)).booleanValue();
32 if (infoTrace
!= null) {
33 INFO
= (new Boolean(infoTrace
)).booleanValue();
36 if (warnTrace
!= null) {
37 WARN
= (new Boolean(warnTrace
)).booleanValue();
41 public static void info(String message
) {
43 ILog logger
= plugin
.getLog();
44 logger
.log(new Status(IStatus
.INFO
, LTTngUiPlugin
.PLUGIN_ID
,
45 IStatus
.OK
, message
, null));
49 public static void warn(String message
) {
51 ILog logger
= plugin
.getLog();
52 logger
.log(new Status(IStatus
.WARNING
, LTTngUiPlugin
.PLUGIN_ID
,
53 IStatus
.WARNING
, message
, null));
57 public static void debug(String message
) {
59 String location
= getCallingLocation();
60 System
.out
.println(location
+ "\n\t-> " + message
);
65 public static void debug(String message
, int additionalStackLines
) {
67 String location
= getCallingLocation(additionalStackLines
);
68 System
.out
.println(location
+ "\n\t-> " + message
);
72 public static void throwException(String message
) {
75 triggerException(message
);
76 } catch (Exception e
) {
82 private static void triggerException(String message
) throws Exception
{
83 throw new Exception(message
);
86 private static String
getCallingLocation() {
87 StringBuilder sb
= new StringBuilder();
88 sb
.append(trace(Thread
.currentThread().getStackTrace(), 4));
89 sb
.append("\n" + trace(Thread
.currentThread().getStackTrace(), 3));
93 private static String
getCallingLocation(int numOfStackLines
) {
94 int stackCalledFromIdx
= 3;
95 int earliestRequested
= numOfStackLines
> 0 ? stackCalledFromIdx
96 + numOfStackLines
: stackCalledFromIdx
;
97 StringBuilder sb
= new StringBuilder();
98 for (int i
= earliestRequested
; i
>= stackCalledFromIdx
; i
--) {
99 sb
.append(trace(Thread
.currentThread().getStackTrace(), i
) + "\n");
101 return sb
.toString();
104 private static String
trace(StackTraceElement e
[], int level
) {
106 level
= level
>= e
.length ? e
.length
- 1 : level
;
107 StackTraceElement s
= e
[level
];
109 String simpleClassName
= s
.getClassName();
110 String
[] clsNameSegs
= simpleClassName
.split("\\.");
111 if (clsNameSegs
.length
> 0)
112 simpleClassName
= clsNameSegs
[clsNameSegs
.length
- 1];
113 return stimeformat
.format(new Date()) + " " + simpleClassName
114 + "." + s
.getLineNumber() + "." + s
.getMethodName();
121 public static boolean isDEBUG() {
125 public static boolean isINFO() {
129 public static boolean isWARN() {