1 21 package com.izforge.izpack.util; 22 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.io.PrintWriter ; 28 import java.util.Date ; 29 import java.util.Enumeration ; 30 import java.util.Properties ; 31 32 import com.izforge.izpack.installer.Installer; 33 34 57 public class Debug 58 { 59 60 62 66 public static final String DLOG = "LOG"; 67 68 72 public static final String DSTACKTRACE = "STACKTRACE"; 73 74 78 public static final String DTRACE = "TRACE"; 79 80 81 public static final String IZPACK_LOGFILE = "izpack.logfile"; 82 83 84 public static final int LOG_WITHOUT_DATE = 0; 85 86 87 public static final int LOG_WITH_DATE = 1; 88 89 90 public static final int LOG_WITH_TIME_STAMP = 2; 91 92 93 public static final int LOG_WITH_TIME_AND_DATE = LOG_WITH_DATE | LOG_WITH_TIME_STAMP; 94 95 96 private static boolean TRACE; 97 98 99 private static boolean STACKTRACE; 100 101 102 private static boolean LOG; 103 104 105 public static String LOGFILE_PREFIX = "IzPack_Logfile_at_"; 106 107 108 public static String LOGFILE_EXTENSION = ".txt"; 109 110 111 public static String LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis() 112 + LOGFILE_EXTENSION; 113 114 117 static 118 { 119 boolean st = false; 120 121 try 122 { 123 st = Boolean.getBoolean(DSTACKTRACE); 124 } 125 catch (Exception ex) 126 { 127 } 129 130 STACKTRACE = st; 131 132 boolean log = false; 133 134 try 135 { 136 log = Boolean.getBoolean(DLOG); 137 } 138 catch (Exception ex) 139 { 140 } 142 143 LOG = log; 144 145 boolean t = false; 146 147 try 148 { 149 if (STACKTRACE) 150 { 151 t = true; 152 } 153 else 154 { 155 t = Boolean.getBoolean(DTRACE); 156 } 157 } 158 catch (Exception ex) 159 { 160 } 162 163 TRACE = t; 164 165 if (LOG) 166 { 167 System.out.println(DLOG + " enabled."); 168 PrintWriter logfile = createLogFile(); 169 170 Debug.log(Installer.class.getName() + " LogFile created at "); 171 172 Debug.log("System.Properties:", LOG_WITH_TIME_STAMP); 174 175 Properties sysProps = System.getProperties(); 176 177 Enumeration spe = sysProps.keys(); 178 179 while (spe.hasMoreElements()) 180 { 181 String aKey = (String ) spe.nextElement(); 182 Debug.log(aKey + " = " + sysProps.getProperty(aKey), LOG_WITHOUT_DATE); 183 } 184 Debug.log("\n==========================================\n", LOG_WITHOUT_DATE); 185 Debug.log("\n " + Installer.class.getName() + " installs on: \n", LOG_WITHOUT_DATE); 186 Debug.log(OsVersion.getOsDetails(), LOG_WITHOUT_DATE); 187 Debug.log("\n==========================================\n", LOG_WITHOUT_DATE); 188 } 189 190 if (TRACE) 191 { 192 System.out.println(DTRACE + " enabled."); 193 } 194 195 if (STACKTRACE) 196 { 197 System.out.println(DSTACKTRACE + " enabled."); 198 } 199 } 200 201 203 208 public static void trace(Object s) 209 { 210 if (TRACE) 211 { 212 System.out.println(s); 214 215 if (STACKTRACE && (s instanceof Throwable )) 216 { 217 ((Throwable ) s).printStackTrace(); 222 } 223 224 System.out.flush(); 225 } 226 } 227 228 233 public static void error(Object s) 234 { 235 trace(s); 236 System.err.println(s); 237 System.err.flush(); 238 log(s); 239 } 240 241 247 public static void log(Object o) 248 { 249 log(o, LOG_WITH_TIME_AND_DATE); 250 } 251 252 259 public static void log(Object o, int withWhatFormat) 260 { 261 if (LOG) 263 { 264 PrintWriter logfile; 265 if ((logfile = getLogFile()) == null) 266 { 267 logfile = createLogFile(); 268 } 269 270 if (logfile != null) 271 { 272 if (o == null) 273 { 274 o = "null"; 275 } 276 277 StringBuffer entry = new StringBuffer (); 278 if (logWithTimeStamp(withWhatFormat)) 279 { 280 entry.append(System.currentTimeMillis()); 281 entry.append(';'); 282 entry.append(' '); 283 } 284 if (logWithDate(withWhatFormat)) 285 { 286 entry.append(new Date ()); 287 entry.append(';'); 288 entry.append(' '); 289 } 290 291 entry.append(o); 292 293 logfile.println(entry.toString()); 294 295 if (o instanceof Throwable ) 296 { 297 ((Throwable ) o).printStackTrace(logfile); 298 } 299 300 logfile.flush(); 301 302 } 305 else 306 { 307 System.err.println("Cannot write into logfile: (" + logfile + ") <- '" + o + "'"); 308 } 309 } 310 } 311 312 318 private static boolean logWithDate(int withWhatFormat) 319 { 320 321 return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE; 322 } 323 324 330 private static boolean logWithTimeStamp(int withWhatFormat) 331 { 332 333 return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE; 334 } 335 336 341 private static PrintWriter createLogFile() 342 { 343 String tempDir = System.getProperty("java.io.tmpdir"); 344 345 File tempDirFile = new File (tempDir); 346 347 try 348 { 349 tempDirFile.mkdirs(); 350 } 351 catch (RuntimeException e1) 352 { 353 e1.printStackTrace(); 354 } 355 356 String logfilename = LOGFILENAME; 357 System.out.println("creating Logfile: '" + logfilename + "' in: '" + tempDir + "'"); 358 359 File out = new File (tempDir, logfilename); 360 361 PrintWriter logfile; 362 if (tempDirFile.canWrite()) 363 { 364 try 365 { 366 BufferedWriter fw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ( 367 out), "UTF-8")); 368 logfile = setLogFile(new PrintWriter (fw)); 369 } 370 catch (Exception e) 371 { 372 logfile = null; 373 e.printStackTrace(); 374 } 375 } 376 else 377 { 378 logfile = null; 379 System.err.println("Fatal: cannot write File: '" + logfilename + "' into: " 380 + tempDirFile); 381 } 382 383 return logfile; 384 } 385 386 391 public static boolean tracing() 392 { 393 return TRACE; 394 } 395 396 401 public static boolean stackTracing() 402 { 403 return STACKTRACE; 404 } 405 406 411 public static boolean isLOG() 412 { 413 return LOG; 414 } 415 416 421 public static void setLOG(boolean aFlag) 422 { 423 System.out.println(DLOG + " = " + aFlag); 424 LOG = aFlag; 425 } 426 427 432 public static boolean isSTACKTRACE() 433 { 434 return STACKTRACE; 435 } 436 437 442 public static void setSTACKTRACE(boolean aFlag) 443 { 444 System.out.println(DSTACKTRACE + " = " + aFlag); 445 STACKTRACE = aFlag; 446 } 447 448 453 public static boolean isTRACE() 454 { 455 return TRACE; 456 } 457 458 463 public static void setTRACE(boolean aFlag) 464 { 465 System.out.println(DTRACE + " = " + aFlag); 466 TRACE = aFlag; 467 } 468 469 474 public static PrintWriter getLogFile() 475 { 476 PrintWriter logfile = (PrintWriter ) System.getProperties().get(IZPACK_LOGFILE); 477 478 return logfile; 479 } 480 481 487 public static synchronized PrintWriter setLogFile(PrintWriter aLogFile) 488 { 489 System.getProperties().put(IZPACK_LOGFILE, aLogFile); 490 491 PrintWriter logfile = (PrintWriter ) System.getProperties().get(IZPACK_LOGFILE); 492 493 if (logfile == null) 494 { 495 System.err.println("Set::logfile == null"); 496 } 497 498 return logfile; 499 } 500 } 501 | Popular Tags |