1 24 25 package com.mckoi.debug; 26 27 import java.io.PrintStream ; 28 import java.io.PrintWriter ; 29 import java.io.Writer ; 30 31 41 42 public class DefaultDebugLogger implements DebugLogger { 43 44 48 private static final boolean PRINT_ALERT_TO_MESSAGES = false; 49 50 51 54 private final Object debug_lock = new Object (); 55 56 59 static final PrintWriter SYSTEM_OUT = new PrintWriter (System.out, true); 60 61 64 static final PrintWriter SYSTEM_ERR = new PrintWriter (System.err, true); 65 66 67 71 private int debug_level = 0; 72 73 76 private PrintWriter out = SYSTEM_ERR; 77 78 81 private PrintWriter err = SYSTEM_ERR; 82 83 84 88 private final void internalWrite(PrintWriter out, 89 int level, String class_string, String message) { 90 synchronized(out) { 91 if (level < MESSAGE) { 92 out.print("> "); 93 out.print(class_string); 94 out.print(" ( lvl: "); 95 out.print(level); 96 out.print(" )\n "); 97 } 98 else { 99 out.print("% "); 100 } 101 out.println(message); 102 out.flush(); 103 } 104 } 105 106 110 public final void setOutput(Writer out) { 111 this.out = new PrintWriter (out, false); 112 } 113 114 118 public final void setDebugLevel(int level) { 119 debug_level = level; 120 } 121 122 126 public final void listenToEventDispatcher() { 127 System.setProperty("sun.awt.exception.handler", 132 "com.mckoi.debug.DispatchNotify"); 133 } 135 136 137 139 public final boolean isInterestedIn(int level) { 140 return (level >= debug_level); 141 } 142 143 public final void write(int level, Object ob, String message) { 144 write(level, ob.getClass().getName(), message); 145 } 146 147 public final void write(int level, Class cla, String message) { 148 write(level, cla.getName(), message); 149 } 150 151 public final void write(int level, String class_string, String message) { 152 if (isInterestedIn(level)) { 153 154 if (level >= ERROR && level < MESSAGE) { 155 internalWrite(SYSTEM_ERR, level, class_string, message); 156 } 157 else if (PRINT_ALERT_TO_MESSAGES) { 158 if (out != SYSTEM_ERR && level >= ALERT) { internalWrite(SYSTEM_ERR, level, class_string, message); 160 } 161 } 162 163 internalWrite(out, level, class_string, message); 164 } 165 166 } 167 168 private final void writeTime() { 169 synchronized(out) { 170 out.print("[ TIME: "); 171 out.print(new java.util.Date (System.currentTimeMillis())); 172 out.println(" ]"); 173 out.flush(); 174 } 175 } 176 177 public final void writeException(Throwable e) { 178 writeException(ERROR, e); 179 } 180 181 public synchronized final void writeException(int level, Throwable e) { 182 183 if (level >= ERROR) { 184 synchronized(SYSTEM_ERR) { 185 SYSTEM_ERR.print("[com.mckoi.debug.Debug - Exception thrown: '"); 186 SYSTEM_ERR.print(e.getMessage()); 187 SYSTEM_ERR.println("']"); 188 e.printStackTrace(SYSTEM_ERR); 189 } 190 } 191 192 if (isInterestedIn(level)) { 193 synchronized(out) { 194 writeTime(); 195 out.print("% "); 196 e.printStackTrace(out); 197 out.flush(); 198 } 199 } 200 201 } 202 203 } 204 | Popular Tags |