00001 package gov.nasa.arc.ase.util; 00002 00003 public class Debug { 00004 public static final int ERROR = 0; 00005 public static final int WARNING = 1; 00006 public static final int MESSAGE = 2; 00007 public static final int DEBUG = 3; 00008 private static final int LAST_LEVEL = 4; 00009 00010 public static final int DEFAULT = 0; 00011 public static final int RACE = 1; 00012 public static final int LOCK_ORDER = 2; 00013 public static final int DEPEND = 3; 00014 public static final int DISTRIBUTED = 4; 00015 public static final int SEARCH = 5; 00016 public static final int TRACE = 6; 00017 private static final int LAST_KIND = 7; 00018 00019 private static int[] enabled = new int[LAST_KIND]; 00020 private static String[] levels = { 00021 "error", 00022 "warning", 00023 "message", 00024 "debug" 00025 }; 00026 00027 private static String[] kinds = { 00028 "default", 00029 "race", 00030 "lock-order", 00031 "depend", 00032 "distributed", 00033 "search", 00034 "trace" 00035 }; 00036 00037 public static int getDebugLevel() { 00038 return enabled[DEFAULT]; 00039 } 00040 public static int getDebugLevel(int k) { 00041 return enabled[k]; 00042 } 00043 public static int getDebugLevel(String ks) { 00044 int k = mapKind(ks); 00045 if(k == -1) throw new IllegalArgumentException(ks + " is not a valid kind"); 00046 00047 return enabled[k]; 00048 } 00049 public static int mapKind(String ks) { 00050 for(int k = 0; k < LAST_KIND; k++) 00051 if(ks.equals(kinds[k])) return k; 00052 return -1; 00053 } 00054 public static int mapLevel(String ls) { 00055 for(int l = 0; l < LAST_LEVEL; l++) 00056 if(ls.equals(levels[l])) return l; 00057 return -1; 00058 } 00059 public static void print(int l, int k, Object o) { 00060 if(l <= enabled[k]) 00061 System.err.print(o); 00062 } 00063 public static void print(int l, int k, String s) { 00064 if(l <= enabled[k]) 00065 System.err.print(s); 00066 } 00067 public static void print(int l, Object o) { 00068 if(l <= enabled[DEFAULT]) 00069 System.err.print(o); 00070 } 00071 public static void print(int l, String s) { 00072 if(l <= enabled[DEFAULT]) 00073 System.err.print(s); 00074 } 00075 public static void println(int l) { 00076 if(l <= enabled[DEFAULT]) 00077 System.err.println(); 00078 } 00079 public static void println(int l, int k) { 00080 if(l <= enabled[k]) 00081 System.err.println(); 00082 } 00083 public static void println(int l, int k, Object o) { 00084 if(l <= enabled[k]) 00085 System.err.println(o); 00086 } 00087 public static void println(int l, int k, String s) { 00088 if(l <= enabled[k]) 00089 System.err.println(s); 00090 } 00091 public static void println(int l, Object o) { 00092 if(l <= enabled[DEFAULT]) 00093 System.err.println(o); 00094 } 00095 public static void println(int l, String s) { 00096 if(l <= enabled[DEFAULT]) 00097 System.err.println(s); 00098 } 00099 public static void setDebugLevel(int l) { 00100 if(l < 0 || l >= LAST_LEVEL) throw new IllegalArgumentException("0 <= level < " + LAST_LEVEL); 00101 00102 enabled[DEFAULT] = l; 00103 } 00104 public static void setDebugLevel(int l, int k) { 00105 if(l < 0 || l >= LAST_LEVEL) throw new IllegalArgumentException("0 <= level < " + LAST_LEVEL); 00106 if(k < 0 || k >= LAST_KIND) throw new IllegalArgumentException("0 <= kind < " + LAST_KIND); 00107 00108 enabled[k] = l; 00109 } 00110 public static void setDebugLevel(int l, String ks) { 00111 if(l < 0 || l >= LAST_LEVEL) throw new IllegalArgumentException("0 <= level < " + LAST_LEVEL); 00112 00113 int k = mapKind(ks); 00114 if(k == -1) throw new IllegalArgumentException(ks + " is not a valid kind"); 00115 00116 enabled[k] = l; 00117 } 00118 public static void setDebugLevel(String ls) { 00119 int l = mapLevel(ls); 00120 if(l == -1) throw new IllegalArgumentException(ls + " is not a valid level"); 00121 00122 enabled[DEFAULT] = l; 00123 } 00124 public static void setDebugLevel(String ls, int k) { 00125 if(k < 0 || k >= LAST_KIND) throw new IllegalArgumentException("0 <= kind < " + LAST_KIND); 00126 00127 int l = mapLevel(ls); 00128 if(l == -1) throw new IllegalArgumentException(ls + " is not a valid level"); 00129 00130 enabled[k] = l; 00131 } 00132 public static void setDebugLevel(String ls, String ks) { 00133 int l = mapLevel(ls); 00134 if(l == -1) throw new IllegalArgumentException(ls + " is not a valid level"); 00135 00136 int k = mapKind(ks); 00137 if(k == -1) throw new IllegalArgumentException(ks + " is not a valid kind"); 00138 00139 enabled[k] = l; 00140 } 00141 public static String status() { 00142 StringBuffer sb = new StringBuffer(); 00143 for(int k = 1; k < LAST_KIND; k++) { 00144 int l = enabled[k]; 00145 00146 if(l != ERROR) { 00147 if(sb.length() != 0) sb.append(","); 00148 sb.append(kinds[k]); 00149 sb.append("="); 00150 sb.append(levels[l]); 00151 } 00152 } 00153 00154 return sb.toString(); 00155 } 00156 }