tmf: Avoid HistoryTree to expose its internal HT_IO node.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / TmfCoreTracer.java
CommitLineData
6151d86c 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
6151d86c
PT
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.internal.tmf.core;
14
15import java.io.BufferedWriter;
16import java.io.FileWriter;
17import java.io.IOException;
18
19import org.eclipse.core.runtime.Platform;
20import org.eclipse.linuxtools.tmf.core.component.ITmfComponent;
fd3f1eff 21import org.eclipse.linuxtools.tmf.core.component.ITmfEventProvider;
6151d86c 22import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
fd3f1eff 23import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
6151d86c
PT
24import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
25
26/**
27 * The TMF Core tracer, used to trace TMF internal components.
28 * <p>
29 * The tracing classes are independently controlled (i.e no implicit inclusion)
30 * from the launch configuration's Tracing. The resulting trace is stored in a
31 * distinct file (TmfTrace.log) in a format that can later be analyzed by TMF.
32 * <p>
33 * The tracing classes are:
34 * <ul>
35 * <li><strong>Component</strong>: TMF components life-cycle
36 * <li><strong>Request</strong>: TMF requests life-cycle
37 * <li><strong>Signal</strong>: TMF signals triggering and distribution
38 * <li><strong>Event</strong>: TMF trace events
39 * </ul>
40 *
8584dc20 41 * @version 1.0
5419a136 42 * @author Francois Chouinard
6151d86c
PT
43 */
44@SuppressWarnings("nls")
45public class TmfCoreTracer {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50
51 private static final String PLUGIN_ID = Activator.PLUGIN_ID;
52
53 // Tracing keys (in .options)
54 private static final String COMPONENT_TRACE_KEY = PLUGIN_ID + "/component";
55 private static final String REQUEST_TRACE_KEY = PLUGIN_ID + "/request";
56 private static final String SIGNAL_TRACE_KEY = PLUGIN_ID + "/signal";
57 private static final String EVENT_TRACE_KEY = PLUGIN_ID + "/event";
58
59 private static final String TRACE_FILE_NAME = "TmfTrace.log";
60
61 // ------------------------------------------------------------------------
62 // Attributes
63 // ------------------------------------------------------------------------
64
65 // Classes tracing flags
66 static Boolean COMPONENT_CLASS_ENABLED = Boolean.FALSE;
67 static Boolean REQUEST_CLASS_ENABLED = Boolean.FALSE;
68 static Boolean SIGNAL_CLASS_ENABLED = Boolean.FALSE;
69 static Boolean EVENT_CLASS_ENABLED = Boolean.FALSE;
70
71 // Trace log file
5419a136 72 private static BufferedWriter fTraceFile;
6151d86c
PT
73
74 // ------------------------------------------------------------------------
75 // Start/stop tracing - controlled by the plug-in
76 // ------------------------------------------------------------------------
77
78 /**
79 * Set the tracing flags according to the launch configuration
80 */
81 public static void init() {
82
83 String traceKey;
84 boolean isTracing = false;
85
86 traceKey = Platform.getDebugOption(COMPONENT_TRACE_KEY);
87 if (traceKey != null) {
88 COMPONENT_CLASS_ENABLED = (Boolean.valueOf(traceKey)).booleanValue();
89 isTracing |= COMPONENT_CLASS_ENABLED;
90 }
91
92 traceKey = Platform.getDebugOption(REQUEST_TRACE_KEY);
93 if (traceKey != null) {
94 REQUEST_CLASS_ENABLED = (Boolean.valueOf(traceKey)).booleanValue();
95 isTracing |= REQUEST_CLASS_ENABLED;
96 }
97
98 traceKey = Platform.getDebugOption(SIGNAL_TRACE_KEY);
99 if (traceKey != null) {
100 SIGNAL_CLASS_ENABLED = (Boolean.valueOf(traceKey)).booleanValue();
101 isTracing |= SIGNAL_CLASS_ENABLED;
102 }
103
104 traceKey = Platform.getDebugOption(EVENT_TRACE_KEY);
105 if (traceKey != null) {
106 EVENT_CLASS_ENABLED = (Boolean.valueOf(traceKey)).booleanValue();
107 isTracing |= EVENT_CLASS_ENABLED;
108 }
109
110 // Create trace log file if any of the flags was set
111 if (isTracing) {
112 try {
113 fTraceFile = new BufferedWriter(new FileWriter(TRACE_FILE_NAME));
114 } catch (IOException e) {
115 Activator.logError("Error opening log file " + TRACE_FILE_NAME, e);
116 fTraceFile = null;
117 }
118 }
119 }
120
121 /**
122 * Close the trace log file
123 */
124 public static void stop() {
125 if (fTraceFile != null) {
126 try {
127 fTraceFile.close();
128 fTraceFile = null;
129 } catch (IOException e) {
130 Activator.logError("Error closing log file", e);
131 }
132 }
133 }
134
135 // ------------------------------------------------------------------------
136 // Predicates
137 // ------------------------------------------------------------------------
138
139 @SuppressWarnings("javadoc")
140 public static boolean isComponentTraced() {
141 return COMPONENT_CLASS_ENABLED;
142 }
143
144 @SuppressWarnings("javadoc")
145 public static boolean isRequestTraced() {
146 return REQUEST_CLASS_ENABLED;
147 }
148
149 @SuppressWarnings("javadoc")
150 public static boolean isSignalTraced() {
151 return SIGNAL_CLASS_ENABLED;
152 }
153
154 @SuppressWarnings("javadoc")
155 public static boolean isEventTraced() {
156 return EVENT_CLASS_ENABLED;
157 }
158
159 // ------------------------------------------------------------------------
160 // Tracing methods
161 // ------------------------------------------------------------------------
162
163 /**
164 * The central tracing method. Prepends the timestamp and the thread id
165 * to the trace message.
166 *
167 * @param msg the trace message to log
168 */
169 public static synchronized void trace(String msg) {
d773f035
EB
170 // Leave when there is no place to write the message.
171 if (fTraceFile == null) {
172 return;
173 }
6151d86c
PT
174
175 // Set the timestamp (ms resolution)
176 long currentTime = System.currentTimeMillis();
177 StringBuilder message = new StringBuilder("[");
178 message.append(currentTime / 1000);
179 message.append(".");
180 message.append(String.format("%1$03d", currentTime % 1000));
181 message.append("] ");
182
183 // Set the thread id
184 message.append("[TID=");
185 message.append(String.format("%1$03d", Thread.currentThread().getId()));
186 message.append("] ");
187
188 // Append the trace message
189 message.append(msg);
190
191 // Write to file
d773f035
EB
192 try {
193 fTraceFile.write(message.toString());
194 fTraceFile.newLine();
195 fTraceFile.flush();
196 } catch (IOException e) {
197 Activator.logError("Error writing to log file", e);
6151d86c
PT
198 }
199 }
200
201 // ------------------------------------------------------------------------
202 // TMF Core specific trace formatters
203 // ------------------------------------------------------------------------
204
205 @SuppressWarnings("javadoc")
206 public static void traceComponent(ITmfComponent component, String msg) {
207 if (COMPONENT_CLASS_ENABLED) {
208 String message = ("[CMP] Cmp=" + component.getName() + " " + msg);
209 trace(message);
210 }
211 }
212
213 @SuppressWarnings("javadoc")
fd3f1eff 214 public static void traceRequest(ITmfEventRequest request, String msg) {
6151d86c
PT
215 if (REQUEST_CLASS_ENABLED) {
216 String message = ("[REQ] Req=" + request.getRequestId() + " " + msg);
217 trace(message);
218 }
219 }
220
221 @SuppressWarnings("javadoc")
222 public static void traceSignal(TmfSignal signal, String msg) {
223 if (SIGNAL_CLASS_ENABLED) {
224 String message = ("[SIG] Sig=" + signal.getClass().getSimpleName()
225 + " Target=" + msg);
226 trace(message);
227 }
228 }
229
230 @SuppressWarnings("javadoc")
fd3f1eff 231 public static void traceEvent(ITmfEventProvider provider, ITmfEventRequest request, ITmfEvent event) {
6151d86c
PT
232 if (EVENT_CLASS_ENABLED) {
233 String message = ("[EVT] Provider=" + provider.toString()
234 + ", Req=" + request.getRequestId() + ", Event=" + event.getTimestamp());
235 trace(message);
236 }
237 }
238
239}
This page took 0.05181 seconds and 5 git commands to generate.