Convert Windows line delimiters to Unix.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / Tracer.java
1 package org.eclipse.linuxtools.internal.tmf.core;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7
8 import org.eclipse.core.runtime.Platform;
9 import org.eclipse.linuxtools.tmf.core.component.ITmfComponent;
10 import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;
11 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
12 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
13 import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
14
15 @SuppressWarnings({"nls", "javadoc"})
16 public class Tracer {
17
18 private static String pluginID = Activator.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 = new BufferedWriter(new OutputStreamWriter(System.out));
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 Activator.getDefault().logError("Error opening log file " + filename, e);
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
100 try {
101 fTraceLog.close();
102 fTraceLog = null;
103 } catch (IOException e) {
104 Activator.getDefault().logError("Error closing log file", e);
105 }
106 }
107
108 // Predicates
109 public static boolean isErrorTraced() {
110 return ERROR;
111 }
112
113 public static boolean isComponentTraced() {
114 return COMPONENT;
115 }
116
117 public static boolean isRequestTraced() {
118 return REQUEST;
119 }
120
121 public static boolean isSignalTraced() {
122 return SIGNAL;
123 }
124
125 public static boolean isEventTraced() {
126 return EVENT;
127 }
128
129 // Tracers
130 public static synchronized void trace(String msg) {
131 long currentTime = System.currentTimeMillis();
132 StringBuilder message = new StringBuilder("[");
133 message.append(currentTime / 1000);
134 message.append(".");
135 message.append(String.format("%1$03d", currentTime % 1000));
136 message.append("] ");
137
138 message.append("[Th=");
139 message.append(String.format("%1$03d", Thread.currentThread().getId()));
140 message.append("] ");
141
142 message.append(msg);
143
144 if (fTraceLog != null) {
145 try {
146 fTraceLog.write(message.toString());
147 fTraceLog.newLine();
148 fTraceLog.flush();
149 } catch (IOException e) {
150 Activator.getDefault().logError("Error writing to log file", e);
151 }
152 }
153 }
154
155 public static void traceComponent(ITmfComponent component, String msg) {
156 String message = ("[CMP] Cmp=" + component.getName() + " " + msg);
157 trace(message);
158 }
159
160 public static void traceRequest(ITmfDataRequest<?> request, String msg) {
161 String message = ("[REQ] Req=" + request.getRequestId() + " " + msg);
162 trace(message);
163 }
164
165 public static void traceSignal(TmfSignal signal, String msg) {
166 String message = ("[SIG] Sig=" + signal.getClass().getSimpleName() + " Target=" + msg);
167 trace(message);
168 }
169
170 public static void traceEvent(ITmfDataProvider<?> provider, ITmfDataRequest<?> request, ITmfEvent event) {
171 String message = ("[EVT] Provider=" + provider.toString() + ", Req=" + request.getRequestId() + ", Event=" + event.getTimestamp());
172 trace(message);
173 }
174
175 public static void traceError(String msg) {
176 String message = ("[ERR] Err=" + msg);
177 trace(message);
178 }
179
180 public static void traceInfo(String msg) {
181 String message = ("[INF] " + msg);
182 trace(message);
183 }
184
185 }
This page took 0.033708 seconds and 5 git commands to generate.