1 22 package org.aspectj.debugger.base; 23 24 import java.io.*; 25 import java.util.*; 26 import java.text.*; 27 28 public class ErrorLogger extends AbstractErrorLogger { 29 30 public ErrorLogger(Debugger debugger) { 31 super(debugger); 32 } 33 34 public ErrorLogger(Adapter adapter) { 35 super(adapter); 36 } 37 38 public ErrorLogger() { 39 super(new Adapter()); 40 } 41 42 public static class Adapter implements ErrorLoggable { 43 public Options getOptions() { 44 return new Options(); 45 } 46 public void fatal(String str) { 47 System.err.println(str); 48 System.exit(-1); 49 } 50 } 51 52 public static void main(String [] args) { 53 AbstractErrorLogger logger = new AbstractErrorLogger( 54 new ErrorLoggable() { 55 public Options getOptions() { 56 return new Options(); 57 } 58 public void fatal(String str) { 59 System.err.println(str); 60 } 61 } 62 ); 63 try { 64 String s = null; s.toString(); 65 } catch (Exception e) { 66 logger.log(e); 67 } 68 } 69 70 public static void error(Object o) { 71 AbstractErrorLogger logger = new AbstractErrorLogger( 72 new ErrorLoggable() { 73 public Options getOptions() { 74 return new Options(); 75 } 76 public void fatal(String str) { 77 System.err.println(str); 78 } 79 } 80 ); 81 if (o instanceof String ) { 82 logger.log((String ) o); 83 } else if (o instanceof Throwable ) { 84 logger.log((Throwable ) o); 85 } else { 86 logger.log(o + ""); 87 } 88 } 89 } 90 91 interface ErrorLoggable { 92 public Options getOptions(); 93 public void fatal(String str); 94 } 95 96 class AbstractErrorLogger { 97 98 private ErrorLoggable loggable; 99 private String errorFileName = ".ajdbErrors"; 100 101 public AbstractErrorLogger(ErrorLoggable loggable) { 102 this.loggable = loggable; 103 Options options = loggable.getOptions(); 104 Object userErrorFileName = options.get("errorfile"); 105 if (userErrorFileName != null) { 106 errorFileName = userErrorFileName + ""; 107 } 108 } 109 110 public void log(Throwable t) { 111 log("No message", t); 112 } 113 114 public void log(String msg) { 115 log(msg, new Throwable (msg)); 116 } 117 118 public void log(String msg, Throwable t) { 119 try { 120 PrintWriter printWriter = new PrintWriter(getOutputStream()); 121 printWriter.println(heading()); 122 printWriter.println(msg); 123 t.printStackTrace(printWriter); 124 printWriter.close(); 125 } catch (IOException ioe) { 126 loggable.fatal("Could not open error file for writing."); 127 } 128 } 129 130 131 private String heading() { 132 String date = DateFormat.getDateTimeInstance(DateFormat.FULL, 133 DateFormat.FULL).format(new Date()); 134 String username = System.getProperty("user.name"); 135 StringBuffer buf = new StringBuffer (); 136 buf.append("# -----------------------------------------------------------\n"); 137 buf.append("# Date: " + date + "\n"); 138 buf.append("# User: " + username + "\n"); 139 buf.append("# -----------------------------------------------------------\n"); 140 return buf.toString(); 141 } 142 143 public OutputStream getOutputStream() throws IOException { 144 File file = new File(getErrorFileName()); 145 boolean append = false; 146 if (file != null && file.exists()) { 147 append = true; 148 } 149 FileOutputStream out = new FileOutputStream(getErrorFileName(), append); 150 return out; 151 } 152 153 public String getErrorFileName() { 154 return errorFileName; 155 } 156 157 public void setErrorFile(String errorFileName) { 158 this.errorFileName = errorFileName; 159 } 160 } | Popular Tags |