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