1 8 package com.mountainminds.eclemma.internal.core; 9 10 import java.io.PrintStream ; 11 import java.text.MessageFormat ; 12 13 import org.eclipse.core.runtime.Platform; 14 15 21 public final class DebugOptions { 22 23 26 public interface ITracer { 27 28 34 public boolean isEnabled(); 35 36 42 public void trace(String message); 43 44 53 public void trace(String message, Object param1); 54 55 66 public void trace(String message, Object param1, Object param2); 67 68 81 public void trace(String message, Object param1, Object param2, 82 Object param3); 83 84 87 public void startTimer(); 88 89 95 public void stopTimer(String message); 96 97 100 public void startMemoryUsage(); 101 102 108 public void stopMemoryUsage(String message); 109 110 } 111 112 private static final ITracer NUL_TRACER = new ITracer() { 113 114 public boolean isEnabled() { 115 return false; 116 } 117 118 public void trace(String message) { 119 } 120 121 public void trace(String message, Object param1) { 122 } 123 124 public void trace(String message, Object param1, Object param2) { 125 } 126 127 public void trace(String message, Object param1, Object param2, 128 Object param3) { 129 } 130 131 public void startTimer() { 132 } 133 134 public void stopTimer(String message) { 135 } 136 137 public void startMemoryUsage() { 138 } 139 140 public void stopMemoryUsage(String message) { 141 } 142 }; 143 144 private static class PrintStreamTracer implements ITracer { 145 146 private final PrintStream out; 147 148 private final String channel; 149 150 private final ThreadLocal starttime = new ThreadLocal (); 151 152 private final ThreadLocal heapsize = new ThreadLocal (); 153 154 PrintStreamTracer(String channel) { 155 this(channel, System.out); 156 } 157 158 PrintStreamTracer(String channel, PrintStream out) { 159 this.channel = channel; 160 this.out = out; 161 } 162 163 public boolean isEnabled() { 164 return true; 165 } 166 167 public void trace(String message) { 168 StringBuffer sb = new StringBuffer (); 169 sb.append('[').append(channel).append("] ").append(message); out.println(sb); 171 } 172 173 private void trace(String message, Object [] params) { 174 trace(MessageFormat.format(message, params)); 175 } 176 177 public void trace(String message, Object param1) { 178 trace(message, new Object [] { param1 }); 179 } 180 181 public void trace(String message, Object param1, Object param2) { 182 trace(message, new Object [] { param1, param2 }); 183 } 184 185 public void trace(String message, Object param1, Object param2, 186 Object param3) { 187 trace(message, new Object [] { param1, param2, param3 }); 188 } 189 190 public void startTimer() { 191 starttime.set(new Long (System.currentTimeMillis())); 192 } 193 194 public void stopTimer(String message) { 195 Long start = (Long ) starttime.get(); 196 if (start == null) { 197 trace("Timer {0} not startet.", message); } else { 199 long time = System.currentTimeMillis() - start.longValue(); 200 trace("{0} ms for {1}", new Object [] { new Long (time), message }); } 202 } 203 204 public void startMemoryUsage() { 205 Runtime rt = Runtime.getRuntime(); 206 heapsize.set(new Long (rt.totalMemory() - rt.freeMemory())); 207 } 208 209 public void stopMemoryUsage(String message) { 210 Long start = (Long ) heapsize.get(); 211 if (start == null) { 212 trace("Memory usage for {0} not started.", message); } else { 214 Runtime rt = Runtime.getRuntime(); 215 long bytes = rt.totalMemory() - rt.freeMemory() - start.longValue(); 216 trace("{0} bytes for {1}", new Object [] { new Long (bytes), message }); } 218 } 219 } 220 221 private static final String KEYPREFIX_DEBUG = EclEmmaCorePlugin.ID 222 + "/debug/"; 224 private static final String KEY_EMMAVERBOSITYLEVEL = KEYPREFIX_DEBUG 225 + "emmaVerbosityLevel"; 227 private static String getOption(String key, String defaultvalue) { 228 String value = Platform.getDebugOption(key); 229 return value == null ? defaultvalue : value; 230 } 231 232 private static ITracer getTracer(String channel) { 233 String key = KEYPREFIX_DEBUG + channel; 234 if (Boolean.valueOf(Platform.getDebugOption(key)).booleanValue()) { 235 return new PrintStreamTracer(channel); 236 } else { 237 return NUL_TRACER; 238 } 239 } 240 241 public static final String EMMAVERBOSITYLEVEL = getOption( 242 KEY_EMMAVERBOSITYLEVEL, "silent"); 244 public static final ITracer PERFORMANCETRACER = getTracer("performance"); 246 public static final ITracer INSTRUMENTATIONTRACER = getTracer("instrumentation"); 248 public static final ITracer LAUNCHINGTRACER = getTracer("launching"); 250 public static final ITracer ANALYSISTRACER = getTracer("analysis"); 252 } 253 | Popular Tags |