1 46 47 48 package org.codehaus.groovy.tools; 49 50 import java.io.PrintStream ; 51 import java.io.PrintWriter ; 52 53 import org.codehaus.groovy.GroovyExceptionInterface; 54 import org.codehaus.groovy.control.CompilationFailedException; 55 56 57 58 65 66 public class ErrorReporter 67 { 68 private Throwable base = null; private boolean debug = false; 71 private Object output = null; 73 74 80 81 public ErrorReporter( Throwable e ) 82 { 83 this.base = e; 84 } 85 86 87 93 94 public ErrorReporter( Throwable e, boolean debug ) 95 { 96 this.base = e; 97 this.debug = debug; 98 } 99 100 101 104 105 public void write( PrintStream stream ) 106 { 107 this.output = stream; 108 dispatch( base, false ); 109 stream.flush(); 110 } 111 112 113 116 117 public void write( PrintWriter writer ) 118 { 119 this.output = writer; 120 dispatch( base, false ); 121 writer.flush(); 122 } 123 124 125 128 129 protected void dispatch( Throwable object, boolean child ) 130 { 131 if( object instanceof CompilationFailedException ) 132 { 133 report( (CompilationFailedException)object, child ); 134 } 135 else if( object instanceof GroovyExceptionInterface ) 136 { 137 report( (GroovyExceptionInterface)object, child ); 138 } 139 else if( object instanceof Exception ) 140 { 141 report( (Exception )object, child ); 142 } 143 else 144 { 145 report( object, child ); 146 } 147 148 } 149 150 151 152 155 156 159 160 protected void report( CompilationFailedException e, boolean child ) 161 { 162 println( e.toString() ); 163 stacktrace( e, false ); 164 } 165 166 167 168 171 172 protected void report( GroovyExceptionInterface e, boolean child ) 173 { 174 println( ((Exception )e).getMessage() ); 175 stacktrace( (Exception )e, false ); 176 } 177 178 179 180 183 184 protected void report( Exception e, boolean child ) 185 { 186 println( e.getMessage() ); 187 stacktrace( e, false ); 188 } 189 190 191 192 195 196 protected void report( Throwable e, boolean child ) 197 { 198 println( ">>> a serious error occurred: " + e.getMessage() ); 199 stacktrace( e, true ); 200 } 201 202 203 204 207 208 211 212 protected void println( String line ) 213 { 214 if( output instanceof PrintStream ) 215 { 216 ((PrintStream )output).println( line ); 217 } 218 else 219 { 220 ((PrintWriter )output).println( line ); 221 } 222 } 223 224 protected void println( StringBuffer line ) 225 { 226 if( output instanceof PrintStream ) 227 { 228 ((PrintStream )output).println( line ); 229 } 230 else 231 { 232 ((PrintWriter )output).println( line ); 233 } 234 } 235 236 237 241 242 protected void stacktrace( Throwable e, boolean always ) 243 { 244 if( debug || always ) 245 { 246 println( ">>> stacktrace:" ); 247 if( output instanceof PrintStream ) 248 { 249 e.printStackTrace( (PrintStream )output ); 250 } 251 else 252 { 253 e.printStackTrace( (PrintWriter )output ); 254 } 255 } 256 } 257 258 259 260 } 261 | Popular Tags |