1 19 20 package org.netbeans.core.startup; 21 22 import java.util.Stack ; 23 import java.util.Arrays ; 24 25 30 public class StartLog { 31 32 private static final StartImpl impl; 33 private static final Stack <String > actions = new Stack <String >(); 34 private static final Stack <Throwable > places = new Stack <Throwable >(); 35 private static final boolean DEBUG_NESTING = Boolean.getBoolean("org.netbeans.log.startup.debug"); 37 private static final String logProp; 38 private static final String logFileProp; 39 40 static { 41 logProp = System.getProperty( "org.netbeans.log.startup" ); logFileProp = System.getProperty( "org.netbeans.log.startup.logfile" ); 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 ("Unknown org.netbeans.log.startup value [" + logProp + "], it should be (print or tests) !"); } 54 55 58 public static void logStart( String action ) { 59 if (willLog()) { 60 impl.start(action, System.nanoTime()/1000000); 61 actions.push(action); 62 if (DEBUG_NESTING) { 63 places.push(new Throwable ("logStart called here:")); } 65 } 66 } 67 68 72 public static void logProgress( String note ) { 73 if( willLog() ) impl.progress( note, System.nanoTime()/1000000 ); 74 } 75 76 81 public static void logEnd( String action ) { 82 if (willLog()) { 83 String old = actions.empty() ? null : actions.pop(); 84 Throwable oldplace = DEBUG_NESTING && !places.empty() ? places.pop() : null; 85 if (!action.equals(old)) { 86 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."); System.err.println("Try running with -J-Dorg.netbeans.log.startup.debug=true"); } 94 Error e = new Error ("StartLog mismatch: ending '" + action + "' but expecting '" + old + "'; rest of stack: " + actions); e.printStackTrace(); 99 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 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 125 private static class StartImpl { 126 StartImpl() {} 127 void start( String action, long time ) {} 128 void progress( String note, long time ) {} 129 void end( String 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 <Long > starts = new Stack <Long >(); 139 long prog; 140 private int indent = 0; 141 142 synchronized void start( String action, long time ) { 143 starts.push(time); 144 prog=time; 145 System.err.println( getIndentString(indent) + "@" + 146 (time - zero) + " - " + action + " started" ); 148 indent += 2; 149 } 150 151 synchronized void progress( String note, long time ) { 152 System.err.println( getIndentString(indent) + "@" + 153 (time - zero) + " - " + note + " dT=" + (time - prog) ); 155 prog = time; 156 } 157 158 synchronized void end( String 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 " + (time - start) + "ms" ); 166 } 167 168 boolean willLog() { 169 return true; 170 } 171 172 private char[] spaces = new char[0]; 173 private String 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 (spaces,0, indent); 179 } 180 } 181 182 private static class PerformanceTestsImpl extends StartImpl { 183 private StringBuffer logs = new StringBuffer (); 184 long zero = System.nanoTime()/1000000; 185 private Stack <Long > starts = new Stack <Long >(); 186 long prog; 187 private int indent = 0; 188 189 PerformanceTestsImpl() {} 190 191 synchronized void start( String action, long time ) { 192 starts.push(time); 193 prog=time; 194 log(getIndentString(indent) + "@" + 195 (time - zero) + " - " + action + " started" ); 197 indent += 2; 198 } 199 200 synchronized void progress( String note, long time ) { 201 log(getIndentString(indent) + "@" + 202 (time - zero) + " - " + note + " dT=" + (time - prog) ); 204 prog = time; 205 } 206 207 synchronized void end( String action, long time ) { 208 indent -= 2; 209 long start = starts.pop(); 210 prog = time; 211 log(getIndentString(indent) + "@" + 212 (time - zero) + " - " + action + " finished, took " + (time - start) + "ms" ); 215 } 216 217 boolean willLog() { 218 return true; 219 } 220 221 private char[] spaces = new char[0]; 222 private String 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 (spaces,0, indent); 228 } 229 230 synchronized void log(String log){ 231 logs.append("\n" + log); 232 } 233 234 synchronized void writeLogs(){ 235 if(logFileProp!=null){ 236 try{ 237 java.io.File logFile = new java.io.File (logFileProp); 238 java.io.FileWriter writer = new java.io.FileWriter (logFile); 239 writer.write(logs.toString()); 240 writer.close(); 241 }catch (Exception exc){ 242 System.err.println("EXCEPTION rises during startup logging:"); 243 exc.printStackTrace(System.err); 244 } 245 } else 246 throw new IllegalStateException ("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 |