001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.util;
020:
021: import java.io.PrintWriter;
022:
023: /**
024: * logger used to print debugging messages based on the required debug level and
025: * message category.
026: * <2do> - replace this by log4j !
027: */
028: public class Debug {
029: public static final int ERROR = 0;
030: public static final int WARNING = 1;
031: public static final int MESSAGE = 2;
032: public static final int DEBUG = 3;
033: private static final int LAST_LEVEL = 4;
034: public static final int DEFAULT = 0;
035: public static final int RACE = 1;
036: public static final int LOCK_ORDER = 2;
037: public static final int DEPEND = 3;
038: public static final int DISTRIBUTED = 4;
039: public static final int SEARCH = 5;
040: public static final int TRACE = 6;
041: private static final int LAST_KIND = 7;
042: private static int[] enabled = new int[LAST_KIND];
043: private static String[] levels = { "error", "warning", "message",
044: "debug" };
045: private static String[] kinds = { "default", "race", "lock-order",
046: "depend", "distributed", "search", "trace" };
047:
048: public static void setDebugLevel(int l) {
049: if ((l < 0) || (l >= LAST_LEVEL)) {
050: throw new IllegalArgumentException("0 <= level < "
051: + LAST_LEVEL);
052: }
053:
054: enabled[DEFAULT] = l;
055: }
056:
057: public static void setDebugLevel(String ls) {
058: int l = mapLevel(ls);
059:
060: if (l == -1) {
061: throw new IllegalArgumentException(ls
062: + " is not a valid level");
063: }
064:
065: enabled[DEFAULT] = l;
066: }
067:
068: public static void setDebugLevel(int l, int k) {
069: if ((l < 0) || (l >= LAST_LEVEL)) {
070: throw new IllegalArgumentException("0 <= level < "
071: + LAST_LEVEL);
072: }
073:
074: if ((k < 0) || (k >= LAST_KIND)) {
075: throw new IllegalArgumentException("0 <= kind < "
076: + LAST_KIND);
077: }
078:
079: enabled[k] = l;
080: }
081:
082: public static void setDebugLevel(int l, String ks) {
083: if ((l < 0) || (l >= LAST_LEVEL)) {
084: throw new IllegalArgumentException("0 <= level < "
085: + LAST_LEVEL);
086: }
087:
088: int k = mapKind(ks);
089:
090: if (k == -1) {
091: throw new IllegalArgumentException(ks
092: + " is not a valid kind");
093: }
094:
095: enabled[k] = l;
096: }
097:
098: public static void setDebugLevel(String ls, int k) {
099: if ((k < 0) || (k >= LAST_KIND)) {
100: throw new IllegalArgumentException("0 <= kind < "
101: + LAST_KIND);
102: }
103:
104: int l = mapLevel(ls);
105:
106: if (l == -1) {
107: throw new IllegalArgumentException(ls
108: + " is not a valid level");
109: }
110:
111: enabled[k] = l;
112: }
113:
114: public static void setDebugLevel(String ls, String ks) {
115: int l = mapLevel(ls);
116:
117: if (l == -1) {
118: throw new IllegalArgumentException(ls
119: + " is not a valid level");
120: }
121:
122: int k = mapKind(ks);
123:
124: if (k == -1) {
125: throw new IllegalArgumentException(ks
126: + " is not a valid kind");
127: }
128:
129: enabled[k] = l;
130: }
131:
132: public static int getDebugLevel() {
133: return enabled[DEFAULT];
134: }
135:
136: public static int getDebugLevel(int k) {
137: return enabled[k];
138: }
139:
140: public static int getDebugLevel(String ks) {
141: int k = mapKind(ks);
142:
143: if (k == -1) {
144: throw new IllegalArgumentException(ks
145: + " is not a valid kind");
146: }
147:
148: return enabled[k];
149: }
150:
151: public static int mapKind(String ks) {
152: for (int k = 0; k < LAST_KIND; k++) {
153: if (ks.equals(kinds[k])) {
154: return k;
155: }
156: }
157:
158: return -1;
159: }
160:
161: public static int mapLevel(String ls) {
162: for (int l = 0; l < LAST_LEVEL; l++) {
163: if (ls.equals(levels[l])) {
164: return l;
165: }
166: }
167:
168: return -1;
169: }
170:
171: public static void print(int l, Object o) {
172: if (l <= enabled[DEFAULT]) {
173: System.err.print(o);
174: }
175: }
176:
177: public static void print(int l, String s) {
178: if (l <= enabled[DEFAULT]) {
179: System.err.print(s);
180: }
181: }
182:
183: public static void print(int l, int k, Object o) {
184: if (l <= enabled[k]) {
185: System.err.print(o);
186: }
187: }
188:
189: public static void print(int l, int k, String s) {
190: if (l <= enabled[k]) {
191: System.err.print(s);
192: }
193: }
194:
195: public static void print(int l, int k, Printable p) {
196: if (l <= enabled[k]) {
197: PrintWriter pw = new PrintWriter(System.err, true);
198: p.printOn(pw);
199: }
200: }
201:
202: public static void print(int l, Printable p) {
203: print(l, DEFAULT, p);
204: }
205:
206: public static void println(int l, int k, Printable p) {
207: if (l <= enabled[k]) {
208: PrintWriter pw = new PrintWriter(System.err, true);
209: p.printOn(pw);
210: System.err.println();
211: }
212: }
213:
214: public static void println(int l, Printable p) {
215: println(l, DEFAULT, p);
216: }
217:
218: public static void println(int l) {
219: if (l <= enabled[DEFAULT]) {
220: System.err.println();
221: }
222: }
223:
224: public static void println(int l, Object o) {
225: if (l <= enabled[DEFAULT]) {
226: System.err.println(o);
227: }
228: }
229:
230: public static void println(int l, String s) {
231: if (l <= enabled[DEFAULT]) {
232: System.err.println(s);
233: }
234: }
235:
236: public static void println(int l, int k) {
237: if (l <= enabled[k]) {
238: System.err.println();
239: }
240: }
241:
242: public static void println(int l, int k, Object o) {
243: if (l <= enabled[k]) {
244: System.err.println(o);
245: }
246: }
247:
248: public static void println(int l, int k, String s) {
249: if (l <= enabled[k]) {
250: System.err.println(s);
251: }
252: }
253:
254: public static String status() {
255: StringBuffer sb = new StringBuffer();
256:
257: for (int k = 1; k < LAST_KIND; k++) {
258: int l = enabled[k];
259:
260: if (l != ERROR) {
261: if (sb.length() != 0) {
262: sb.append(",");
263: }
264:
265: sb.append(kinds[k]);
266: sb.append("=");
267: sb.append(levels[l]);
268: }
269: }
270:
271: return sb.toString();
272: }
273: }
|