KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > Debug


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21 import java.io.PrintWriter JavaDoc;
22
23
24 /**
25  * logger used to print debugging messages based on the required debug level and
26  * message category.
27  * <2do> - replace this by log4j !
28  */

29 public class Debug {
30   public static final int ERROR = 0;
31   public static final int WARNING = 1;
32   public static final int MESSAGE = 2;
33   public static final int DEBUG = 3;
34   private static final int LAST_LEVEL = 4;
35   public static final int DEFAULT = 0;
36   public static final int RACE = 1;
37   public static final int LOCK_ORDER = 2;
38   public static final int DEPEND = 3;
39   public static final int DISTRIBUTED = 4;
40   public static final int SEARCH = 5;
41   public static final int TRACE = 6;
42   private static final int LAST_KIND = 7;
43   private static int[] enabled = new int[LAST_KIND];
44   private static String JavaDoc[] levels = { "error", "warning", "message", "debug" };
45   private static String JavaDoc[] kinds = {
46     "default", "race", "lock-order", "depend", "distributed", "search", "trace"
47   };
48
49   public static void setDebugLevel (int l) {
50     if ((l < 0) || (l >= LAST_LEVEL)) {
51       throw new IllegalArgumentException JavaDoc("0 <= level < " + LAST_LEVEL);
52     }
53
54     enabled[DEFAULT] = l;
55   }
56
57   public static void setDebugLevel (String JavaDoc ls) {
58     int l = mapLevel(ls);
59
60     if (l == -1) {
61       throw new IllegalArgumentException JavaDoc(ls + " is not a valid level");
62     }
63
64     enabled[DEFAULT] = l;
65   }
66
67   public static void setDebugLevel (int l, int k) {
68     if ((l < 0) || (l >= LAST_LEVEL)) {
69       throw new IllegalArgumentException JavaDoc("0 <= level < " + LAST_LEVEL);
70     }
71
72     if ((k < 0) || (k >= LAST_KIND)) {
73       throw new IllegalArgumentException JavaDoc("0 <= kind < " + LAST_KIND);
74     }
75
76     enabled[k] = l;
77   }
78
79   public static void setDebugLevel (int l, String JavaDoc ks) {
80     if ((l < 0) || (l >= LAST_LEVEL)) {
81       throw new IllegalArgumentException JavaDoc("0 <= level < " + LAST_LEVEL);
82     }
83
84     int k = mapKind(ks);
85
86     if (k == -1) {
87       throw new IllegalArgumentException JavaDoc(ks + " is not a valid kind");
88     }
89
90     enabled[k] = l;
91   }
92
93   public static void setDebugLevel (String JavaDoc ls, int k) {
94     if ((k < 0) || (k >= LAST_KIND)) {
95       throw new IllegalArgumentException JavaDoc("0 <= kind < " + LAST_KIND);
96     }
97
98     int l = mapLevel(ls);
99
100     if (l == -1) {
101       throw new IllegalArgumentException JavaDoc(ls + " is not a valid level");
102     }
103
104     enabled[k] = l;
105   }
106
107   public static void setDebugLevel (String JavaDoc ls, String JavaDoc ks) {
108     int l = mapLevel(ls);
109
110     if (l == -1) {
111       throw new IllegalArgumentException JavaDoc(ls + " is not a valid level");
112     }
113
114     int k = mapKind(ks);
115
116     if (k == -1) {
117       throw new IllegalArgumentException JavaDoc(ks + " is not a valid kind");
118     }
119
120     enabled[k] = l;
121   }
122
123   public static int getDebugLevel () {
124     return enabled[DEFAULT];
125   }
126
127   public static int getDebugLevel (int k) {
128     return enabled[k];
129   }
130
131   public static int getDebugLevel (String JavaDoc ks) {
132     int k = mapKind(ks);
133
134     if (k == -1) {
135       throw new IllegalArgumentException JavaDoc(ks + " is not a valid kind");
136     }
137
138     return enabled[k];
139   }
140
141   public static int mapKind (String JavaDoc ks) {
142     for (int k = 0; k < LAST_KIND; k++) {
143       if (ks.equals(kinds[k])) {
144         return k;
145       }
146     }
147
148     return -1;
149   }
150
151   public static int mapLevel (String JavaDoc ls) {
152     for (int l = 0; l < LAST_LEVEL; l++) {
153       if (ls.equals(levels[l])) {
154         return l;
155       }
156     }
157
158     return -1;
159   }
160
161   public static void print (int l, Object JavaDoc o) {
162     if (l <= enabled[DEFAULT]) {
163       System.err.print(o);
164     }
165   }
166
167   public static void print (int l, String JavaDoc s) {
168     if (l <= enabled[DEFAULT]) {
169       System.err.print(s);
170     }
171   }
172
173   public static void print (int l, int k, Object JavaDoc o) {
174     if (l <= enabled[k]) {
175       System.err.print(o);
176     }
177   }
178
179   public static void print (int l, int k, String JavaDoc s) {
180     if (l <= enabled[k]) {
181       System.err.print(s);
182     }
183   }
184
185   public static void print (int l, int k, Printable p) {
186     if (l <= enabled[k]) {
187       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.err, true);
188       p.printOn(pw);
189     }
190   }
191
192   public static void print (int l, Printable p) {
193     print(l, DEFAULT, p);
194   }
195
196   public static void println (int l, int k, Printable p) {
197     if (l <= enabled[k]) {
198       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.err, true);
199       p.printOn(pw);
200       System.err.println();
201     }
202   }
203
204   public static void println (int l, Printable p) {
205     println(l, DEFAULT, p);
206   }
207
208   public static void println (int l) {
209     if (l <= enabled[DEFAULT]) {
210       System.err.println();
211     }
212   }
213
214   public static void println (int l, Object JavaDoc o) {
215     if (l <= enabled[DEFAULT]) {
216       System.err.println(o);
217     }
218   }
219
220   public static void println (int l, String JavaDoc s) {
221     if (l <= enabled[DEFAULT]) {
222       System.err.println(s);
223     }
224   }
225
226   public static void println (int l, int k) {
227     if (l <= enabled[k]) {
228       System.err.println();
229     }
230   }
231
232   public static void println (int l, int k, Object JavaDoc o) {
233     if (l <= enabled[k]) {
234       System.err.println(o);
235     }
236   }
237
238   public static void println (int l, int k, String JavaDoc s) {
239     if (l <= enabled[k]) {
240       System.err.println(s);
241     }
242   }
243
244   public static String JavaDoc status () {
245     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
246
247     for (int k = 1; k < LAST_KIND; k++) {
248       int l = enabled[k];
249
250       if (l != ERROR) {
251         if (sb.length() != 0) {
252           sb.append(",");
253         }
254
255         sb.append(kinds[k]);
256         sb.append("=");
257         sb.append(levels[l]);
258       }
259     }
260
261     return sb.toString();
262   }
263 }
264
Popular Tags