KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > DebugOptions


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: DebugOptions.java 11 2006-08-28 20:06:31Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core;
9
10 import java.io.PrintStream JavaDoc;
11 import java.text.MessageFormat JavaDoc;
12
13 import org.eclipse.core.runtime.Platform;
14
15 /**
16  * Access to debug options and tracing facilities for this plugin.
17  *
18  * @author Marc R. Hoffmann
19  * @version $Revision: 11 $
20  */

21 public final class DebugOptions {
22
23   /**
24    * Interface for optional trace output.
25    */

26   public interface ITracer {
27
28     /**
29      * Determines whether this tracer is enabled. Clients may use this method to
30      * avoid expensive calculation for debug output.
31      *
32      * @return <code>true</code> if the tracer is enabled
33      */

34     public boolean isEnabled();
35
36     /**
37      * Prints the given debug message if the tracer is enabled.
38      *
39      * @param message
40      * text message for trace output
41      */

42     public void trace(String JavaDoc message);
43
44     /**
45      * Prints the given debug message if the tracer is enabled. The parameter
46      * object will be inserted for the <code>{x}</code> placeholder.
47      *
48      * @param message
49      * text message for trace output
50      * @param param1
51      * parameter object for inserting
52      */

53     public void trace(String JavaDoc message, Object JavaDoc param1);
54
55     /**
56      * Prints the given debug message if the tracer is enabled. The parameter
57      * object wills be inserted for the <code>{x}</code> placeholder.
58      *
59      * @param message
60      * text message for trace output
61      * @param param1
62      * first parameter object for inserting
63      * @param param2
64      * first parameter object for inserting
65      */

66     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2);
67
68     /**
69      * Prints the given debug message if the tracer is enabled. The parameter
70      * object wills be inserted for the <code>{x}</code> placeholder.
71      *
72      * @param message
73      * text message for trace output
74      * @param param1
75      * first parameter object for inserting
76      * @param param2
77      * first parameter object for inserting
78      * @param param3
79      * third parameter object for inserting
80      */

81     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2,
82         Object JavaDoc param3);
83
84     /**
85      * Starts a timer for the calling thread.
86      */

87     public void startTimer();
88
89     /**
90      * Prints out the elapsed time since starting the timer.
91      *
92      * @param message
93      * identification for the timed period
94      */

95     public void stopTimer(String JavaDoc message);
96
97     /**
98      * Start measuring heap memory usage.
99      */

100     public void startMemoryUsage();
101
102     /**
103      * Print out heap memory usage since starting measurement.
104      *
105      * @param message
106      * identification for this memory usage output
107      */

108     public void stopMemoryUsage(String JavaDoc 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 JavaDoc message) {
119     }
120
121     public void trace(String JavaDoc message, Object JavaDoc param1) {
122     }
123
124     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2) {
125     }
126
127     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2,
128         Object JavaDoc param3) {
129     }
130
131     public void startTimer() {
132     }
133
134     public void stopTimer(String JavaDoc message) {
135     }
136
137     public void startMemoryUsage() {
138     }
139
140     public void stopMemoryUsage(String JavaDoc message) {
141     }
142   };
143
144   private static class PrintStreamTracer implements ITracer {
145
146     private final PrintStream JavaDoc out;
147
148     private final String JavaDoc channel;
149
150     private final ThreadLocal JavaDoc starttime = new ThreadLocal JavaDoc();
151
152     private final ThreadLocal JavaDoc heapsize = new ThreadLocal JavaDoc();
153
154     PrintStreamTracer(String JavaDoc channel) {
155       this(channel, System.out);
156     }
157
158     PrintStreamTracer(String JavaDoc channel, PrintStream JavaDoc 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 JavaDoc message) {
168       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
169       sb.append('[').append(channel).append("] ").append(message); //$NON-NLS-1$
170
out.println(sb);
171     }
172
173     private void trace(String JavaDoc message, Object JavaDoc[] params) {
174       trace(MessageFormat.format(message, params));
175     }
176
177     public void trace(String JavaDoc message, Object JavaDoc param1) {
178       trace(message, new Object JavaDoc[] { param1 });
179     }
180
181     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2) {
182       trace(message, new Object JavaDoc[] { param1, param2 });
183     }
184
185     public void trace(String JavaDoc message, Object JavaDoc param1, Object JavaDoc param2,
186         Object JavaDoc param3) {
187       trace(message, new Object JavaDoc[] { param1, param2, param3 });
188     }
189
190     public void startTimer() {
191       starttime.set(new Long JavaDoc(System.currentTimeMillis()));
192     }
193
194     public void stopTimer(String JavaDoc message) {
195       Long JavaDoc start = (Long JavaDoc) starttime.get();
196       if (start == null) {
197         trace("Timer {0} not startet.", message); //$NON-NLS-1$
198
} else {
199         long time = System.currentTimeMillis() - start.longValue();
200         trace("{0} ms for {1}", new Object JavaDoc[] { new Long JavaDoc(time), message }); //$NON-NLS-1$
201
}
202     }
203
204     public void startMemoryUsage() {
205       Runtime JavaDoc rt = Runtime.getRuntime();
206       heapsize.set(new Long JavaDoc(rt.totalMemory() - rt.freeMemory()));
207     }
208
209     public void stopMemoryUsage(String JavaDoc message) {
210       Long JavaDoc start = (Long JavaDoc) heapsize.get();
211       if (start == null) {
212         trace("Memory usage for {0} not started.", message); //$NON-NLS-1$
213
} else {
214         Runtime JavaDoc rt = Runtime.getRuntime();
215         long bytes = rt.totalMemory() - rt.freeMemory() - start.longValue();
216         trace("{0} bytes for {1}", new Object JavaDoc[] { new Long JavaDoc(bytes), message }); //$NON-NLS-1$
217
}
218     }
219   }
220
221   private static final String JavaDoc KEYPREFIX_DEBUG = EclEmmaCorePlugin.ID
222       + "/debug/"; //$NON-NLS-1$
223

224   private static final String JavaDoc KEY_EMMAVERBOSITYLEVEL = KEYPREFIX_DEBUG
225       + "emmaVerbosityLevel"; //$NON-NLS-1$
226

227   private static String JavaDoc getOption(String JavaDoc key, String JavaDoc defaultvalue) {
228     String JavaDoc value = Platform.getDebugOption(key);
229     return value == null ? defaultvalue : value;
230   }
231
232   private static ITracer getTracer(String JavaDoc channel) {
233     String JavaDoc 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 JavaDoc EMMAVERBOSITYLEVEL = getOption(
242       KEY_EMMAVERBOSITYLEVEL, "silent"); //$NON-NLS-1$
243

244   public static final ITracer PERFORMANCETRACER = getTracer("performance"); //$NON-NLS-1$
245

246   public static final ITracer INSTRUMENTATIONTRACER = getTracer("instrumentation"); //$NON-NLS-1$
247

248   public static final ITracer LAUNCHINGTRACER = getTracer("launching"); //$NON-NLS-1$
249

250   public static final ITracer ANALYSISTRACER = getTracer("analysis"); //$NON-NLS-1$
251

252 }
253
Popular Tags