1 21 22 package org.apache.derby.impl.tools.dblook; 23 24 import java.io.PrintWriter ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 28 import java.sql.SQLException ; 29 import org.apache.derby.tools.dblook; 30 31 public class Logs { 32 33 private static PrintWriter logFile = null; 35 36 private static PrintWriter ddlFile = null; 38 39 private static String stmtEnd; 41 42 private static boolean verbose; 44 45 private static boolean atLeastOneDebug; 47 48 61 62 public static boolean initLogs(String logFileName, String ddlFileName, 63 boolean appendLogs, boolean doVerbose, String endOfStmt) 64 { 65 try { 66 67 logFile = new PrintWriter (new FileOutputStream (logFileName, appendLogs)); 68 ddlFile = (ddlFileName == null) ? null 69 : new PrintWriter (new FileOutputStream (ddlFileName, appendLogs)); 70 verbose = doVerbose; 71 stmtEnd = endOfStmt; 72 atLeastOneDebug = false; 73 } 74 catch (IOException ioe) 75 { 76 System.out.println("Error initializing log file(s): " + ioe); 77 return false; 78 } 79 80 return true; 81 82 } 83 84 85 95 96 public static void report(String msg) { 97 98 if (ddlFile == null) 99 System.out.println("-- " + msg); 100 else 101 ddlFile.println("-- " + msg); 102 103 return; 104 105 } 106 107 111 112 public static void reportString(String str) { 113 report(str); 114 } 115 116 120 121 public static void reportMessage(String key) { 122 reportMessage(key, (String [])null); 123 } 124 125 133 134 public static void reportMessage(String key, 135 String value) { 136 reportMessage(key, new String [] {value}); 137 } 138 139 147 148 public static void reportMessage(String key, 149 String [] values) { 150 151 String msg = dblook.lookupMessage(key, values); 152 report(msg); 153 154 } 155 156 162 163 public static void debug(Exception e) { 164 165 e.printStackTrace(logFile); 166 if (verbose) 167 e.printStackTrace(System.err); 168 atLeastOneDebug = true; 169 170 } 171 172 180 181 public static void debug(String key, 182 String value) 183 { 184 185 String msg = key; 186 if (value != null) { 187 msg = dblook.lookupMessage(key, 188 new String [] {value}); 189 } 190 191 logFile.println("-- **--> DEBUG: " + msg); 192 if (verbose) 193 System.err.println("-- **--> DEBUG: " + msg); 194 atLeastOneDebug = true; 195 196 } 197 198 206 207 public static void debug(String key, 208 String [] values) 209 { 210 211 String msg = key; 212 if (values != null) { 213 msg = dblook.lookupMessage(key, values); 214 } 215 216 logFile.println("-- **--> DEBUG: " + msg); 217 if (verbose) 218 System.err.println("-- **--> DEBUG: " + msg); 219 atLeastOneDebug = true; 220 221 } 222 223 229 230 public static String unRollExceptions(SQLException sqlE) { 231 232 String rv = sqlE.getMessage() + "\n"; 233 if (sqlE.getNextException() != null) 234 return rv + unRollExceptions(sqlE.getNextException()); 235 else 236 return rv; 237 238 } 239 240 245 246 public static void writeToNewDDL(String sql) { 247 248 if (ddlFile == null) 249 System.out.print(sql); 250 else 251 ddlFile.print(sql); 252 253 } 254 255 259 260 public static void writeStmtEndToNewDDL() { 261 262 if (ddlFile == null) 263 System.out.println(stmtEnd); 264 else 265 ddlFile.println(stmtEnd); 266 267 } 268 269 273 274 public static void writeNewlineToNewDDL() { 275 276 if (ddlFile == null) 277 System.out.println(); 278 else 279 ddlFile.println(); 280 } 281 282 289 290 public static boolean cleanup() { 291 292 try { 293 if (atLeastOneDebug) 294 dblook.writeVerboseOutput( 295 "DBLOOK_AtLeastOneDebug", null); 296 logFile.close(); 297 if (ddlFile != null) 298 ddlFile.close(); 299 300 } 301 catch (Exception e) { 302 System.out.println("Error releasing resources: " + e); 303 return false; 304 } 305 306 return true; 307 308 } 309 310 } 311 | Popular Tags |