1 32 33 package com.knowgate.debug; 34 35 import java.util.Date ; 36 37 import java.io.FileWriter ; 38 import java.io.IOException ; 39 40 41 48 public final class DebugFile { 49 50 private static final String DEFAULT_TMP_UNIX = "/tmp/"; 51 private static final String DEFAULT_TMP_WIN32 = "C:\\"; 52 53 private static String sFilePath = null; 54 55 private static String chomp(String sSource, char cEndsWith) { 56 return sSource.charAt(sSource.length()-1)==cEndsWith ? sSource : sSource + cEndsWith; 57 } 59 63 public static void setFile(String sDebugFilePath) { 64 sFilePath = sDebugFilePath; 65 } 66 67 75 public static String getFile() { 76 if (null==sFilePath) { 77 if (System.getProperty("os.name").startsWith("Windows")) 78 sFilePath = chomp(System.getProperty("hipergate.debugdir", DEFAULT_TMP_WIN32),'\\') + "javatrc.txt"; 79 else 80 sFilePath = chomp(System.getProperty("hipergate.debugdir", DEFAULT_TMP_UNIX),'/') + "javatrc.txt"; 81 } 82 return sFilePath; 83 } 84 85 88 public static void incIdent() { 89 sIdent += " "; 90 } 91 92 95 96 public static void decIdent() { 97 if (sIdent.length()>2) 98 sIdent = sIdent.substring(0,sIdent.length()-2); 99 else 100 sIdent = ""; 101 } 102 103 107 public static void write(char[] str) { 108 FileWriter oDebugWriter; 109 110 try { 111 112 115 switch (dumpTo) { 116 case DUMP_TO_FILE: 117 oDebugWriter = new FileWriter (getFile(), true); 118 oDebugWriter.write(str); 119 oDebugWriter.close(); 120 break; 121 case DUMP_TO_STDOUT: 122 System.out.print(str); 123 break; 124 } 125 } 126 catch (IOException e) { 127 System.out.print(str); 128 } 129 } 130 131 135 public static void write(String str) { 136 FileWriter oDebugWriter; 137 138 try { 139 140 143 switch (dumpTo) { 144 case DUMP_TO_FILE: 145 oDebugWriter = new FileWriter (getFile(), true); 146 oDebugWriter.write(str); 147 oDebugWriter.close(); 148 break; 149 case DUMP_TO_STDOUT: 150 System.out.print(str); 151 break; 152 } 153 } 154 catch (IOException e) { 155 System.out.print(str); 156 } 157 } 158 159 163 public static void writeln(String str) { 164 FileWriter oDebugWriter; 165 Date dt = new Date (System.currentTimeMillis()); 166 167 try { 168 169 172 switch (dumpTo) { 173 case DUMP_TO_FILE: 174 oDebugWriter = new FileWriter (getFile(), true); 175 oDebugWriter.write(dt.toString()+" "+sIdent+str+"\n"); 176 oDebugWriter.close(); 177 break; 178 case DUMP_TO_STDOUT: 179 System.out.print(dt.toString()+sIdent+str+"\n"); 180 break; 181 } 182 } 183 catch (IOException e) { 184 System.out.print(dt.toString()+sIdent+str+"\n"); 185 } 186 } 187 188 192 public static void writeln(char[] str) { 193 FileWriter oDebugWriter; 194 Date dt = new Date (System.currentTimeMillis()); 195 196 try { 197 198 201 switch (dumpTo) { 202 case DUMP_TO_FILE: 203 oDebugWriter = new FileWriter (getFile(), true); 204 oDebugWriter.write(dt.toString()+" "+sIdent+str+"\n"); 205 oDebugWriter.close(); 206 break; 207 case DUMP_TO_STDOUT: 208 System.out.print(dt.toString()+sIdent+str+"\n"); 209 break; 210 } 211 } 212 catch (IOException e) { 213 System.out.print(dt.toString()+sIdent+str+"\n"); 214 } 215 } 217 221 public static void writeStackTrace(Throwable t) { 222 try { 223 DebugFile.write(StackTraceUtil.getStackTrace(t)); 224 } catch (Exception ignore) {} 225 } 226 227 231 public void debug(String str) { 232 DebugFile.writeln(str); 233 } 234 235 public void debug(String str, Exception xcpt) { 236 DebugFile.writeln(str); 237 new ErrorHandler(xcpt); 238 } 239 240 241 public static void envinfo() throws java.security.AccessControlException { 242 DebugFile.writeln(System.getProperty("java.vendor") + " Runtime Environment " + System.getProperty("java.version")); 243 DebugFile.writeln(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version")); 244 DebugFile.writeln(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); 245 DebugFile.writeln("JVM encoding " + System.getProperty( "file.encoding ")); 246 247 281 } 282 283 public static void main(String [] argv) { 284 System.out.println("Debug mode " + (trace ? "enabled" : "disabled")); 285 System.out.println("Debug file " + getFile()); 286 System.out.println(System.getProperty("java.vendor") + " Runtime Environment " + System.getProperty("java.version")); 287 System.out.println(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version")); 288 System.out.println(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); 289 System.out.println("JVM encoding " + System.getProperty( "file.encoding ")); 290 } 291 292 private static String sIdent = ""; 294 295 298 public static final short DUMP_TO_FILE = (short) 1; 299 public static final short DUMP_TO_STDOUT = (short) 2; 300 301 public static short dumpTo = DUMP_TO_FILE; 302 303 306 public static final boolean trace = false; 307 } 308
| Popular Tags
|