Commit | Line | Data |
---|---|---|
a0a88f65 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
a0a88f65 AM |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | *******************************************************************************/ | |
9 | ||
2bdf0193 | 10 | package org.eclipse.tracecompass.internal.tmf.ui; |
be222f56 PT |
11 | |
12 | import java.io.BufferedWriter; | |
13 | import java.io.FileWriter; | |
14 | import java.io.IOException; | |
15 | ||
16 | import org.eclipse.core.runtime.Platform; | |
17 | ||
a0a88f65 AM |
18 | /** |
19 | * Tracer class for the tmf.ui plugin | |
20 | */ | |
be222f56 PT |
21 | @SuppressWarnings("nls") |
22 | public class TmfUiTracer { | |
23 | ||
a0a88f65 AM |
24 | private static String pluginID = Activator.PLUGIN_ID; |
25 | ||
41c54394 MK |
26 | private static boolean fError = false; |
27 | private static boolean fWarning = false; | |
28 | private static boolean fInfo = false; | |
a0a88f65 | 29 | |
41c54394 MK |
30 | private static boolean fIndex = false; |
31 | private static boolean fDisplay = false; | |
32 | private static boolean fSorting = false; | |
a0a88f65 | 33 | |
41c54394 | 34 | private static final String LOGNAME = "traceUI.log"; |
a0a88f65 AM |
35 | private static BufferedWriter fTraceLog = null; |
36 | ||
37 | private static BufferedWriter openLogFile(String filename) { | |
38 | BufferedWriter outfile = null; | |
39 | try { | |
40 | outfile = new BufferedWriter(new FileWriter(filename)); | |
41 | } catch (IOException e) { | |
42 | Activator.getDefault().logError("Error creating log file " + LOGNAME, e); //$NON-NLS-1$ | |
43 | } | |
44 | return outfile; | |
45 | } | |
46 | ||
47 | /** | |
48 | * Initialize tracing | |
49 | */ | |
50 | public static void init() { | |
51 | ||
52 | String traceKey; | |
53 | boolean isTracing = false; | |
54 | ||
55 | traceKey = Platform.getDebugOption(pluginID + "/error"); | |
56 | if (traceKey != null) { | |
86609c0f | 57 | fError = Boolean.parseBoolean(traceKey); |
41c54394 | 58 | isTracing |= fError; |
a0a88f65 AM |
59 | } |
60 | ||
61 | traceKey = Platform.getDebugOption(pluginID + "/warning"); | |
62 | if (traceKey != null) { | |
86609c0f | 63 | fWarning = Boolean.parseBoolean(traceKey); |
41c54394 | 64 | isTracing |= fWarning; |
a0a88f65 AM |
65 | } |
66 | ||
67 | traceKey = Platform.getDebugOption(pluginID + "/info"); | |
68 | if (traceKey != null) { | |
86609c0f | 69 | fInfo = Boolean.parseBoolean(traceKey); |
41c54394 | 70 | isTracing |= fInfo; |
a0a88f65 AM |
71 | } |
72 | ||
73 | traceKey = Platform.getDebugOption(pluginID + "/updateindex"); | |
74 | if (traceKey != null) { | |
86609c0f | 75 | fIndex = Boolean.parseBoolean(traceKey); |
41c54394 | 76 | isTracing |= fIndex; |
a0a88f65 AM |
77 | } |
78 | ||
79 | traceKey = Platform.getDebugOption(pluginID + "/display"); | |
80 | if (traceKey != null) { | |
86609c0f | 81 | fDisplay = Boolean.parseBoolean(traceKey); |
41c54394 | 82 | isTracing |= fDisplay; |
a0a88f65 AM |
83 | } |
84 | ||
85 | traceKey = Platform.getDebugOption(pluginID + "/sorting"); | |
86 | if (traceKey != null) { | |
86609c0f | 87 | fSorting = Boolean.parseBoolean(traceKey); |
41c54394 | 88 | isTracing |= fSorting; |
a0a88f65 AM |
89 | } |
90 | ||
91 | // Create trace log file if needed | |
92 | if (isTracing) { | |
93 | fTraceLog = openLogFile(LOGNAME); | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Stop tracing | |
99 | */ | |
100 | public static void stop() { | |
101 | if (fTraceLog == null) { | |
102 | return; | |
103 | } | |
104 | ||
105 | try { | |
106 | fTraceLog.close(); | |
107 | fTraceLog = null; | |
108 | } catch (IOException e) { | |
109 | Activator.getDefault().logError("Error closing log file " + LOGNAME, e); //$NON-NLS-1$ | |
110 | } | |
111 | } | |
112 | ||
113 | // ------------------------------------------------------------------------ | |
114 | // Predicates | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | /** | |
118 | * @return If ERROR messages are traced | |
119 | */ | |
120 | public static boolean isErrorTraced() { | |
41c54394 | 121 | return fError; |
a0a88f65 AM |
122 | } |
123 | ||
124 | /** | |
125 | * @return If INDEX messages are traced | |
126 | */ | |
127 | public static boolean isIndexTraced() { | |
41c54394 | 128 | return fIndex; |
a0a88f65 AM |
129 | } |
130 | ||
131 | /** | |
132 | * @return If DISPLAY messages are traced | |
133 | */ | |
134 | public static boolean isDisplayTraced() { | |
41c54394 | 135 | return fDisplay; |
a0a88f65 AM |
136 | } |
137 | ||
138 | /** | |
139 | * @return If SORTING messages are traced | |
140 | */ | |
141 | public static boolean isSortingTraced() { | |
41c54394 | 142 | return fSorting; |
a0a88f65 AM |
143 | } |
144 | ||
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // Tracing methods | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
150 | /** | |
151 | * Trace a generic event | |
152 | * | |
153 | * @param msg | |
154 | * The event's message | |
155 | */ | |
156 | public static void trace(String msg) { | |
d773f035 EB |
157 | // Leave when there is no place to write the message. |
158 | if (fTraceLog == null) { | |
159 | return; | |
160 | } | |
161 | ||
a0a88f65 AM |
162 | long currentTime = System.currentTimeMillis(); |
163 | StringBuilder message = new StringBuilder("["); | |
164 | message.append(currentTime / 1000); | |
165 | message.append("."); | |
166 | message.append(String.format("%1$03d", currentTime % 1000)); | |
167 | message.append("] "); | |
168 | message.append(msg); | |
169 | ||
5b255dc0 MAL |
170 | System.out.println(message); |
171 | ||
d773f035 EB |
172 | try { |
173 | fTraceLog.write(message.toString()); | |
174 | fTraceLog.newLine(); | |
175 | fTraceLog.flush(); | |
176 | } catch (IOException e) { | |
177 | Activator.getDefault().logError("Error writing to log file " + LOGNAME, e); //$NON-NLS-1$ | |
a0a88f65 AM |
178 | } |
179 | } | |
180 | ||
181 | /** | |
182 | * Trace an INDEX event | |
183 | * | |
184 | * @param msg | |
185 | * The event's message | |
186 | */ | |
187 | public static void traceIndex(String msg) { | |
188 | String message = ("[INDEX] " + msg); | |
189 | trace(message); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Trace a DISPLAY event | |
194 | * | |
195 | * @param msg | |
196 | * The event's message | |
197 | */ | |
198 | public static void traceDisplay(String msg) { | |
199 | String message = ("[DISPLAY]" + msg); | |
200 | trace(message); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Trace a SORTING event | |
205 | * | |
206 | * @param msg | |
207 | * The event's message | |
208 | */ | |
209 | public static void traceSorting(String msg) { | |
210 | String message = ("[SORT] " + msg); | |
211 | trace(message); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Trace an ERROR event | |
216 | * | |
217 | * @param msg | |
218 | * The event's message | |
219 | */ | |
220 | public static void traceError(String msg) { | |
221 | String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg); | |
222 | trace(message); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Trace a WARNING event | |
227 | * | |
228 | * @param msg | |
229 | * The event's message | |
230 | */ | |
231 | public static void traceWarning(String msg) { | |
232 | String message = ("[WARN] Thread=" + Thread.currentThread().getId() + " " + msg); | |
233 | trace(message); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Trace an INFO event | |
238 | * | |
239 | * @param msg | |
240 | * The event's message | |
241 | */ | |
242 | public static void traceInfo(String msg) { | |
243 | String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg); | |
244 | trace(message); | |
245 | } | |
be222f56 PT |
246 | |
247 | } |