1 19 package org.netbeans.lib.jmi.util; 20 21 import java.io.*; 22 import java.util.*; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import org.openide.ErrorManager; 26 27 31 public class Logger extends ErrorManager { 32 private static final String PROP_MIN_SEVERITY = "org.netbeans.lib.jmi.Logger"; 33 private static final String PROP_LOG_FILE = "org.netbeans.lib.jmi.Logger.fileName"; 34 35 private static ErrorManager defaultManager = null; 36 37 38 private static final Map map = new WeakHashMap (11); 39 40 41 private final LogWriter logWriter; 42 43 44 private final int minLogSeverity; 45 46 47 private final String name; 48 49 public static synchronized ErrorManager getDefault() { 50 if (defaultManager == null) { 51 defaultManager = ErrorManager.getDefault().getInstance(PROP_MIN_SEVERITY); 52 } 53 return defaultManager; 54 } 55 56 public Logger() { 57 name = null; 58 minLogSeverity = ErrorManager.INFORMATIONAL + 1; 59 String fileName = System.getProperty(PROP_LOG_FILE); 60 if (fileName == null) { 61 logWriter = new LogWriter(System.err); 62 } else { 63 if (fileName.equals("")) { 64 logWriter = new LogWriter(); 65 } else { 66 LogWriter writer; 67 try { 68 writer = new LogWriter(new FileOutputStream(fileName)); 69 } catch (FileNotFoundException e) { 70 writer = new LogWriter(System.err); 71 notify(e); 72 } 73 logWriter = writer; 74 } 75 } 76 } 77 78 public Logger(String name, int minSeverity, LogWriter logWriter) { 79 this.name = name; 80 this.minLogSeverity = minSeverity; 81 this.logWriter = logWriter; 82 } 83 84 98 public Throwable annotate(Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 99 List annotations = (List) map.get(t); 100 101 if (annotations == null) { 102 annotations = new ArrayList(); 103 map.put(t, annotations); 104 } 105 106 annotations.add(0, new Ann(severity, message, localizedMessage, stackTrace, date)); 107 108 return t; 109 } 110 111 118 public Throwable attachAnnotations(Throwable t, Annotation[] arr) { 119 List annotations = (List) map.get(t); 120 if (annotations == null) { 121 annotations = new ArrayList(arr.length + 5); 122 map.put(t, annotations); 123 } 124 annotations.addAll(0, Arrays.asList(arr)); 125 return t; 126 } 127 128 133 public Annotation[] findAnnotations(Throwable t) { 134 List annotations = (List) map.get(t); 135 if (annotations != null) { 136 return (Annotation[]) annotations.toArray(new Annotation[annotations.size()]); 137 } else { 138 return null; 139 } 140 } 141 142 157 public ErrorManager getInstance(String name) { 158 String loggerName = (this.name == null) ? name : this.name + "." + name; 159 String prop = loggerName; 160 int minLogSeverity = this.minLogSeverity; 161 while (prop != null) { 162 String value = System.getProperty(prop); 163 if (value != null) { 164 try { 165 minLogSeverity = Integer.parseInt(value); 166 } catch (NumberFormatException nfe) { 167 notify(WARNING, nfe); 168 } 169 break; 170 } else { 171 int idx = prop.lastIndexOf('.'); 172 if (idx == -1) 173 prop = null; 174 else 175 prop = prop.substring(0, idx); 176 } 177 } 178 return new Logger(loggerName, minLogSeverity, logWriter); 179 } 180 181 186 public void log(int severity, String s) { 187 if (isLoggable(severity)) { 188 if (name != null) { 189 logWriter.printlnIndented("[" + name + "] " + s); 190 } else { 191 logWriter.printlnIndented(s); 192 } 193 logWriter.flush(); 194 } 195 } 196 197 public boolean isLoggable(int severity) { 198 return severity >= minLogSeverity; 199 } 200 201 public boolean isNotifiable(int severity) { 202 return isLoggable(severity + 1); 203 } 204 205 210 public void notify(int severity, Throwable t) { 211 notify(severity, t, new HashSet ()); 212 } 213 214 private void notify(int severity, Throwable t, Set visited) { 215 if (!isNotifiable(severity)) 216 return; 217 218 if (!visited.add(t)) return; 219 Annotation[] ann = findAnnotations(t); 220 221 String level = (severity == INFORMATIONAL ? "INFORMATIONAL " : ""); 222 if (name != null) { 223 level = "[" + name + "] " + level; 224 } 225 logWriter.printlnIndented(level + "*********** Exception occurred ************ at " + new Date()); 226 t.printStackTrace(logWriter); 227 if (ann != null) { 228 logWriter.printlnIndented("ANNOTATIONS:"); 229 logWriter.indent(); 230 for (int i = 0; i < ann.length; i++) { 231 logAnnotation(ann[i], visited); 232 } 233 logWriter.unindent(); 234 } 235 logWriter.flush(); 236 } 237 238 private void logAnnotation(Annotation ann, Set visited) { 239 StringBuffer sb = new StringBuffer (100); 240 sb.append("[" + ann.getDate() + "] "); 241 if (ann.getSeverity() == INFORMATIONAL) { 242 sb.append("INFORMATIONAL "); 243 } 244 if (ann.getLocalizedMessage() == null) { 245 if (ann.getMessage() == null) { 246 if (ann.getStackTrace() != null) { 247 sb.append("Exception occurred:"); 248 } 249 } else { 250 sb.append(ann.getMessage()); 251 } 252 } else { 253 sb.append(ann.getLocalizedMessage()); 254 } 255 logWriter.printlnIndented(sb.toString()); 256 if (ann.getStackTrace() != null) { 257 notify(ann.getSeverity(), ann.getStackTrace(), visited); 259 } 260 } 261 262 264 private static class Ann implements Annotation { 265 private final int severity; 266 private final String message; 267 private final String localizedMessage; 268 private final Throwable stackTrace; 269 private final Date date; 270 271 273 public Ann (int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 274 this.severity = severity; 275 this.message = message; 276 this.localizedMessage = localizedMessage; 277 this.stackTrace = stackTrace; 278 this.date = date; 279 } 280 281 284 public String getMessage() { 285 return message; 286 } 287 290 public String getLocalizedMessage() { 291 return localizedMessage; 292 } 293 298 public Throwable getStackTrace() { 299 return stackTrace; 300 } 301 304 public Date getDate() { 305 return date; 306 } 307 310 public int getSeverity() { 311 return severity; 312 } 313 } 315 private static class EmptyStream extends OutputStream { 316 public void write(int b) throws IOException{ 317 } 318 } 319 320 private static class LogWriter extends PrintWriter { 321 private static final String TAB = " "; 322 private int indent = 0; 323 324 public LogWriter() { 325 super(new EmptyStream()); 326 } 327 328 public LogWriter(OutputStream stream) { 329 super(stream); 330 } 331 332 public void printIndented(String str) { 333 for (int i = 0; i < indent; i++) { 334 print(TAB); 335 } 336 print(str); 337 } 338 339 public void printlnIndented(String str) { 340 print(str); 341 println(); 342 } 343 344 public void indent() { 345 indent++; 346 } 347 348 public void unindent() { 349 indent--; 350 } 351 } 352 } 353 | Popular Tags |