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