KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > StartLog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import java.util.Stack JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 /** Logger that will enable the logging of important events during the startup
26  * annotated with real time and possibly time differences.
27  *
28  * @author Petr Nejedly, Jesse Glick
29  */

30 public class StartLog {
31
32     private static final StartImpl impl;
33     private static final Stack JavaDoc<String JavaDoc> actions = new Stack JavaDoc<String JavaDoc>();
34     private static final Stack JavaDoc<Throwable JavaDoc> places = new Stack JavaDoc<Throwable JavaDoc>();
35     private static final boolean DEBUG_NESTING = Boolean.getBoolean("org.netbeans.log.startup.debug"); // NOI18N
36

37     private static final String JavaDoc logProp;
38     private static final String JavaDoc logFileProp;
39     
40     static {
41         logProp = System.getProperty( "org.netbeans.log.startup" ); // NOI18N
42
//if you want to create file with measured values, we do expect the property org.netbeans.log.startup=print
43
logFileProp = System.getProperty( "org.netbeans.log.startup.logfile" ); // NOI18N
44

45         if(logProp == null)
46             impl = new StartImpl();
47         else if("print".equals(logProp))
48             impl = new PrintImpl();
49         else if("tests".equals(logProp))
50             impl = new PerformanceTestsImpl();
51         else
52             throw new Error JavaDoc("Unknown org.netbeans.log.startup value [" + logProp + "], it should be (print or tests) !"); // NOI18N
53
}
54     
55     /** Start running some interval action.
56      * @param action some identifying string
57      */

58     public static void logStart( String JavaDoc action ) {
59         if (willLog()) {
60             impl.start(action, System.nanoTime()/1000000);
61             actions.push(action);
62             if (DEBUG_NESTING) {
63                 places.push(new Throwable JavaDoc("logStart called here:")); // NOI18N
64
}
65         }
66     }
67
68     /** Note that something happened, but not an interval.
69      * The log will note only the time elapsed since the last interesting event.
70      * @param note some identifying string
71      */

72     public static void logProgress( String JavaDoc note ) {
73         if( willLog() ) impl.progress( note, System.nanoTime()/1000000 );
74     }
75
76     /** Stop running some interval action.
77      * The log will note the time elapsed since the start of the action.
78      * Actions <em>must</em> be properly nested.
79      * @param action some identifying string
80      */

81     public static void logEnd( String JavaDoc action ) {
82         if (willLog()) {
83             String JavaDoc old = actions.empty() ? null : actions.pop();
84             Throwable JavaDoc oldplace = DEBUG_NESTING && !places.empty() ? places.pop() : null;
85             if (!action.equals(old)) {
86                 // Error, not ISE; don't want this caught and reported
87
// with ErrorManager, for then you get a wierd cycle!
88
if (oldplace != null) {
89                     oldplace.printStackTrace();
90                 } else {
91                     System.err.println("Either ending too soon, or no info about caller of unmatched start log."); // NOI18N
92
System.err.println("Try running with -J-Dorg.netbeans.log.startup.debug=true"); // NOI18N
93
}
94                 Error JavaDoc e = new Error JavaDoc("StartLog mismatch: ending '" + action + "' but expecting '" + old + "'; rest of stack: " + actions); // NOI18N
95
// Print stack trace since you can get strange situations
96
// when ErrorManager tries to report it - may need to initialize
97
// ErrorManager, etc.
98
e.printStackTrace();
99                 // Presumably you did want to keep on going at this point.
100
// Note that this can only happen when logging is off
101
// (which is the default).
102
System.exit(1);
103             }
104             impl.end(action, System.nanoTime()/1000000);
105         }
106     }
107
108     public static boolean willLog() {
109         return impl.willLog();
110     }
111     
112     /** Logs the startup time. The begining is tracked by this class.
113      * The end is passed as argument.
114      */

115     public static void logMeasuredStartupTime(long end){
116         if (impl instanceof PerformanceTestsImpl) {
117             ((PerformanceTestsImpl)impl).log("IDE starts t=" + Long.toString(((PerformanceTestsImpl)impl).zero) + "\nIDE is running t=" + Long.toString(end) + "\n");
118             ((PerformanceTestsImpl)impl).writeLogs();
119         }
120     }
121     
122     
123     
124     /** The dummy, no-op implementation */
125     private static class StartImpl {
126         StartImpl() {}
127         void start( String JavaDoc action, long time ) {}
128         void progress( String JavaDoc note, long time ) {}
129         void end( String JavaDoc action, long time ) {}
130         boolean willLog() {
131             return false;
132         }
133     }
134
135     private static class PrintImpl extends StartImpl {
136         PrintImpl() {}
137         long zero = System.nanoTime()/1000000;
138         private Stack JavaDoc<Long JavaDoc> starts = new Stack JavaDoc<Long JavaDoc>();
139         long prog;
140         private int indent = 0;
141         
142         synchronized void start( String JavaDoc action, long time ) {
143             starts.push(time);
144             prog=time;
145             System.err.println( getIndentString(indent) + "@" +
146                     (time - zero) + " - " + action + " started" // NOI18N
147
);
148             indent += 2;
149         }
150         
151         synchronized void progress( String JavaDoc note, long time ) {
152             System.err.println( getIndentString(indent) + "@" +
153                     (time - zero) + " - " + note + " dT=" + (time - prog) // NOI18N
154
);
155             prog = time;
156         }
157         
158         synchronized void end( String JavaDoc action, long time ) {
159             indent -= 2;
160             long start = starts.pop();
161             prog = time;
162             System.err.println( getIndentString(indent) + "@" +
163                     (time - zero) + " - " + action + " finished, took " + // NOI18N
164
(time - start) + "ms" // NOI18N
165
);
166         }
167         
168         boolean willLog() {
169             return true;
170         }
171         
172         private char[] spaces = new char[0];
173         private String JavaDoc getIndentString( int indent ) {
174             if( spaces.length < indent ) {
175                 spaces = new char[Math.max( spaces.length*2, indent+10 )];
176                 Arrays.fill( spaces, ' ');
177             }
178             return new String JavaDoc(spaces,0, indent);
179         }
180     }
181
182     private static class PerformanceTestsImpl extends StartImpl {
183         private StringBuffer JavaDoc logs = new StringBuffer JavaDoc();
184         long zero = System.nanoTime()/1000000;
185         private Stack JavaDoc<Long JavaDoc> starts = new Stack JavaDoc<Long JavaDoc>();
186         long prog;
187         private int indent = 0;
188         
189         PerformanceTestsImpl() {}
190         
191         synchronized void start( String JavaDoc action, long time ) {
192             starts.push(time);
193             prog=time;
194             log(getIndentString(indent) + "@" +
195                     (time - zero) + " - " + action + " started" // NOI18N
196
);
197             indent += 2;
198         }
199         
200         synchronized void progress( String JavaDoc note, long time ) {
201             log(getIndentString(indent) + "@" +
202                     (time - zero) + " - " + note + " dT=" + (time - prog) // NOI18N
203
);
204             prog = time;
205         }
206         
207         synchronized void end( String JavaDoc action, long time ) {
208             indent -= 2;
209             long start = starts.pop();
210             prog = time;
211             log(getIndentString(indent) + "@" +
212                     (time - zero) + " - " + action + " finished, took " + // NOI18N
213
(time - start) + "ms" // NOI18N
214
);
215         }
216         
217         boolean willLog() {
218             return true;
219         }
220         
221         private char[] spaces = new char[0];
222         private String JavaDoc getIndentString( int indent ) {
223             if( spaces.length < indent ) {
224                 spaces = new char[Math.max( spaces.length*2, indent+10 )];
225                 Arrays.fill( spaces, ' ');
226             }
227             return new String JavaDoc(spaces,0, indent);
228         }
229         
230         synchronized void log(String JavaDoc log){
231             logs.append("\n" + log);
232         }
233         
234         synchronized void writeLogs(){
235             if(logFileProp!=null){
236                 try{
237                     java.io.File JavaDoc logFile = new java.io.File JavaDoc(logFileProp);
238                     java.io.FileWriter JavaDoc writer = new java.io.FileWriter JavaDoc(logFile);
239                     writer.write(logs.toString());
240                     writer.close();
241                 }catch (Exception JavaDoc exc){
242                     System.err.println("EXCEPTION rises during startup logging:");
243                     exc.printStackTrace(System.err);
244                 }
245             } else
246                 throw new IllegalStateException JavaDoc("You are trying to log startup logs to unexisting file. You have to set property org.netbeans.log.startup.logfile.");
247         }
248
249     }
250 }
251
Popular Tags