Monster merge from the integration branch. Still some problems left and JUnits failing.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / TraceDebug.java
1 package org.eclipse.linuxtools.lttng;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5
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;
11
12 public class TraceDebug {
13 static boolean DEBUG = false;
14 static boolean INFO = false;
15 static boolean WARN = false;
16
17 private static Plugin plugin = LTTngCorePlugin.getDefault();
18 private static String pluginID = LTTngCorePlugin.PLUGIN_ID;
19 private static SimpleDateFormat stimeformat = new SimpleDateFormat(
20 "HH:mm:ss:SSS");
21
22 public static void init() {
23 // Update JniTrace configuration options
24 String debugTrace = Platform.getDebugOption(pluginID + "/debug");
25 String infoTrace = Platform.getDebugOption(pluginID + "/info");
26 String warnTrace = Platform.getDebugOption(pluginID + "/warn");
27
28 if (debugTrace != null) {
29 DEBUG = (Boolean.valueOf(debugTrace)).booleanValue();
30 }
31
32 if (infoTrace != null) {
33 INFO = (Boolean.valueOf(infoTrace)).booleanValue();
34 }
35
36 if (warnTrace != null) {
37 WARN = (Boolean.valueOf(warnTrace)).booleanValue();
38 }
39 }
40
41 public static void info(String message) {
42 if (INFO) {
43 ILog logger = plugin.getLog();
44 logger.log(new Status(IStatus.INFO, LTTngCorePlugin.PLUGIN_ID,
45 IStatus.OK, message, null));
46 }
47 }
48
49 public static void warn(String message) {
50 if (WARN) {
51 ILog logger = plugin.getLog();
52 logger.log(new Status(IStatus.WARNING, LTTngCorePlugin.PLUGIN_ID,
53 IStatus.WARNING, message, null));
54 }
55 }
56
57 public static void debug(String message) {
58 if (DEBUG) {
59 String location = getCallingLocation();
60 System.out.println(location + "\n\t-> " + message);
61
62 }
63 }
64
65 public static void debug(String message, int additionalStackLines) {
66 if (DEBUG) {
67 String location = getCallingLocation(additionalStackLines);
68 System.out.println(location + "\n\t-> " + message);
69 }
70 }
71
72 public static void throwException(String message) {
73 if (DEBUG) {
74 try {
75 triggerException(message);
76 } catch (Exception e) {
77 e.printStackTrace();
78 }
79 }
80 }
81
82 private static void triggerException(String message) throws Exception {
83 throw new Exception(message);
84 }
85
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));
90 return sb.toString();
91 }
92
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 int max = Thread.currentThread().getStackTrace().length - 1;
99 earliestRequested = earliestRequested > max ? max : earliestRequested;
100 for (int i = earliestRequested; i >= stackCalledFromIdx; i--) {
101 sb.append(trace(Thread.currentThread().getStackTrace(), i) + "\n");
102 }
103 return sb.toString();
104 }
105
106 private static String trace(StackTraceElement e[], int level) {
107 if (e != null) {
108 level = level >= e.length ? e.length - 1 : level;
109 StackTraceElement s = e[level];
110 if (s != null) {
111 String simpleClassName = s.getClassName();
112 String[] clsNameSegs = simpleClassName.split("\\.");
113 if (clsNameSegs.length > 0)
114 simpleClassName = clsNameSegs[clsNameSegs.length - 1];
115 return stimeformat.format(new Date()) + " " + simpleClassName
116 + "." + s.getLineNumber() + "." + s.getMethodName();
117 }
118 }
119 return null;
120 }
121
122 public static boolean setDEBUG(boolean newValue) {
123 boolean oldValue = DEBUG;
124 DEBUG = newValue;
125 return oldValue;
126 }
127
128 public static boolean setINFO(boolean newValue) {
129 boolean oldValue = INFO;
130 INFO = newValue;
131 return oldValue;
132 }
133
134 public static boolean setWARN(boolean newValue) {
135 boolean oldValue = WARN;
136 WARN = newValue;
137 return oldValue;
138 }
139
140 public static boolean isDEBUG() {
141 return DEBUG;
142 }
143
144 public static boolean isINFO() {
145 return INFO;
146 }
147
148 public static boolean isWARN() {
149 return WARN;
150 }
151 }
This page took 0.034083 seconds and 5 git commands to generate.