KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > profile > Profile


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.internal.profile;
13
14 import org.eclipse.osgi.framework.debug.FrameworkDebugOptions;
15 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
16
17 /**
18  * This class is a development tool that provides a simple way to log
19  * programmer defined timmings for performance evaluations. This profiling
20  * allows logging of a timestamp with a corresponding message to a trace
21  * buffer.
22  */

23
24 public class Profile {
25     /**
26      * Profiling is enabled and available.
27      */

28     public static final boolean PROFILE = true; // enable profile compiling
29
/**
30      * The logging state of <tt>STARTUP</tt> messages
31      */

32     public static boolean STARTUP = false; // enable startup profiling
33
/**
34      * The logging state of <tt>BENCHMARK</tt> messages
35      */

36     public static boolean BENCHMARK = false; // enable all benchmarking
37
/**
38      * The logging state of <tt>DEBUG</tt> messages
39      */

40     public static boolean DEBUG = false; // enable general debug profiling
41

42     private static final String JavaDoc OSGI_PROP = "osgi.profile."; //$NON-NLS-1$
43
private static final String JavaDoc PROP_STARTUP = OSGI_PROP + "startup"; //$NON-NLS-1$
44
private static final String JavaDoc PROP_BENCHMARK = OSGI_PROP + "benchmark"; //$NON-NLS-1$
45
private static final String JavaDoc PROP_DEBUG = OSGI_PROP + "debug"; //$NON-NLS-1$
46
private static final String JavaDoc PROP_IMPL = OSGI_PROP + "impl"; //$NON-NLS-1$
47

48     private static final String JavaDoc OSGI_OPTION = "org.eclipse.osgi/profile/"; //$NON-NLS-1$
49
private static final String JavaDoc OPTION_STARTUP = OSGI_OPTION + "startup"; //$NON-NLS-1$
50
private static final String JavaDoc OPTION_BENCHMARK = OSGI_OPTION + "benchmark"; //$NON-NLS-1$
51
private static final String JavaDoc OPTION_DEBUG = OSGI_OPTION + "debug"; //$NON-NLS-1$
52
private static final String JavaDoc OPTION_IMPL = OSGI_OPTION + "impl"; //$NON-NLS-1$
53

54     /**
55      * The default logging flag.
56      */

57     public static final int FLAG_NONE = 0;
58     /**
59      * The logging flag for <strong>method enter</strong>
60      */

61     public static final int FLAG_ENTER = 1;
62     /**
63      * The logging flag for <strong>method exit</strong>
64      */

65     public static final int FLAG_EXIT = 2;
66     /**
67      * The description for <strong>method enter</strong>
68      */

69     public static final String JavaDoc ENTER_DESCRIPTION = "enter"; //$NON-NLS-1$
70
/**
71      * The description for <strong>method exit</strong>
72      */

73     public static final String JavaDoc EXIT_DESCRIPTION = "exit"; //$NON-NLS-1$
74

75     private static ProfileLogger profileLogger = null;
76     private static String JavaDoc profileLoggerClassName = null;
77
78     static {
79         initProps();
80     }
81
82     /**
83      * Initialize/update profiling properties.
84      *
85      * If profiling properties are updated, this method is called to update
86      * the profile states.
87      */

88     public static void initProps() {
89         String JavaDoc prop;
90         FrameworkDebugOptions dbgOptions = null;
91
92         // if osgi.debug is not available, don't force DebugOptions
93
// to init as this variable may be set later on where
94
// DebugOptions will succeed.
95
if (FrameworkProperties.getProperty("osgi.debug") != null) { //$NON-NLS-1$
96
dbgOptions = FrameworkDebugOptions.getDefault();
97             if (dbgOptions != null) {
98                 STARTUP = dbgOptions.getBooleanOption(OPTION_STARTUP, false);
99                 BENCHMARK = dbgOptions.getBooleanOption(OPTION_BENCHMARK, false);
100                 DEBUG = dbgOptions.getBooleanOption(OPTION_DEBUG, false);
101                 if (profileLogger == null)
102                     profileLoggerClassName = dbgOptions.getOption(OPTION_IMPL);
103             }
104         }
105
106         // System properties will always override anything in .options file
107
if ((prop = FrameworkProperties.getProperty(PROP_STARTUP)) != null) {
108             STARTUP = Boolean.valueOf(prop).booleanValue();
109             if (dbgOptions != null)
110                 dbgOptions.setOption(OPTION_STARTUP, new Boolean JavaDoc(STARTUP).toString());
111         }
112         if ((prop = FrameworkProperties.getProperty(PROP_BENCHMARK)) != null) {
113             BENCHMARK = Boolean.valueOf(prop).booleanValue();
114             if (dbgOptions != null)
115                 dbgOptions.setOption(OPTION_BENCHMARK, new Boolean JavaDoc(BENCHMARK).toString());
116         }
117         if ((prop = FrameworkProperties.getProperty(PROP_DEBUG)) != null) {
118             DEBUG = Boolean.valueOf(prop).booleanValue();
119             if (dbgOptions != null)
120                 dbgOptions.setOption(OPTION_DEBUG, new Boolean JavaDoc(DEBUG).toString());
121         }
122
123         if (profileLogger == null) {
124             if ((prop = FrameworkProperties.getProperty(PROP_IMPL)) != null) {
125                 profileLoggerClassName = prop;
126                 if (dbgOptions != null)
127                     dbgOptions.setOption(OPTION_IMPL, profileLoggerClassName);
128             }
129         } else {
130             profileLogger.initProps();
131         }
132     }
133
134     /**
135      * Log a method enter.
136      *
137      * @param id The method's unique identification (e.g. org.eclipse.class#name).
138      */

139     public static void logEnter(String JavaDoc id) {
140         logTime(FLAG_ENTER, id, ENTER_DESCRIPTION, null);
141     }
142
143     /**
144      * Log a method enter.
145      *
146      * @param id The method's unique identification (e.g. org.eclipse.class#name).
147      * @param description A description of the method.
148      */

149     public static void logEnter(String JavaDoc id, String JavaDoc description) {
150         logTime(FLAG_ENTER, id, ENTER_DESCRIPTION, description);
151     }
152
153     /**
154      * Log a method exit.
155      *
156      * @param id The method's unique identification (e.g. org.eclipse.class#name).
157      */

158     public static void logExit(String JavaDoc id) {
159         logTime(FLAG_EXIT, id, EXIT_DESCRIPTION, null);
160     }
161
162     /**
163      * Log a method exit.
164      *
165      * @param id The method's unique identification (e.g. org.eclipse.class#name).
166      * @param description A description of the method.
167      */

168     public static void logExit(String JavaDoc id, String JavaDoc description) {
169         logTime(FLAG_EXIT, id, EXIT_DESCRIPTION, description);
170     }
171
172     /**
173      * Log a message.
174      *
175      * @param id The method's unique identification (e.g. org.eclipse.class#name).
176      * @param msg The message.
177      */

178     public static void logTime(String JavaDoc id, String JavaDoc msg) {
179         logTime(FLAG_NONE, id, msg, null);
180     }
181
182     /**
183      * Log a message.
184      *
185      * @param id The method's unique identification (e.g. org.eclipse.class#name).
186      * @param msg The message.
187      * @param description A description of the method.
188      */

189     public static void logTime(String JavaDoc id, String JavaDoc msg, String JavaDoc description) {
190         logTime(FLAG_NONE, id, msg, description);
191     }
192
193     /**
194      * Log a message.
195      *
196      * @param flag A profile logging flag.
197      * @param id The method's unique identification (e.g. org.eclipse.class#name).
198      * @param msg The message.
199      * @param description A description of the method.
200      *
201      * @see #FLAG_ENTER
202      * @see #FLAG_EXIT
203      * @see #FLAG_NONE
204      */

205     public static void logTime(int flag, String JavaDoc id, String JavaDoc msg, String JavaDoc description) {
206         if (profileLogger == null)
207             profileLogger = createProfileLogger();
208         profileLogger.logTime(flag, id, msg, description);
209     }
210
211     /**
212      * Use cumulative logging to record the entrance from this scope.
213      *
214      * @param scope The entering scope
215      */

216     public static void accumLogEnter(String JavaDoc scope) {
217         if (profileLogger == null)
218             profileLogger = createProfileLogger();
219         profileLogger.accumLogEnter(scope);
220     }
221
222     /**
223      * Use cumulative logging to record the exit from this scope.
224      *
225      * @param scope The exiting scope
226      */

227     public static void accumLogExit(String JavaDoc scope) {
228         if (profileLogger == null)
229             profileLogger = createProfileLogger();
230         profileLogger.accumLogExit(scope);
231     }
232
233     /**
234      * Get the profiling log report and reset the trace buffer.
235      *
236      * @return The profiling log report.
237      */

238     public static String JavaDoc getProfileLog() {
239         if (profileLogger != null)
240             return profileLogger.getProfileLog();
241         return ""; //$NON-NLS-1$
242
}
243
244     /**
245      * Create an instance of the appropriate profile logger
246      */

247     private static ProfileLogger createProfileLogger() {
248         ProfileLogger result = null;
249
250         // Try to create it by class name
251
if (profileLoggerClassName != null) {
252             Class JavaDoc profileImplClass = null;
253             try {
254                 profileImplClass = Class.forName(profileLoggerClassName);
255                 result = (ProfileLogger) profileImplClass.newInstance();
256             } catch (Exception JavaDoc e) {
257                 // could not find the class
258
e.printStackTrace();
259             }
260         }
261
262         // Use the default
263
if (result == null)
264             result = new DefaultProfileLogger();
265
266         return (result);
267     }
268 }
269
Popular Tags