Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / Tracer.java
index 6739ed708f0a5ecbf1b78a9f76096e67ef432398..bf86f2f28fab8160d31df491df4648030771e88a 100644 (file)
 package org.eclipse.linuxtools.tmf;\r
 \r
+import java.io.BufferedWriter;\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
+\r
 import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.linuxtools.tmf.component.ITmfComponent;\r
+import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;\r
+import org.eclipse.linuxtools.tmf.event.TmfData;\r
+import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;\r
+import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;\r
+import org.eclipse.linuxtools.tmf.signal.TmfSignal;\r
 \r
+@SuppressWarnings("nls")\r
 public class Tracer {\r
 \r
-       static Boolean TRACE = Boolean.FALSE;\r
+    private static String pluginID = TmfCorePlugin.PLUGIN_ID;\r
+\r
+    static Boolean ERROR = Boolean.FALSE;\r
+    static Boolean WARNING = Boolean.FALSE;\r
+    static Boolean INFO = Boolean.FALSE;\r
+\r
+    static Boolean COMPONENT = Boolean.FALSE;\r
+    static Boolean REQUEST = Boolean.FALSE;\r
+    static Boolean SIGNAL = Boolean.FALSE;\r
+    static Boolean EVENT = Boolean.FALSE;\r
+\r
+    private static String LOGNAME = "trace.log";\r
+    private static BufferedWriter fTraceLog = null;\r
+\r
+    private static BufferedWriter openLogFile(String filename) {\r
+        BufferedWriter outfile = null;\r
+        try {\r
+            outfile = new BufferedWriter(new FileWriter(filename));\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return outfile;\r
+    }\r
+\r
+    public static void init() {\r
+\r
+        String traceKey;\r
+        boolean isTracing = false;\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/error");\r
+        if (traceKey != null) {\r
+            ERROR = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= ERROR;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/warning");\r
+        if (traceKey != null) {\r
+            WARNING = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= WARNING;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/info");\r
+        if (traceKey != null) {\r
+            INFO = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= INFO;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/component");\r
+        if (traceKey != null) {\r
+            COMPONENT = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= COMPONENT;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/request");\r
+        if (traceKey != null) {\r
+            REQUEST = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= REQUEST;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/signal");\r
+        if (traceKey != null) {\r
+            SIGNAL = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= SIGNAL;\r
+        }\r
+\r
+        traceKey = Platform.getDebugOption(pluginID + "/event");\r
+        if (traceKey != null) {\r
+            EVENT = (Boolean.valueOf(traceKey)).booleanValue();\r
+            isTracing |= EVENT;\r
+        }\r
+\r
+        // Create trace log file if needed\r
+        if (isTracing) {\r
+            fTraceLog = openLogFile(LOGNAME);\r
+        }\r
+    }\r
+\r
+    public static void stop() {\r
+        if (fTraceLog == null)\r
+            return;\r
+\r
+        try {\r
+            fTraceLog.close();\r
+            fTraceLog = null;\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+        }\r
+    }\r
+\r
+    // Predicates\r
+    public static boolean isErrorTraced() {\r
+        return ERROR;\r
+    }\r
+\r
+    public static boolean isComponentTraced() {\r
+        return COMPONENT;\r
+    }\r
+\r
+    public static boolean isRequestTraced() {\r
+        return REQUEST;\r
+    }\r
+\r
+    public static boolean isSignalTraced() {\r
+        return SIGNAL;\r
+    }\r
+\r
+    public static boolean isEventTraced() {\r
+        return EVENT;\r
+    }\r
+\r
+    // Tracers\r
+    public static void trace(String msg) {\r
+        long currentTime = System.currentTimeMillis();\r
+        StringBuilder message = new StringBuilder("[");\r
+        message.append(currentTime / 1000);\r
+        message.append(".");\r
+        message.append(String.format("%1$03d", currentTime % 1000));\r
+        message.append("] ");\r
+        message.append(msg);\r
+//             System.out.println(message);\r
+\r
+        if (fTraceLog != null) {\r
+            try {\r
+                fTraceLog.write(message.toString());\r
+                fTraceLog.newLine();\r
+                fTraceLog.flush();\r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+\r
+    public static void traceComponent(ITmfComponent component, String msg) {\r
+        String message = ("[CMP] Thread=" + Thread.currentThread().getId() + " Cmp=" + component.getName() + " " + msg);\r
+        trace(message);\r
+    }\r
+\r
+    public static void traceRequest(ITmfDataRequest<?> request, String msg) {\r
+        String message = ("[REQ] Req=" + request.getRequestId()\r
+                + (request.getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? " (BG)" : " (FG)") + " Thread="\r
+                + Thread.currentThread().getId() + " Type=" + simpleType(request.getClass().getName()) + " Index="\r
+                + request.getIndex() + " NbReq=" + request.getNbRequested()\r
+                + (request instanceof ITmfEventRequest ? " Range=" + ((ITmfEventRequest<?>) request).getRange() : "")\r
+                + " DataType=" + request.getDataType().getSimpleName() + " " + msg);\r
+        trace(message);\r
+    }\r
+\r
+    private static String simpleType(String type) {\r
+        return type.substring(type.lastIndexOf('.') + 1);\r
+    }\r
+\r
+    public static void traceSignal(TmfSignal signal, String msg) {\r
+        String message = ("[SIG] Type=" + signal.getClass().getSimpleName() + " Target=" + msg);\r
+        trace(message);\r
+    }\r
 \r
-       private static String pluginID = TmfCorePlugin.PLUGIN_ID;\r
+    public static void traceEvent(ITmfDataProvider<?> provider, ITmfDataRequest<?> request, TmfData data) {\r
+        String message = ("[EVT] Provider=" + provider.toString() + ", Req=" + request.getRequestId() + ", Event=" + data\r
+                .toString());\r
+        trace(message);\r
+    }\r
 \r
-       public static void init() {\r
-               String traceKey = Platform.getDebugOption(pluginID + "/trace");\r
+    public static void traceError(String msg) {\r
+        String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);\r
+        trace(message);\r
+    }\r
 \r
-               if (traceKey != null) {\r
-                       TRACE = (new Boolean(traceKey)).booleanValue();\r
-               }\r
-       }\r
+    public static void traceInfo(String msg) {\r
+        String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);\r
+        trace(message);\r
+    }\r
 \r
-       public static void trace(String message) {\r
-               if (TRACE) {\r
-                       System.out.println(message);\r
-               }\r
-       }\r
 }\r
This page took 0.027253 seconds and 5 git commands to generate.