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 final class Debug { 43 44 48 private static final boolean PRINT_ALERT_TO_MESSAGES = false; 49 50 53 private static final boolean EXCEPTIONS_TO_ERR = false; 54 55 56 59 public final static int INFORMATION = 10; public final static int WARNING = 20; public final static int ALERT = 30; public final static int ERROR = 40; public final static int MESSAGE = 10000; 66 69 private static final Object debug_lock = new Object (); 70 71 74 static final PrintWriter SYSTEM_OUT = new PrintWriter (System.out, true); 75 76 79 static final PrintWriter SYSTEM_ERR = new PrintWriter (System.err, true); 80 81 82 86 static int debug_level = 0; 87 88 91 static PrintWriter out = SYSTEM_ERR; 92 93 96 static PrintWriter err = SYSTEM_ERR; 97 98 102 private static final void internalWrite(PrintWriter out, 103 int level, String class_string, String message) { 104 if (level < MESSAGE) { 105 out.print("> "); 106 out.print(class_string); 107 out.print(" ( lvl: "); 108 out.print(level); 109 out.print(" )\n "); 110 } 111 else { 112 out.print("% "); 113 } 114 out.println(message); 115 out.flush(); 116 } 117 118 122 public static final void setOutput(Writer out) { 123 Debug.out = new PrintWriter (out, false); 124 } 125 126 130 public static final void setDebugLevel(int level) { 131 debug_level = level; 132 } 133 134 138 public static final void listenToEventDispatcher() { 139 System.setProperty("sun.awt.exception.handler", 144 "com.mckoi.debug.DispatchNotify"); 145 } 147 148 149 155 public static final boolean isInterestedIn(int level) { 156 return (level >= debug_level); 157 } 158 159 165 public final static void write(int level, Object ob, String message) { 166 write(level, ob.getClass().getName(), message); 167 } 168 169 public final static void write(int level, Class cla, String message) { 170 write(level, cla.getName(), message); 171 } 172 173 public final static void write(int level, String class_string, String message) { 174 if (isInterestedIn(level)) { 175 176 synchronized (debug_lock) { 177 if (level >= ERROR && level < MESSAGE) { 178 internalWrite(SYSTEM_ERR, level, class_string, message); 179 } 180 else if (PRINT_ALERT_TO_MESSAGES) { 181 if (out != SYSTEM_ERR && level >= ALERT) { internalWrite(SYSTEM_ERR, level, class_string, message); 183 } 184 } 185 186 internalWrite(out, level, class_string, message); 187 } 188 189 } 190 } 191 192 195 public final static void write(Object ob, String message) { 196 write(5, ob, message); 197 } 198 199 202 private static final void writeTime() { 203 out.print("[ TIME: "); 204 out.print(new java.util.Date (System.currentTimeMillis())); 205 out.println(" ]"); 206 out.flush(); 207 } 208 209 213 public final static void writeException(Throwable e) { 214 writeException(ERROR, e); 215 } 216 217 221 public final static void writeException(int level, Throwable e) { 222 223 225 synchronized (debug_lock) { 226 if (level >= ERROR) { 227 System.err.print("[com.mckoi.debug.Debug - Exception thrown: '"); 228 System.err.print(e.getMessage()); 229 System.err.println("']"); 230 e.printStackTrace(System.err); 231 } 232 233 if (isInterestedIn(level)) { 234 writeTime(); 235 out.print("% "); 236 e.printStackTrace(out); 237 out.flush(); 238 } 239 } 240 } 241 242 243 244 } 245 | Popular Tags |