1 package org
.eclipse
.linuxtools
.internal
.lttng
.core
;
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 @SuppressWarnings("nls")
13 public class TraceDebug
{
14 static boolean DEBUG
= false;
15 static boolean INFO
= false;
16 static boolean WARN
= false;
18 private static Plugin plugin
= Activator
.getDefault();
19 private static String pluginID
= Activator
.PLUGIN_ID
;
20 private static SimpleDateFormat stimeformat
= new SimpleDateFormat(
23 public static void init() {
24 // Update JniTrace configuration options
25 String debugTrace
= Platform
.getDebugOption(pluginID
+ "/debug");
26 String infoTrace
= Platform
.getDebugOption(pluginID
+ "/info");
27 String warnTrace
= Platform
.getDebugOption(pluginID
+ "/warn");
29 if (debugTrace
!= null) {
30 DEBUG
= (Boolean
.valueOf(debugTrace
)).booleanValue();
33 if (infoTrace
!= null) {
34 INFO
= (Boolean
.valueOf(infoTrace
)).booleanValue();
37 if (warnTrace
!= null) {
38 WARN
= (Boolean
.valueOf(warnTrace
)).booleanValue();
42 public static void info(String message
) {
44 ILog logger
= plugin
.getLog();
45 logger
.log(new Status(IStatus
.INFO
, Activator
.PLUGIN_ID
,
46 IStatus
.OK
, message
, null));
50 public static void warn(String message
) {
52 ILog logger
= plugin
.getLog();
53 logger
.log(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
,
54 IStatus
.WARNING
, message
, null));
58 public static void debug(String message
) {
60 String location
= getCallingLocation();
61 System
.out
.println(location
+ "\n\t-> " + message
);
66 public static void debug(String message
, int additionalStackLines
) {
68 String location
= getCallingLocation(additionalStackLines
);
69 System
.out
.println(location
+ "\n\t-> " + message
);
73 public static void throwException(String message
) {
76 triggerException(message
);
77 } catch (Exception e
) {
78 Activator
.getDefault().logError("Unexpected Error", e
); //$NON-NLS-1$
83 private static void triggerException(String message
) throws Exception
{
84 throw new Exception(message
);
87 private static String
getCallingLocation() {
88 StringBuilder sb
= new StringBuilder();
89 sb
.append(trace(Thread
.currentThread().getStackTrace(), 4));
90 sb
.append("\n" + trace(Thread
.currentThread().getStackTrace(), 3));
94 private static String
getCallingLocation(int numOfStackLines
) {
95 int stackCalledFromIdx
= 3;
96 int earliestRequested
= numOfStackLines
> 0 ? stackCalledFromIdx
97 + numOfStackLines
: stackCalledFromIdx
;
98 StringBuilder sb
= new StringBuilder();
99 int max
= Thread
.currentThread().getStackTrace().length
- 1;
100 earliestRequested
= earliestRequested
> max ? max
: earliestRequested
;
101 for (int i
= earliestRequested
; i
>= stackCalledFromIdx
; i
--) {
102 sb
.append(trace(Thread
.currentThread().getStackTrace(), i
) + "\n");
104 return sb
.toString();
107 private static String
trace(StackTraceElement e
[], int level
) {
109 level
= level
>= e
.length ? e
.length
- 1 : level
;
110 StackTraceElement s
= e
[level
];
112 String simpleClassName
= s
.getClassName();
113 String
[] clsNameSegs
= simpleClassName
.split("\\.");
114 if (clsNameSegs
.length
> 0)
115 simpleClassName
= clsNameSegs
[clsNameSegs
.length
- 1];
116 return stimeformat
.format(new Date()) + " " + simpleClassName
117 + "." + s
.getLineNumber() + "." + s
.getMethodName();
123 public static boolean setDEBUG(boolean newValue
) {
124 boolean oldValue
= DEBUG
;
129 public static boolean setINFO(boolean newValue
) {
130 boolean oldValue
= INFO
;
135 public static boolean setWARN(boolean newValue
) {
136 boolean oldValue
= WARN
;
141 public static boolean isDEBUG() {
145 public static boolean isINFO() {
149 public static boolean isWARN() {