tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / TmfUiTracer.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 package org.eclipse.linuxtools.internal.tmf.ui;
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
18 /**
19 * Tracer class for the tmf.ui plugin
20 */
21 @SuppressWarnings("nls")
22 public class TmfUiTracer {
23
24 private static String pluginID = Activator.PLUGIN_ID;
25
26 static Boolean ERROR = Boolean.FALSE;
27 static Boolean WARNING = Boolean.FALSE;
28 static Boolean INFO = Boolean.FALSE;
29
30 static Boolean INDEX = Boolean.FALSE;
31 static Boolean DISPLAY = Boolean.FALSE;
32 static Boolean SORTING = Boolean.FALSE;
33
34 private static String LOGNAME = "traceUI.log";
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) {
57 ERROR = (Boolean.valueOf(traceKey)).booleanValue();
58 isTracing |= ERROR;
59 }
60
61 traceKey = Platform.getDebugOption(pluginID + "/warning");
62 if (traceKey != null) {
63 WARNING = (Boolean.valueOf(traceKey)).booleanValue();
64 isTracing |= WARNING;
65 }
66
67 traceKey = Platform.getDebugOption(pluginID + "/info");
68 if (traceKey != null) {
69 INFO = (Boolean.valueOf(traceKey)).booleanValue();
70 isTracing |= INFO;
71 }
72
73 traceKey = Platform.getDebugOption(pluginID + "/updateindex");
74 if (traceKey != null) {
75 INDEX = (Boolean.valueOf(traceKey)).booleanValue();
76 isTracing |= INDEX;
77 }
78
79 traceKey = Platform.getDebugOption(pluginID + "/display");
80 if (traceKey != null) {
81 DISPLAY = (Boolean.valueOf(traceKey)).booleanValue();
82 isTracing |= DISPLAY;
83 }
84
85 traceKey = Platform.getDebugOption(pluginID + "/sorting");
86 if (traceKey != null) {
87 SORTING = (Boolean.valueOf(traceKey)).booleanValue();
88 isTracing |= SORTING;
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() {
121 return ERROR;
122 }
123
124 /**
125 * @return If INDEX messages are traced
126 */
127 public static boolean isIndexTraced() {
128 return INDEX;
129 }
130
131 /**
132 * @return If DISPLAY messages are traced
133 */
134 public static boolean isDisplayTraced() {
135 return DISPLAY;
136 }
137
138 /**
139 * @return If SORTING messages are traced
140 */
141 public static boolean isSortingTraced() {
142 return SORTING;
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) {
157 long currentTime = System.currentTimeMillis();
158 StringBuilder message = new StringBuilder("[");
159 message.append(currentTime / 1000);
160 message.append(".");
161 message.append(String.format("%1$03d", currentTime % 1000));
162 message.append("] ");
163 message.append(msg);
164
165 if (fTraceLog != null) {
166 try {
167 fTraceLog.write(message.toString());
168 fTraceLog.newLine();
169 fTraceLog.flush();
170 } catch (IOException e) {
171 Activator.getDefault().logError("Error writing to log file " + LOGNAME, e); //$NON-NLS-1$
172 }
173 }
174 }
175
176 /**
177 * Trace an INDEX event
178 *
179 * @param msg
180 * The event's message
181 */
182 public static void traceIndex(String msg) {
183 String message = ("[INDEX] " + msg);
184 trace(message);
185 }
186
187 /**
188 * Trace a DISPLAY event
189 *
190 * @param msg
191 * The event's message
192 */
193 public static void traceDisplay(String msg) {
194 String message = ("[DISPLAY]" + msg);
195 trace(message);
196 }
197
198 /**
199 * Trace a SORTING event
200 *
201 * @param msg
202 * The event's message
203 */
204 public static void traceSorting(String msg) {
205 String message = ("[SORT] " + msg);
206 trace(message);
207 }
208
209 /**
210 * Trace an ERROR event
211 *
212 * @param msg
213 * The event's message
214 */
215 public static void traceError(String msg) {
216 String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);
217 trace(message);
218 }
219
220 /**
221 * Trace a WARNING event
222 *
223 * @param msg
224 * The event's message
225 */
226 public static void traceWarning(String msg) {
227 String message = ("[WARN] Thread=" + Thread.currentThread().getId() + " " + msg);
228 trace(message);
229 }
230
231 /**
232 * Trace an INFO event
233 *
234 * @param msg
235 * The event's message
236 */
237 public static void traceInfo(String msg) {
238 String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);
239 trace(message);
240 }
241
242 }
This page took 0.045487 seconds and 5 git commands to generate.