1 package gov.nasa.jpf.util; 20 21 import java.io.PrintWriter ; 22 23 24 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 [] levels = { "error", "warning", "message", "debug" }; 45 private static String [] 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 ("0 <= level < " + LAST_LEVEL); 52 } 53 54 enabled[DEFAULT] = l; 55 } 56 57 public static void setDebugLevel (String ls) { 58 int l = mapLevel(ls); 59 60 if (l == -1) { 61 throw new IllegalArgumentException (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 ("0 <= level < " + LAST_LEVEL); 70 } 71 72 if ((k < 0) || (k >= LAST_KIND)) { 73 throw new IllegalArgumentException ("0 <= kind < " + LAST_KIND); 74 } 75 76 enabled[k] = l; 77 } 78 79 public static void setDebugLevel (int l, String ks) { 80 if ((l < 0) || (l >= LAST_LEVEL)) { 81 throw new IllegalArgumentException ("0 <= level < " + LAST_LEVEL); 82 } 83 84 int k = mapKind(ks); 85 86 if (k == -1) { 87 throw new IllegalArgumentException (ks + " is not a valid kind"); 88 } 89 90 enabled[k] = l; 91 } 92 93 public static void setDebugLevel (String ls, int k) { 94 if ((k < 0) || (k >= LAST_KIND)) { 95 throw new IllegalArgumentException ("0 <= kind < " + LAST_KIND); 96 } 97 98 int l = mapLevel(ls); 99 100 if (l == -1) { 101 throw new IllegalArgumentException (ls + " is not a valid level"); 102 } 103 104 enabled[k] = l; 105 } 106 107 public static void setDebugLevel (String ls, String ks) { 108 int l = mapLevel(ls); 109 110 if (l == -1) { 111 throw new IllegalArgumentException (ls + " is not a valid level"); 112 } 113 114 int k = mapKind(ks); 115 116 if (k == -1) { 117 throw new IllegalArgumentException (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 ks) { 132 int k = mapKind(ks); 133 134 if (k == -1) { 135 throw new IllegalArgumentException (ks + " is not a valid kind"); 136 } 137 138 return enabled[k]; 139 } 140 141 public static int mapKind (String 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 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 o) { 162 if (l <= enabled[DEFAULT]) { 163 System.err.print(o); 164 } 165 } 166 167 public static void print (int l, String s) { 168 if (l <= enabled[DEFAULT]) { 169 System.err.print(s); 170 } 171 } 172 173 public static void print (int l, int k, Object o) { 174 if (l <= enabled[k]) { 175 System.err.print(o); 176 } 177 } 178 179 public static void print (int l, int k, String 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 pw = new PrintWriter (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 pw = new PrintWriter (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 o) { 215 if (l <= enabled[DEFAULT]) { 216 System.err.println(o); 217 } 218 } 219 220 public static void println (int l, String 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 o) { 233 if (l <= enabled[k]) { 234 System.err.println(o); 235 } 236 } 237 238 public static void println (int l, int k, String s) { 239 if (l <= enabled[k]) { 240 System.err.println(s); 241 } 242 } 243 244 public static String status () { 245 StringBuffer sb = new StringBuffer (); 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 |