Merge in Tycho-ification work
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / Tracer.java
1 package org.eclipse.linuxtools.tmf;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6
7 import org.eclipse.core.runtime.Platform;
8 import org.eclipse.linuxtools.tmf.component.ITmfComponent;
9 import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;
10 import org.eclipse.linuxtools.tmf.event.TmfData;
11 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
12 import org.eclipse.linuxtools.tmf.signal.TmfSignal;
13
14 @SuppressWarnings("nls")
15 public class Tracer {
16
17 private static String pluginID = TmfCorePlugin.PLUGIN_ID;
18
19 static Boolean ERROR = Boolean.FALSE;
20 static Boolean WARNING = Boolean.FALSE;
21 static Boolean INFO = Boolean.FALSE;
22
23 static Boolean COMPONENT = Boolean.FALSE;
24 static Boolean REQUEST = Boolean.FALSE;
25 static Boolean SIGNAL = Boolean.FALSE;
26 static Boolean EVENT = Boolean.FALSE;
27
28 private static String LOGNAME = "trace.log";
29 private static BufferedWriter fTraceLog = null;
30
31 private static BufferedWriter openLogFile(String filename) {
32 BufferedWriter outfile = null;
33 try {
34 outfile = new BufferedWriter(new FileWriter(filename));
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 return outfile;
39 }
40
41 public static void init() {
42
43 String traceKey;
44 boolean isTracing = false;
45
46 traceKey = Platform.getDebugOption(pluginID + "/error");
47 if (traceKey != null) {
48 ERROR = (Boolean.valueOf(traceKey)).booleanValue();
49 isTracing |= ERROR;
50 }
51
52 traceKey = Platform.getDebugOption(pluginID + "/warning");
53 if (traceKey != null) {
54 WARNING = (Boolean.valueOf(traceKey)).booleanValue();
55 isTracing |= WARNING;
56 }
57
58 traceKey = Platform.getDebugOption(pluginID + "/info");
59 if (traceKey != null) {
60 INFO = (Boolean.valueOf(traceKey)).booleanValue();
61 isTracing |= INFO;
62 }
63
64 traceKey = Platform.getDebugOption(pluginID + "/component");
65 if (traceKey != null) {
66 COMPONENT = (Boolean.valueOf(traceKey)).booleanValue();
67 isTracing |= COMPONENT;
68 }
69
70 traceKey = Platform.getDebugOption(pluginID + "/request");
71 if (traceKey != null) {
72 REQUEST = (Boolean.valueOf(traceKey)).booleanValue();
73 isTracing |= REQUEST;
74 }
75
76 traceKey = Platform.getDebugOption(pluginID + "/signal");
77 if (traceKey != null) {
78 SIGNAL = (Boolean.valueOf(traceKey)).booleanValue();
79 isTracing |= SIGNAL;
80 }
81
82 traceKey = Platform.getDebugOption(pluginID + "/event");
83 if (traceKey != null) {
84 EVENT = (Boolean.valueOf(traceKey)).booleanValue();
85 isTracing |= EVENT;
86 }
87
88 // Create trace log file if needed
89 if (isTracing) {
90 fTraceLog = openLogFile(LOGNAME);
91 }
92 }
93
94 public static void stop() {
95 if (fTraceLog == null)
96 return;
97
98 try {
99 fTraceLog.close();
100 fTraceLog = null;
101 } catch (IOException e) {
102 e.printStackTrace();
103 }
104 }
105
106 // Predicates
107 public static boolean isErrorTraced() {
108 return ERROR;
109 }
110
111 public static boolean isComponentTraced() {
112 return COMPONENT;
113 }
114
115 public static boolean isRequestTraced() {
116 return REQUEST;
117 }
118
119 public static boolean isSignalTraced() {
120 return SIGNAL;
121 }
122
123 public static boolean isEventTraced() {
124 return EVENT;
125 }
126
127 // Tracers
128 public static void trace(String msg) {
129 long currentTime = System.currentTimeMillis();
130 StringBuilder message = new StringBuilder("[");
131 message.append(currentTime / 1000);
132 message.append(".");
133 message.append(String.format("%1$03d", currentTime % 1000));
134 message.append("] ");
135 message.append(msg);
136 // System.out.println(message);
137
138 if (fTraceLog != null) {
139 try {
140 fTraceLog.write(message.toString());
141 fTraceLog.newLine();
142 fTraceLog.flush();
143 } catch (IOException e) {
144 e.printStackTrace();
145 }
146 }
147 }
148
149 public static void traceComponent(ITmfComponent component, String msg) {
150 String message = ("[CMP] Thread=" + Thread.currentThread().getId() + " Cmp=" + component.getName() + " " + msg);
151 trace(message);
152 }
153
154 public static void traceRequest(ITmfDataRequest<?> request, String msg) {
155 String message = ("[REQ] Req=" + request.getRequestId() +
156 (request.getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? " (BG)" : " (FG)") +
157 " Thread=" + Thread.currentThread().getId() +
158 " Type=" + request.getClass().getName() +
159 " DataType=" + request.getDataType().getSimpleName() + " " + msg);
160 trace(message);
161 }
162
163 public static void traceSignal(TmfSignal signal, String msg) {
164 String message = ("[SIG] Type=" + signal.getClass().getSimpleName() + " Target=" + msg);
165 trace(message);
166 }
167
168 public static void traceEvent(ITmfDataProvider<?> provider, ITmfDataRequest<?> request, TmfData data) {
169 String message = ("[EVT] Provider=" + provider.toString() + ", Req=" + request.getRequestId() + ", Event=" + data.toString());
170 trace(message);
171 }
172
173 public static void traceError(String msg) {
174 String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);
175 trace(message);
176 }
177
178 public static void traceInfo(String msg) {
179 String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);
180 trace(message);
181 }
182
183 }
This page took 0.035765 seconds and 6 git commands to generate.