1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.core
;
15 import java
.io
.BufferedWriter
;
16 import java
.io
.FileWriter
;
17 import java
.io
.IOException
;
19 import org
.eclipse
.core
.runtime
.Platform
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.component
.ITmfEventProvider
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignal
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
27 * The TMF Core tracer, used to trace TMF internal components.
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.
33 * The tracing classes are:
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 * <li><strong>Analysis</strong>: TMF analyzes
43 * @author Francois Chouinard
45 @SuppressWarnings("nls")
46 public class TmfCoreTracer
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static final String PLUGIN_ID
= Activator
.PLUGIN_ID
;
54 // Tracing keys (in .options)
55 private static final String COMPONENT_TRACE_KEY
= PLUGIN_ID
+ "/component";
56 private static final String REQUEST_TRACE_KEY
= PLUGIN_ID
+ "/request";
57 private static final String SIGNAL_TRACE_KEY
= PLUGIN_ID
+ "/signal";
58 private static final String EVENT_TRACE_KEY
= PLUGIN_ID
+ "/event";
59 private static final String ANALYSIS_TRACE_KEY
= PLUGIN_ID
+ "/analysis";
61 private static final String TRACE_FILE_NAME
= "TmfTrace.log";
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 // Classes tracing flags
68 static boolean COMPONENT_CLASS_ENABLED
= false;
69 static boolean REQUEST_CLASS_ENABLED
= false;
70 static boolean SIGNAL_CLASS_ENABLED
= false;
71 static boolean EVENT_CLASS_ENABLED
= false;
72 static boolean ANALYSIS_CLASS_ENABLED
= false;
75 private static BufferedWriter fTraceFile
;
77 // ------------------------------------------------------------------------
78 // Start/stop tracing - controlled by the plug-in
79 // ------------------------------------------------------------------------
82 * Set the tracing flags according to the launch configuration
84 public static void init() {
87 boolean isTracing
= false;
89 traceKey
= Platform
.getDebugOption(COMPONENT_TRACE_KEY
);
90 if (traceKey
!= null) {
91 COMPONENT_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
92 isTracing
|= COMPONENT_CLASS_ENABLED
;
95 traceKey
= Platform
.getDebugOption(REQUEST_TRACE_KEY
);
96 if (traceKey
!= null) {
97 REQUEST_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
98 isTracing
|= REQUEST_CLASS_ENABLED
;
101 traceKey
= Platform
.getDebugOption(SIGNAL_TRACE_KEY
);
102 if (traceKey
!= null) {
103 SIGNAL_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
104 isTracing
|= SIGNAL_CLASS_ENABLED
;
107 traceKey
= Platform
.getDebugOption(EVENT_TRACE_KEY
);
108 if (traceKey
!= null) {
109 EVENT_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
110 isTracing
|= EVENT_CLASS_ENABLED
;
113 traceKey
= Platform
.getDebugOption(ANALYSIS_TRACE_KEY
);
114 if (traceKey
!= null) {
115 ANALYSIS_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
116 isTracing
|= ANALYSIS_CLASS_ENABLED
;
119 // Create trace log file if any of the flags was set
122 fTraceFile
= new BufferedWriter(new FileWriter(TRACE_FILE_NAME
));
123 } catch (IOException e
) {
124 Activator
.logError("Error opening log file " + TRACE_FILE_NAME
, e
);
131 * Close the trace log file
133 public static void stop() {
134 if (fTraceFile
!= null) {
138 } catch (IOException e
) {
139 Activator
.logError("Error closing log file", e
);
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
148 @SuppressWarnings("javadoc")
149 public static boolean isComponentTraced() {
150 return COMPONENT_CLASS_ENABLED
;
153 @SuppressWarnings("javadoc")
154 public static boolean isRequestTraced() {
155 return REQUEST_CLASS_ENABLED
;
158 @SuppressWarnings("javadoc")
159 public static boolean isSignalTraced() {
160 return SIGNAL_CLASS_ENABLED
;
163 @SuppressWarnings("javadoc")
164 public static boolean isEventTraced() {
165 return EVENT_CLASS_ENABLED
;
168 @SuppressWarnings("javadoc")
169 public static boolean isAnalysisTraced() {
170 return ANALYSIS_CLASS_ENABLED
;
173 // ------------------------------------------------------------------------
175 // ------------------------------------------------------------------------
178 * The central tracing method. Prepends the timestamp and the thread id to
182 * the trace message to log
184 public static synchronized void trace(String msg
) {
185 // Leave when there is no place to write the message.
186 if (fTraceFile
== null) {
190 // Set the timestamp (ms resolution)
191 long currentTime
= System
.currentTimeMillis();
192 StringBuilder message
= new StringBuilder("[");
193 message
.append(currentTime
/ 1000);
195 message
.append(String
.format("%1$03d", currentTime
% 1000));
196 message
.append("] ");
199 message
.append("[TID=");
200 message
.append(String
.format("%1$03d", Thread
.currentThread().getId()));
201 message
.append("] ");
203 // Append the trace message
208 fTraceFile
.write(message
.toString());
209 fTraceFile
.newLine();
211 } catch (IOException e
) {
212 Activator
.logError("Error writing to log file", e
);
216 // ------------------------------------------------------------------------
217 // TMF Core specific trace formatters
218 // ------------------------------------------------------------------------
221 * Trace an event happening in a component.
223 * @param componentName
224 * The name of the component being traced
226 * The message to record for this component
228 public static void traceComponent(String componentName
, String msg
) {
229 if (COMPONENT_CLASS_ENABLED
) {
230 String message
= ("[CMP] Cmp=" + componentName
+ " " + msg
);
236 * Trace an event happening in an event request.
239 * The request ID of the request being traced
241 * The message to record for this component
243 public static void traceRequest(int requestId
, String msg
) {
244 if (REQUEST_CLASS_ENABLED
) {
245 String message
= ("[REQ] Req=" + requestId
+ " " + msg
);
250 @SuppressWarnings("javadoc")
251 public static void traceSignal(TmfSignal signal
, String msg
) {
252 if (SIGNAL_CLASS_ENABLED
) {
253 String message
= ("[SIG] Sig=" + signal
.getClass().getSimpleName()
259 @SuppressWarnings("javadoc")
260 public static void traceEvent(ITmfEventProvider provider
, ITmfEventRequest request
, ITmfEvent event
) {
261 if (EVENT_CLASS_ENABLED
) {
262 String message
= ("[EVT] Provider=" + provider
.toString()
263 + ", Req=" + request
.getRequestId() + ", Event=" + event
.getTimestamp());
269 * Trace an event happening in an analysis
272 * The analysis ID of the analysis being run
274 * The trace this analysis is run on
276 * The message to record for this analysis
278 public static void traceAnalysis(String analysisId
, ITmfTrace trace
, String msg
) {
279 if (ANALYSIS_CLASS_ENABLED
) {
280 String traceName
= (trace
== null) ?
"" : trace
.getName();
281 String message
= ("[ANL] Anl=" + analysisId
+ " for " + traceName
+ " " + msg
);