1 21 22 package org.apache.derby.shared.common.sanity; 23 24 25 import java.util.Hashtable ; 26 import java.util.Enumeration ; 27 28 53 public class SanityManager { 54 60 61 public static final boolean ASSERT = SanityState.ASSERT; public static final boolean DEBUG = SanityState.DEBUG; 63 64 public static final String DEBUGDEBUG = "DumpSanityDebug"; 65 66 70 static private java.io.PrintWriter debugStream = new java.io.PrintWriter (System.err); 71 75 static private Hashtable DebugFlags = new Hashtable (); 76 79 static private boolean AllDebugOn = false; 80 static private boolean AllDebugOff = false; 81 82 86 94 public static final void ASSERT(boolean mustBeTrue) { 95 if (DEBUG) 96 if (! mustBeTrue) { 97 if (DEBUG) { 98 AssertFailure af = new AssertFailure("ASSERT FAILED"); 99 if (DEBUG_ON("AssertFailureTrace")) { 100 showTrace(af); 101 } 102 throw af; 103 } 104 else 105 throw new AssertFailure("ASSERT FAILED"); 106 } 107 } 108 109 116 public static final void ASSERT(boolean mustBeTrue, String msgIfFail) { 117 if (DEBUG) 118 if (! mustBeTrue) { 119 if (DEBUG) { 120 AssertFailure af = new AssertFailure("ASSERT FAILED " + msgIfFail); 121 if (DEBUG_ON("AssertFailureTrace")) { 122 showTrace(af); 123 } 124 throw af; 125 } 126 else 127 throw new AssertFailure("ASSERT FAILED " + msgIfFail); 128 } 129 } 130 131 143 public static final void THROWASSERT(String msgIfFail) { 144 148 if (DEBUG) { 149 AssertFailure af = new AssertFailure("ASSERT FAILED " + msgIfFail); 150 if (DEBUG_ON("AssertFailureTrace")) { 151 showTrace(af); 152 } 153 throw af; 154 } 155 else 156 throw new AssertFailure("ASSERT FAILED " + msgIfFail); 157 } 158 159 170 public static final void THROWASSERT(String msg, Throwable t) { 171 172 if (DEBUG) { 173 AssertFailure af = new AssertFailure("ASSERT FAILED " + t.toString(), t); 174 if (DEBUG_ON("AssertFailureTrace")) { 175 showTrace(af); 176 } 177 showTrace(t); 178 throw af; 179 } 180 else { 181 showTrace(t); 182 throw new AssertFailure("ASSERT FAILED " + t.toString(), t); 183 } 184 } 185 186 194 public static final void THROWASSERT(Throwable t) { 195 196 if (DEBUG) { 197 AssertFailure af = new AssertFailure("ASSERT FAILED " + t.toString(), t); 198 if (DEBUG_ON("AssertFailureTrace")) { 199 showTrace(af); 200 } 201 showTrace(t); 202 throw af; 203 } 204 else { 205 showTrace(t); 206 throw new AssertFailure("ASSERT FAILED " + t.toString(), t); 207 } 208 } 209 210 224 public static final void DEBUG(String flag, String message) { 225 if (DEBUG) { 226 if (DEBUG_ON(flag)) { 227 DEBUG_PRINT(flag, message); 228 } 229 } 230 } 231 232 248 public static final boolean DEBUG_ON(String flag) { 249 if (DEBUG) { 250 if (AllDebugOn) return true; 251 else if (AllDebugOff) return false; 252 else { 253 Boolean flagValue = (Boolean ) DebugFlags.get(flag); 254 if (! DEBUGDEBUG.equals(flag)) { 255 if (DEBUG_ON(DEBUGDEBUG)) { 256 DEBUG_PRINT(DEBUGDEBUG, "DEBUG_ON: Debug flag "+flag+" = "+flagValue); 257 } 258 } 259 if (flagValue == null) return false; 260 else return flagValue.booleanValue(); 261 } 262 } 263 else return false; 264 } 265 266 277 public static final void DEBUG_SET(String flag) { 278 if (DEBUG) { 279 if (! DEBUGDEBUG.equals(flag)) { 280 if (DEBUG_ON(DEBUGDEBUG)) 281 DEBUG_PRINT(DEBUGDEBUG, "DEBUG_SET: Debug flag " + flag); 282 } 283 284 DebugFlags.put(flag, Boolean.TRUE); 285 } 286 } 287 288 299 public static final void DEBUG_CLEAR(String flag) { 300 if (DEBUG) { 301 if (! DEBUGDEBUG.equals(flag)) { 302 if (DEBUG_ON(DEBUGDEBUG)) 303 DEBUG_PRINT(DEBUGDEBUG, "DEBUG_CLEAR: Debug flag " + flag); 304 } 305 306 DebugFlags.put(flag, Boolean.FALSE); 307 } 308 } 309 310 315 public static final void DEBUG_ALL_ON() { 316 if (DEBUG) { 317 AllDebugOn = true; 318 AllDebugOff = false; 319 } 320 } 321 322 327 public static final void DEBUG_ALL_OFF() { 328 if (DEBUG) { 329 AllDebugOff = true; 330 AllDebugOn = false; 331 } 332 } 333 334 338 static public void SET_DEBUG_STREAM(java.io.PrintWriter pw) { 339 debugStream = pw; 340 } 341 342 static public java.io.PrintWriter GET_DEBUG_STREAM() { 343 return debugStream; 344 } 345 346 static private void showTrace(AssertFailure af) { 347 af.printStackTrace(); 348 java.io.PrintWriter assertStream = GET_DEBUG_STREAM(); 349 350 assertStream.println("Assertion trace:"); 351 af.printStackTrace(assertStream); 352 assertStream.flush(); 353 } 354 355 static public void showTrace(Throwable t) { 356 java.io.PrintWriter assertStream = GET_DEBUG_STREAM(); 357 358 assertStream.println("Exception trace: "); 359 t.printStackTrace(assertStream); 360 } 361 362 373 static public void DEBUG_PRINT(String flag, String message) { 374 java.io.PrintWriter debugStream = GET_DEBUG_STREAM(); 375 376 debugStream.println("DEBUG "+flag+" OUTPUT: " + message); 377 debugStream.flush(); 378 } 379 380 public static void NOTREACHED() { 381 THROWASSERT("code should not be reached"); 382 } 383 } 384 385 | Popular Tags |