1 package com.quadcap.util; 2 3 40 41 import java.io.File ; 42 import java.io.FileOutputStream ; 43 import java.io.IOException ; 44 import java.io.PrintStream ; 45 46 import java.util.Date ; 47 import java.util.Hashtable ; 48 49 import java.sql.SQLException ; 50 51 import java.text.SimpleDateFormat ; 52 53 import com.quadcap.sql.file.DatafileException; 54 55 60 public class Debug { 61 static public PrintStream debugStream = System.out; 62 static public int printLevel = 0; 63 64 private static String debugFileName = null; 65 private static FileOutputStream debugFileStream = null; 66 67 static Hashtable debugMap = new Hashtable (); 68 static NullSecurityManager sm = null; 69 70 static public final int debugOff = 0; 71 static public final int debugSome = 1; 72 static public final int debugAll = 3; 73 static public final int debugPackage = 4; 74 75 static public int debugMode = debugAll; 76 77 static public final int debugNoTime = 0; 78 static public final int debugElap = 1; 79 static public final int debugInterval = 2; 80 static public final int debugShowTime = 3; 81 82 static public int debugTime = debugShowTime; 83 84 static public long start = new Date ().getTime(); 85 static public long last = start; 86 87 static SimpleDateFormat sdf = 88 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS"); 89 90 91 95 96 100 101 121 static { 122 try { 124 ConfigString file = ConfigString.find("debug.file", "stderr"); 125 if (file != null) { 126 setLogFile(file.toString()); 127 } 128 ConfigNumber level = ConfigNumber.find("debug.level", "0"); 129 if (level != null) { 130 printLevel = level.intValue(); 131 } 132 } catch (IOException e) { 133 Debug.print(e); 134 } 135 } 136 137 static public Class [] getClassContext() { 138 return sm.pubGetClassContext(); 139 } 140 141 static public void setDebugLevel(String name, String level) { 142 debugMap.put(name, level); 143 } 144 145 static public void print(Throwable e) { 146 if (e == null) return; 147 if (!(e instanceof SQLException )) { 148 Debug.println(0, 2, e.getClass().getName()); 149 } 150 debugStream.print(hdr()); 151 e.printStackTrace(debugStream); 152 if (e instanceof SQLException ) { 153 printSQLException((SQLException )e); 154 } else if (e.getCause() != null) { 155 Debug.println("Cause of this exception:"); 156 print(e.getCause()); 157 } 158 } 159 160 static void printSQLException(SQLException e) { 161 StringBuffer sb = new StringBuffer (e.toString()); 162 int idx; 163 while ((idx = sb.toString().indexOf("Exception:")) >= 0) { 164 sb.setCharAt(idx + 9, ','); 165 } 166 Debug.println(0, 3, sb.toString() + ", SQLState = " + e.getSQLState() + 167 ", errorCode = " + e.getErrorCode()); 168 SQLException next = e.getNextException(); 169 if (next != null) { 170 Debug.println("Next SQL Exception:"); 171 printSQLException(next); 172 } else if (e.getCause() != null) { 173 Debug.println("Cause of this exception:"); 174 print(e); 175 } 176 } 177 178 static public void println(String s) { 179 println(0, 1, s); 180 } 181 182 static public String getInterval() { 183 String ret = null; 184 Date d = new Date (); 185 long now = d.getTime(); 186 187 switch (debugTime) { 188 case debugInterval: 189 ret = "" + (now - last); 190 last = now; 191 break; 192 case debugElap: 193 ret = "" + (now - start); 194 break; 195 case debugShowTime: 196 ret = "" + sdf.format(d); 197 break; 198 } 199 return ret; 200 } 201 202 211 static public void setLogFile(String name) throws IOException { 212 if (name.equals("stdout")) { 213 if (debugFileStream != null) { 214 debugFileStream.close(); 215 debugFileStream = null; 216 } 217 debugStream = System.out; 218 } else if (name.equals("stderr")) { 219 if (debugFileStream != null) { 220 debugFileStream.close(); 221 debugFileStream = null; 222 } 223 debugStream = System.err; 224 } else { 225 boolean append = name.startsWith("append:"); 226 if (append) name = name.substring(7).trim(); 227 name = new File (name).getAbsolutePath(); 228 if (debugFileName == null || !name.equals(debugFileName)) { 229 debugFileName = name; 230 if (debugFileStream != null) { 231 debugFileStream.close(); 232 debugFileStream = null; 233 } 234 debugFileStream = new FileOutputStream (name, append); 235 debugStream = new PrintStream (debugFileStream); 236 println("---- [Debug started]"); 237 } 238 } 239 } 240 241 247 static public void setLogLevel(int printLevel) { 248 if (printLevel != Debug.printLevel) { 249 Debug.println(0, "Changing log level from " + Debug.printLevel + 250 " to " + printLevel); 251 Debug.printLevel = printLevel; 252 } 253 } 254 255 260 static public void setDebugMode(String mode) { 261 if (mode.equals("off")) { 262 Debug.debugMode = Debug.debugOff; 263 } else if (mode.equals("some")) { 264 Debug.debugMode = Debug.debugSome; 265 } else if (mode.equals("all")) { 266 Debug.debugMode = Debug.debugAll; 267 } else if (mode.equals("package")) { 268 Debug.debugMode = Debug.debugPackage; 269 } 270 } 271 272 275 static synchronized public void println(int level, String s) { 276 println(level, 1, s); 277 } 278 279 static String hdr() { 280 StringBuffer sb = new StringBuffer (); 281 if (debugTime != debugNoTime) { 282 sb.append(getInterval()); 283 sb.append(": "); 284 } 285 sb.append("["); 286 sb.append(Thread.currentThread().getName()); 287 sb.append("] "); 288 return sb.toString(); 289 } 290 291 294 static synchronized public void println(int level, int depth, String s) { 295 if (level > printLevel) return; 296 debugStream.print(hdr()); 297 if (sm != null) { 298 String className = debugSite(level, depth+1); 299 debugStream.print(className + ": "); 300 } 301 debugStream.println(s); 302 } 303 304 static String debugSite(int level, int depth) { 305 if (debugMode == debugOff || level > printLevel) return null; 306 boolean print = true; 307 Class [] classes = sm.pubGetClassContext(); 308 309 String className = ""; 310 String packageName = ""; 311 if (classes.length > 2 + depth) { 312 Class caller = classes[2 + depth]; 313 className = caller.getName(); 314 int pos = className.lastIndexOf('.'); 315 if (pos >= 0) { 316 packageName = className.substring(0, pos); 317 className = className.substring(pos+1); 318 } 319 } 320 321 String debugVal; 322 323 switch (debugMode) { 324 case debugSome: 325 debugVal = (String )debugMap.get(className); 326 if (debugVal != null && level <= Integer.parseInt(debugVal)) { 327 print = debugMode == debugSome; 328 } 329 break; 330 case debugPackage: 331 debugVal = (String )debugMap.get(packageName); 332 if (debugVal != null && level <= Integer.parseInt(debugVal)) { 333 print = debugMode == debugPackage; 334 } 335 break; 336 } 337 338 if (print) return className; 339 return null; 340 } 341 342 } 343
| Popular Tags
|