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