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) { | |
41c54394 MK |
57 | fError = (Boolean.valueOf(traceKey)).booleanValue(); |
58 | isTracing |= fError; | |
a0a88f65 AM |
59 | } |
60 | ||
61 | traceKey = Platform.getDebugOption(pluginID + "/warning"); | |
62 | if (traceKey != null) { | |
41c54394 MK |
63 | fWarning = (Boolean.valueOf(traceKey)).booleanValue(); |
64 | isTracing |= fWarning; | |
a0a88f65 AM |
65 | } |
66 | ||
67 | traceKey = Platform.getDebugOption(pluginID + "/info"); | |
68 | if (traceKey != null) { | |
41c54394 MK |
69 | fInfo = (Boolean.valueOf(traceKey)).booleanValue(); |
70 | isTracing |= fInfo; | |
a0a88f65 AM |
71 | } |
72 | ||
73 | traceKey = Platform.getDebugOption(pluginID + "/updateindex"); | |
74 | if (traceKey != null) { | |
41c54394 MK |
75 | fIndex = (Boolean.valueOf(traceKey)).booleanValue(); |
76 | isTracing |= fIndex; | |
a0a88f65 AM |
77 | } |
78 | ||
79 | traceKey = Platform.getDebugOption(pluginID + "/display"); | |
80 | if (traceKey != null) { | |
41c54394 MK |
81 | fDisplay = (Boolean.valueOf(traceKey)).booleanValue(); |
82 | isTracing |= fDisplay; | |
a0a88f65 AM |
83 | } |
84 | ||
85 | traceKey = Platform.getDebugOption(pluginID + "/sorting"); | |
86 | if (traceKey != null) { | |
41c54394 MK |
87 | fSorting = (Boolean.valueOf(traceKey)).booleanValue(); |
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 | ||
d773f035 EB |
170 | try { |
171 | fTraceLog.write(message.toString()); | |
172 | fTraceLog.newLine(); | |
173 | fTraceLog.flush(); | |
174 | } catch (IOException e) { | |
175 | Activator.getDefault().logError("Error writing to log file " + LOGNAME, e); //$NON-NLS-1$ | |
a0a88f65 AM |
176 | } |
177 | } | |
178 | ||
179 | /** | |
180 | * Trace an INDEX event | |
181 | * | |
182 | * @param msg | |
183 | * The event's message | |
184 | */ | |
185 | public static void traceIndex(String msg) { | |
186 | String message = ("[INDEX] " + msg); | |
187 | trace(message); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Trace a DISPLAY event | |
192 | * | |
193 | * @param msg | |
194 | * The event's message | |
195 | */ | |
196 | public static void traceDisplay(String msg) { | |
197 | String message = ("[DISPLAY]" + msg); | |
198 | trace(message); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Trace a SORTING event | |
203 | * | |
204 | * @param msg | |
205 | * The event's message | |
206 | */ | |
207 | public static void traceSorting(String msg) { | |
208 | String message = ("[SORT] " + msg); | |
209 | trace(message); | |
210 | } | |
211 | ||
212 | /** | |
213 | * Trace an ERROR event | |
214 | * | |
215 | * @param msg | |
216 | * The event's message | |
217 | */ | |
218 | public static void traceError(String msg) { | |
219 | String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg); | |
220 | trace(message); | |
221 | } | |
222 | ||
223 | /** | |
224 | * Trace a WARNING event | |
225 | * | |
226 | * @param msg | |
227 | * The event's message | |
228 | */ | |
229 | public static void traceWarning(String msg) { | |
230 | String message = ("[WARN] Thread=" + Thread.currentThread().getId() + " " + msg); | |
231 | trace(message); | |
232 | } | |
233 | ||
234 | /** | |
235 | * Trace an INFO event | |
236 | * | |
237 | * @param msg | |
238 | * The event's message | |
239 | */ | |
240 | public static void traceInfo(String msg) { | |
241 | String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg); | |
242 | trace(message); | |
243 | } | |
be222f56 PT |
244 | |
245 | } |