Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / 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 @SuppressWarnings("nls")
13 public class TraceDebug {
14 static boolean DEBUG = false;
15 static boolean INFO = false;
16 static boolean WARN = false;
17
18 private static Plugin plugin = LTTngCorePlugin.getDefault();
19 private static String pluginID = LTTngCorePlugin.PLUGIN_ID;
20 private static SimpleDateFormat stimeformat = new SimpleDateFormat(
21 "HH:mm:ss:SSS");
22
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");
28
29 if (debugTrace != null) {
30 DEBUG = (Boolean.valueOf(debugTrace)).booleanValue();
31 }
32
33 if (infoTrace != null) {
34 INFO = (Boolean.valueOf(infoTrace)).booleanValue();
35 }
36
37 if (warnTrace != null) {
38 WARN = (Boolean.valueOf(warnTrace)).booleanValue();
39 }
40 }
41
42 public static void info(String message) {
43 if (INFO) {
44 ILog logger = plugin.getLog();
45 logger.log(new Status(IStatus.INFO, LTTngCorePlugin.PLUGIN_ID,
46 IStatus.OK, message, null));
47 }
48 }
49
50 public static void warn(String message) {
51 if (WARN) {
52 ILog logger = plugin.getLog();
53 logger.log(new Status(IStatus.WARNING, LTTngCorePlugin.PLUGIN_ID,
54 IStatus.WARNING, message, null));
55 }
56 }
57
58 public static void debug(String message) {
59 if (DEBUG) {
60 String location = getCallingLocation();
61 System.out.println(location + "\n\t-> " + message);
62
63 }
64 }
65
66 public static void debug(String message, int additionalStackLines) {
67 if (DEBUG) {
68 String location = getCallingLocation(additionalStackLines);
69 System.out.println(location + "\n\t-> " + message);
70 }
71 }
72
73 public static void throwException(String message) {
74 if (DEBUG) {
75 try {
76 triggerException(message);
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81 }
82
83 private static void triggerException(String message) throws Exception {
84 throw new Exception(message);
85 }
86
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));
91 return sb.toString();
92 }
93
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");
103 }
104 return sb.toString();
105 }
106
107 private static String trace(StackTraceElement e[], int level) {
108 if (e != null) {
109 level = level >= e.length ? e.length - 1 : level;
110 StackTraceElement s = e[level];
111 if (s != null) {
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();
118 }
119 }
120 return null;
121 }
122
123 public static boolean setDEBUG(boolean newValue) {
124 boolean oldValue = DEBUG;
125 DEBUG = newValue;
126 return oldValue;
127 }
128
129 public static boolean setINFO(boolean newValue) {
130 boolean oldValue = INFO;
131 INFO = newValue;
132 return oldValue;
133 }
134
135 public static boolean setWARN(boolean newValue) {
136 boolean oldValue = WARN;
137 WARN = newValue;
138 return oldValue;
139 }
140
141 public static boolean isDEBUG() {
142 return DEBUG;
143 }
144
145 public static boolean isINFO() {
146 return INFO;
147 }
148
149 public static boolean isWARN() {
150 return WARN;
151 }
152 }
This page took 0.035369 seconds and 5 git commands to generate.