1 30 31 package rcm.util; 32 33 import java.lang.reflect.*; 34 import java.io.PrintStream ; 35 36 public abstract class Debug { 37 public static final Debug VERBOSE = new Verbose (); 38 public static final Debug QUIET = new Quiet (); 39 public static final Debug NONE = new NoDebug (); 40 41 public static Debug getDebugLevel (Class cls) 42 throws NoSuchFieldException { 43 try { 44 Field fld = cls.getField ("debug"); 45 if (fld.getType () != Debug.class 46 || !Modifier.isStatic (fld.getModifiers ())) 47 throw new NoSuchFieldException (); 48 return (Debug) fld.get (null); 49 } catch (IllegalArgumentException e) { 50 throw new NoSuchFieldException (); 51 } catch (IllegalAccessException e) { 52 throw new NoSuchFieldException (); 53 } catch (SecurityException e) { 54 throw new NoSuchFieldException (); 55 } 56 } 57 58 public static void setDebugLevel (Class cls, Debug level) 59 throws NoSuchFieldException { 60 try { 61 Field fld = cls.getField ("debug"); 62 if (fld.getType () != Debug.class 63 || !Modifier.isStatic (fld.getModifiers ())) 64 throw new NoSuchFieldException (); 65 fld.set (null, level); 66 } catch (IllegalArgumentException e) { 67 throw new NoSuchFieldException (); 68 } catch (IllegalAccessException e) { 69 throw new NoSuchFieldException (); 70 } catch (SecurityException e) { 71 throw new NoSuchFieldException (); 72 } 73 } 74 75 public abstract boolean isEnabled (); 76 public abstract void print (String message); 77 public abstract void println (String message); 78 public abstract void print (Object obj); 79 public abstract void println (Object obj); 80 public abstract void report (Throwable t); 81 public abstract void printThreadInfo (); 82 public abstract void printStackTrace (); 83 public abstract void assertion (boolean f); 84 85 public static class Verbose extends Debug { 86 protected PrintStream out; 87 88 public Verbose () { 89 this (System.err); 90 } 91 92 public Verbose (PrintStream out) { 93 this.out = out; 94 } 95 96 public boolean isEnabled () { 97 return true; 98 } 99 100 public void print (String message) { 101 out.print (message); 102 out.flush (); 103 } 104 105 public void println (String message) { 106 out.println (message); 107 out.flush (); 108 } 109 110 public void print (Object obj) { 111 print (obj.toString ()); 112 } 113 114 public void println (Object obj) { 115 println (obj.toString ()); 116 } 117 118 public void report (Throwable t) { 119 t.printStackTrace (out); 120 out.flush (); 121 } 122 123 public void printThreadInfo () { 124 ThreadGroup g = Thread.currentThread().getThreadGroup (); 125 Thread [] t = new Thread [g.activeCount ()]; 126 g.enumerate (t); 127 out.println ("Active threads in " + g); 128 for (int i=0; i<t.length; ++i) 129 out.println (t[i]); 130 out.flush (); 131 } 132 133 public void printStackTrace () { 134 try { 135 throw new Exception (); 136 } catch (Exception e) { 137 e.printStackTrace (out); 138 out.flush (); 139 } 140 } 141 142 public void assertion (boolean f) { 143 if (!f) 144 throw new RuntimeException ("assertion failure"); 145 } 146 } 147 148 public static class Quiet extends Verbose { 149 public Quiet () { 150 } 151 152 public Quiet (PrintStream out) { 153 super (out); 154 } 155 156 public boolean isEnabled () { 157 return false; 158 } 159 160 public void print (String message) { 161 } 162 public void println (String message) { 163 } 164 public void print (Object message) { 165 } 166 public void println (Object message) { 167 } 168 public void report (Throwable t) { 169 t.printStackTrace (out); 170 out.flush (); 171 } 172 public void printThreadInfo () { 173 } 174 public void printStackTrace () { 175 } 176 public void assertion (boolean f) { 177 if (!f) { 178 try { 179 throw new RuntimeException ("assertion failure"); 180 } catch (RuntimeException e) { 181 e.printStackTrace (out); 182 out.flush (); 183 } 184 } 185 } 186 } 187 188 public static class NoDebug extends Debug { 189 public boolean isEnabled () { 190 return false; 191 } 192 193 public void print (String message) { 194 } 195 public void println (String message) { 196 } 197 public void print (Object message) { 198 } 199 public void println (Object message) { 200 } 201 public void report (Throwable t) { 202 } 203 public void printThreadInfo () { 204 } 205 public void printStackTrace () { 206 } 207 public void assertion (boolean f) { 208 } 209 } 210 211 } 212 | Popular Tags |