001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: /** this class offers some methods for assertions, debug output and so
012: * on */package de.uka.ilkd.key.util;
013:
014: public final class Debug {
015:
016: private Debug() {
017: }
018:
019: /** has to be set in order to enable assertion */
020: public static boolean ENABLE_ASSERTION = true;
021: /** has to be set in order to enable debugging */
022: public static boolean ENABLE_DEBUG = "on".equals(System
023: .getProperty("KeyDebugFlag"));
024:
025: /** prints given string if in debug mode
026: * @param msg the String to be printed
027: */
028: public static void out(String msg) {
029: if (ENABLE_DEBUG) {
030: System.out.println("DEBUG:: " + msg);
031: }
032: }
033:
034: /** prints given string and the result of calling the toString method of
035: * of the given obj if in debug mode. The advantage of calling the toString
036: * here and not before is that if debug mode is disabled no string
037: * computation is done
038: * @param msg the String to be printed
039: * @param obj the Object where to call the toString method
040: */
041: public static final void out(String msg, Object obj) {
042: if (ENABLE_DEBUG) {
043: System.out.println("DEBUG:: " + msg + " " + obj);
044: }
045: }
046:
047: /** prints given string and the result of calling the toString method of
048: * of the given objects if in debug mode. The advantage of calling the toString
049: * here and not before is that if debug mode is disabled no string
050: * computation is done
051: * @param msg the String to be printed
052: * @param obj1 the first Object where to call the toString method
053: * @param obj2 the second Object where to call the toString method
054: */
055: public static final void out(String msg, Object obj1, Object obj2) {
056: if (ENABLE_DEBUG) {
057: System.out.println("DEBUG:: " + msg + ":(" + obj1 + ", "
058: + obj2 + ")");
059: }
060: }
061:
062: /** prints given string and the result of calling the toString method of
063: * of the given objects if in debug mode. The advantage of calling the toString
064: * here and not before is that if debug mode is disabled no string
065: * computation is done
066: * @param msg the String to be printed
067: * @param obj1 the first Object where to call the toString method
068: * @param obj2 the second Object where to call the toString method
069: * @param obj3 the third Object where to call the toString method
070: */
071: public static final void out(String msg, Object obj1, Object obj2,
072: Object obj3) {
073: if (ENABLE_DEBUG) {
074: System.out.println("DEBUG:: " + msg + ":(" + obj1 + ", "
075: + obj2 + ", " + obj3 + ")");
076: }
077: }
078:
079: /** prints the given string followed by the int if in debug mode.
080: * @param msg the String to be printed
081: * @param id the int printed after msg
082: */
083: public static final void out(String msg, long id) {
084: if (ENABLE_DEBUG) {
085: System.out.println("DEBUG:: " + msg + " " + id);
086: }
087: }
088:
089: /** prints the given string followed by the int if in debug mode.
090: * @param msg the String to be printed
091: * @param id1 the int printed first after msg
092: * @param id1 the int printed second after msg
093: */
094: public static final void out(String msg, long id1, long id2) {
095: if (ENABLE_DEBUG) {
096: System.out.println("DEBUG:: " + msg + ":(" + id1 + ", "
097: + id2 + ")");
098: }
099: }
100:
101: /** prints the given string followed by the boolean if in debug mode.
102: * @param msg the String to be printed
103: * @param b the boolean printed after msg
104: */
105: public static final void out(String msg, boolean b) {
106: if (ENABLE_DEBUG) {
107: System.out.println("DEBUG:: " + msg + " " + b);
108: }
109: }
110:
111: /** prints given string, if the condition cond is true
112: * @param msg the String to be printed
113: * @param cond the boolean deciding if the message is printed or not
114: */
115: public static final void outIfEqual(String msg, boolean cond) {
116: if (ENABLE_DEBUG) {
117: if (cond) {
118: System.out.println("DEBUG:: " + msg);
119: }
120: }
121: }
122:
123: /** prints given string, if calling the equal method of obj1, with obj2 as
124: * argument results in true
125: * @param msg the String to be printed
126: * @param obj1 the Object where to call the equals method
127: * @param obj2 the Object given to as parameter of the equals method of obj1
128: */
129: public static final void outIfEqual(String msg, Object obj1,
130: Object obj2) {
131: if (ENABLE_DEBUG) {
132: if ((obj1 == null && obj2 == null)
133: || (obj1 != null && obj1.equals(obj2))) {
134: System.out.println("DEBUG:: " + msg);
135: }
136: }
137: }
138:
139: /** prints the stack trace if in debug mode
140: * @author VK
141: */
142: public static final void out(Exception e) {
143: if (ENABLE_DEBUG) {
144: e.printStackTrace();
145: }
146: }
147:
148: /** an assertion failure is thrown if isOK is evaluated to false
149: * @param isOK boolean the assertion that is checked
150: */
151: public static final void assertTrue(boolean isOK) {
152: if (ENABLE_ASSERTION) {
153: if (!isOK) {
154: fail();
155: }
156: }
157: }
158:
159: public static final void assertFalse(boolean isNotOK) {
160: assertTrue(!isNotOK);
161: }
162:
163: /** an assertion failure is thrown if isOK is evaluated to false
164: * the text in message is handed over to this exception
165: * @param isOK boolean the assertion that is checked
166: * @param message String describes the failed assertion
167: */
168: public static final void assertTrue(boolean isOK, String message) {
169: if (ENABLE_ASSERTION) {
170: if (!isOK) {
171: fail(message);
172: }
173: }
174: }
175:
176: /** an assertion failure is thrown if isOK is evaluated to false
177: * the text in message is handed over to this exception
178: * @param isOK boolean the assertion that is checked
179: * @param message String describes the failed assertion
180: */
181: public static final void assertTrue(boolean isOK, String message,
182: Object parameter) {
183: if (ENABLE_ASSERTION) {
184: if (!isOK) {
185: fail(message + ":" + parameter);
186: }
187: }
188: }
189:
190: public static final void assertFalse(boolean isNotOK, String message) {
191: assertTrue(!isNotOK, message);
192: }
193:
194: public static final void fail() {
195: fail("No further information available.");
196: }
197:
198: public static final void fail(String message) {
199: if (ENABLE_ASSERTION) {
200: throw new AssertionFailure("\nAssertion failure: "
201: + message);
202: }
203: }
204:
205: public static final void fail(String message, Object o) {
206: if (ENABLE_ASSERTION) {
207: throw new AssertionFailure("\nAssertion failure: "
208: + message + ":" + o);
209: }
210: }
211:
212: /**
213: * prints the stack trace of the given exception
214: */
215: public static final void printStackTrace(Exception e) {
216: if (ENABLE_DEBUG) {
217: e.printStackTrace(System.out);
218: }
219: }
220:
221: /**
222: * Prints a stack trace (without influencing the execution in any way).
223: * @author VK
224: */
225: public static final void printStackTrace() {
226: try {
227: throw new Exception();
228: } catch (Exception e) {
229: System.out
230: .println("************* DEBUG::Stacktrace *************");
231: e.printStackTrace(System.out);
232: }
233: }
234:
235: public static String stackTrace() {
236: Throwable t = new Throwable();
237: java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
238: t.printStackTrace(new java.io.PrintStream(baos));
239: return (baos.toString());
240: }
241:
242: }
|