1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.linuxtools
.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
.linuxtools
.tmf
.core
.component
.ITmfComponent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.component
.ITmfEventProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignal
;
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
42 * @author Francois Chouinard
44 @SuppressWarnings("nls")
45 public class TmfCoreTracer
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private static final String PLUGIN_ID
= Activator
.PLUGIN_ID
;
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";
59 private static final String TRACE_FILE_NAME
= "TmfTrace.log";
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 // Classes tracing flags
66 static boolean COMPONENT_CLASS_ENABLED
= false;
67 static boolean REQUEST_CLASS_ENABLED
= false;
68 static boolean SIGNAL_CLASS_ENABLED
= false;
69 static boolean EVENT_CLASS_ENABLED
= false;
72 private static BufferedWriter fTraceFile
;
74 // ------------------------------------------------------------------------
75 // Start/stop tracing - controlled by the plug-in
76 // ------------------------------------------------------------------------
79 * Set the tracing flags according to the launch configuration
81 public static void init() {
84 boolean isTracing
= false;
86 traceKey
= Platform
.getDebugOption(COMPONENT_TRACE_KEY
);
87 if (traceKey
!= null) {
88 COMPONENT_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
89 isTracing
|= COMPONENT_CLASS_ENABLED
;
92 traceKey
= Platform
.getDebugOption(REQUEST_TRACE_KEY
);
93 if (traceKey
!= null) {
94 REQUEST_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
95 isTracing
|= REQUEST_CLASS_ENABLED
;
98 traceKey
= Platform
.getDebugOption(SIGNAL_TRACE_KEY
);
99 if (traceKey
!= null) {
100 SIGNAL_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
101 isTracing
|= SIGNAL_CLASS_ENABLED
;
104 traceKey
= Platform
.getDebugOption(EVENT_TRACE_KEY
);
105 if (traceKey
!= null) {
106 EVENT_CLASS_ENABLED
= (Boolean
.valueOf(traceKey
)).booleanValue();
107 isTracing
|= EVENT_CLASS_ENABLED
;
110 // Create trace log file if any of the flags was set
113 fTraceFile
= new BufferedWriter(new FileWriter(TRACE_FILE_NAME
));
114 } catch (IOException e
) {
115 Activator
.logError("Error opening log file " + TRACE_FILE_NAME
, e
);
122 * Close the trace log file
124 public static void stop() {
125 if (fTraceFile
!= null) {
129 } catch (IOException e
) {
130 Activator
.logError("Error closing log file", e
);
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
139 @SuppressWarnings("javadoc")
140 public static boolean isComponentTraced() {
141 return COMPONENT_CLASS_ENABLED
;
144 @SuppressWarnings("javadoc")
145 public static boolean isRequestTraced() {
146 return REQUEST_CLASS_ENABLED
;
149 @SuppressWarnings("javadoc")
150 public static boolean isSignalTraced() {
151 return SIGNAL_CLASS_ENABLED
;
154 @SuppressWarnings("javadoc")
155 public static boolean isEventTraced() {
156 return EVENT_CLASS_ENABLED
;
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 * The central tracing method. Prepends the timestamp and the thread id
165 * to the trace message.
167 * @param msg the trace message to log
169 public static synchronized void trace(String msg
) {
170 // Leave when there is no place to write the message.
171 if (fTraceFile
== null) {
175 // Set the timestamp (ms resolution)
176 long currentTime
= System
.currentTimeMillis();
177 StringBuilder message
= new StringBuilder("[");
178 message
.append(currentTime
/ 1000);
180 message
.append(String
.format("%1$03d", currentTime
% 1000));
181 message
.append("] ");
184 message
.append("[TID=");
185 message
.append(String
.format("%1$03d", Thread
.currentThread().getId()));
186 message
.append("] ");
188 // Append the trace message
193 fTraceFile
.write(message
.toString());
194 fTraceFile
.newLine();
196 } catch (IOException e
) {
197 Activator
.logError("Error writing to log file", e
);
201 // ------------------------------------------------------------------------
202 // TMF Core specific trace formatters
203 // ------------------------------------------------------------------------
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
);
213 @SuppressWarnings("javadoc")
214 public static void traceRequest(ITmfEventRequest request
, String msg
) {
215 if (REQUEST_CLASS_ENABLED
) {
216 String message
= ("[REQ] Req=" + request
.getRequestId() + " " + msg
);
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()
230 @SuppressWarnings("javadoc")
231 public static void traceEvent(ITmfEventProvider provider
, ITmfEventRequest request
, ITmfEvent event
) {
232 if (EVENT_CLASS_ENABLED
) {
233 String message
= ("[EVT] Provider=" + provider
.toString()
234 + ", Req=" + request
.getRequestId() + ", Event=" + event
.getTimestamp());