Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / Tracer.java
CommitLineData
8c8bf09f
ASL
1package org.eclipse.linuxtools.tmf;\r
2\r
550d787e
FC
3import java.io.BufferedWriter;\r
4import java.io.FileWriter;\r
5import java.io.IOException;\r
6\r
8c8bf09f 7import org.eclipse.core.runtime.Platform;\r
550d787e
FC
8import org.eclipse.linuxtools.tmf.component.ITmfComponent;\r
9import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;\r
10import org.eclipse.linuxtools.tmf.event.TmfData;\r
11import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;\r
a79913eb 12import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;\r
dc299841 13import org.eclipse.linuxtools.tmf.signal.TmfSignal;\r
8c8bf09f 14\r
3b38ea61 15@SuppressWarnings("nls")\r
8c8bf09f
ASL
16public class Tracer {\r
17\r
12c155f5
FC
18 private static String pluginID = TmfCorePlugin.PLUGIN_ID;\r
19\r
20 static Boolean ERROR = Boolean.FALSE;\r
21 static Boolean WARNING = Boolean.FALSE;\r
22 static Boolean INFO = Boolean.FALSE;\r
23\r
24 static Boolean COMPONENT = Boolean.FALSE;\r
25 static Boolean REQUEST = Boolean.FALSE;\r
26 static Boolean SIGNAL = Boolean.FALSE;\r
27 static Boolean EVENT = Boolean.FALSE;\r
28\r
29 private static String LOGNAME = "trace.log";\r
30 private static BufferedWriter fTraceLog = null;\r
31\r
32 private static BufferedWriter openLogFile(String filename) {\r
33 BufferedWriter outfile = null;\r
34 try {\r
35 outfile = new BufferedWriter(new FileWriter(filename));\r
36 } catch (IOException e) {\r
37 e.printStackTrace();\r
38 }\r
39 return outfile;\r
40 }\r
41\r
42 public static void init() {\r
43\r
44 String traceKey;\r
45 boolean isTracing = false;\r
46\r
47 traceKey = Platform.getDebugOption(pluginID + "/error");\r
48 if (traceKey != null) {\r
49 ERROR = (Boolean.valueOf(traceKey)).booleanValue();\r
50 isTracing |= ERROR;\r
51 }\r
52\r
53 traceKey = Platform.getDebugOption(pluginID + "/warning");\r
54 if (traceKey != null) {\r
55 WARNING = (Boolean.valueOf(traceKey)).booleanValue();\r
56 isTracing |= WARNING;\r
57 }\r
58\r
59 traceKey = Platform.getDebugOption(pluginID + "/info");\r
60 if (traceKey != null) {\r
61 INFO = (Boolean.valueOf(traceKey)).booleanValue();\r
62 isTracing |= INFO;\r
63 }\r
64\r
65 traceKey = Platform.getDebugOption(pluginID + "/component");\r
66 if (traceKey != null) {\r
67 COMPONENT = (Boolean.valueOf(traceKey)).booleanValue();\r
68 isTracing |= COMPONENT;\r
69 }\r
70\r
71 traceKey = Platform.getDebugOption(pluginID + "/request");\r
72 if (traceKey != null) {\r
73 REQUEST = (Boolean.valueOf(traceKey)).booleanValue();\r
74 isTracing |= REQUEST;\r
75 }\r
76\r
77 traceKey = Platform.getDebugOption(pluginID + "/signal");\r
78 if (traceKey != null) {\r
79 SIGNAL = (Boolean.valueOf(traceKey)).booleanValue();\r
80 isTracing |= SIGNAL;\r
81 }\r
82\r
83 traceKey = Platform.getDebugOption(pluginID + "/event");\r
84 if (traceKey != null) {\r
85 EVENT = (Boolean.valueOf(traceKey)).booleanValue();\r
86 isTracing |= EVENT;\r
87 }\r
88\r
89 // Create trace log file if needed\r
90 if (isTracing) {\r
91 fTraceLog = openLogFile(LOGNAME);\r
92 }\r
93 }\r
94\r
95 public static void stop() {\r
96 if (fTraceLog == null)\r
97 return;\r
98\r
99 try {\r
100 fTraceLog.close();\r
101 fTraceLog = null;\r
102 } catch (IOException e) {\r
103 e.printStackTrace();\r
104 }\r
105 }\r
106\r
107 // Predicates\r
108 public static boolean isErrorTraced() {\r
109 return ERROR;\r
110 }\r
111\r
112 public static boolean isComponentTraced() {\r
113 return COMPONENT;\r
114 }\r
115\r
116 public static boolean isRequestTraced() {\r
117 return REQUEST;\r
118 }\r
119\r
120 public static boolean isSignalTraced() {\r
121 return SIGNAL;\r
122 }\r
123\r
124 public static boolean isEventTraced() {\r
125 return EVENT;\r
126 }\r
127\r
128 // Tracers\r
129 public static void trace(String msg) {\r
130 long currentTime = System.currentTimeMillis();\r
131 StringBuilder message = new StringBuilder("[");\r
132 message.append(currentTime / 1000);\r
133 message.append(".");\r
134 message.append(String.format("%1$03d", currentTime % 1000));\r
135 message.append("] ");\r
136 message.append(msg);\r
9b635e61
FC
137// System.out.println(message);\r
138\r
12c155f5
FC
139 if (fTraceLog != null) {\r
140 try {\r
141 fTraceLog.write(message.toString());\r
142 fTraceLog.newLine();\r
143 fTraceLog.flush();\r
144 } catch (IOException e) {\r
145 e.printStackTrace();\r
146 }\r
147 }\r
148 }\r
149\r
150 public static void traceComponent(ITmfComponent component, String msg) {\r
151 String message = ("[CMP] Thread=" + Thread.currentThread().getId() + " Cmp=" + component.getName() + " " + msg);\r
152 trace(message);\r
153 }\r
154\r
155 public static void traceRequest(ITmfDataRequest<?> request, String msg) {\r
156 String message = ("[REQ] Req=" + request.getRequestId()\r
157 + (request.getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? " (BG)" : " (FG)") + " Thread="\r
158 + Thread.currentThread().getId() + " Type=" + simpleType(request.getClass().getName()) + " Index="\r
159 + request.getIndex() + " NbReq=" + request.getNbRequested()\r
160 + (request instanceof ITmfEventRequest ? " Range=" + ((ITmfEventRequest<?>) request).getRange() : "")\r
161 + " DataType=" + request.getDataType().getSimpleName() + " " + msg);\r
162 trace(message);\r
163 }\r
164\r
165 private static String simpleType(String type) {\r
166 return type.substring(type.lastIndexOf('.') + 1);\r
167 }\r
168\r
169 public static void traceSignal(TmfSignal signal, String msg) {\r
170 String message = ("[SIG] Type=" + signal.getClass().getSimpleName() + " Target=" + msg);\r
171 trace(message);\r
172 }\r
173\r
174 public static void traceEvent(ITmfDataProvider<?> provider, ITmfDataRequest<?> request, TmfData data) {\r
175 String message = ("[EVT] Provider=" + provider.toString() + ", Req=" + request.getRequestId() + ", Event=" + data\r
176 .toString());\r
177 trace(message);\r
178 }\r
179\r
180 public static void traceError(String msg) {\r
181 String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);\r
182 trace(message);\r
183 }\r
184\r
185 public static void traceInfo(String msg) {\r
186 String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);\r
187 trace(message);\r
188 }\r
9b635e61 189\r
8c8bf09f 190}\r
This page took 0.063996 seconds and 5 git commands to generate.