15
15
*/
16
16
package io .reactiverse .es4x .jul ;
17
17
18
+ import io .reactiverse .es4x .sourcemap .SourceMap ;
19
+ import io .vertx .core .buffer .Buffer ;
20
+ import io .vertx .core .json .Json ;
21
+ import io .vertx .core .json .JsonObject ;
22
+ import org .graalvm .polyglot .PolyglotException ;
23
+ import org .graalvm .polyglot .SourceSection ;
24
+
18
25
import java .io .IOException ;
19
26
import java .io .PrintWriter ;
20
27
import java .io .StringWriter ;
28
+ import java .net .URI ;
29
+ import java .nio .file .Files ;
30
+ import java .nio .file .Paths ;
31
+ import java .util .Collections ;
32
+ import java .util .IdentityHashMap ;
33
+ import java .util .Set ;
21
34
import java .util .logging .*;
22
35
23
36
import static java .util .logging .Level .*;
@@ -26,6 +39,7 @@ public class ANSIFormatter extends Formatter {
26
39
27
40
// are ANSI colors allowed?
28
41
private static final boolean colors ;
42
+ // private static final SourceMap sourceMap;
29
43
30
44
static {
31
45
if (Boolean .getBoolean ("noTTY" )) {
@@ -48,6 +62,14 @@ public class ANSIFormatter extends Formatter {
48
62
colors = System .console () != null ;
49
63
}
50
64
}
65
+ //
66
+ // // handle source maps
67
+ // String sourceMapFile = System.getProperty("sourceMap");
68
+ // if (sourceMapFile != null) {
69
+ // sourceMap = new SourceMap(Buffer.buffer(Files.readAllBytes(Paths.get(sourceMapFile))));
70
+ // } else {
71
+ // sourceMap = null;
72
+ // }
51
73
}
52
74
53
75
@ Override
@@ -151,4 +173,104 @@ private static String suffix(Level l) {
151
173
152
174
return "" ;
153
175
}
176
+
177
+
178
+ private static final String CAUSE_CAPTION = "Caused by: " ;
179
+ private static final String SUPPRESSED_CAPTION = "Suppressed: " ;
180
+
181
+ public static void printStackTrace (Throwable self , PrintWriter s ) {
182
+ // Guard against malicious overrides of Throwable.equals by
183
+ // using a Set with identity equality semantics.
184
+ Set <Throwable > dejaVu = Collections .newSetFromMap (new IdentityHashMap <>());
185
+ dejaVu .add (self );
186
+
187
+ // Print our stack trace
188
+ s .println (self );
189
+ StackTraceElement [] trace = self .getStackTrace ();
190
+ printTrace (self , trace , trace .length , s );
191
+
192
+ // Print suppressed exceptions, if any
193
+ for (Throwable se : self .getSuppressed ())
194
+ printEnclosedStackTrace (se , s , trace , SUPPRESSED_CAPTION , "\t " , dejaVu );
195
+
196
+ // Print cause, if any
197
+ Throwable ourCause = self .getCause ();
198
+ if (ourCause != null )
199
+ printEnclosedStackTrace (ourCause , s , trace , CAUSE_CAPTION , "" , dejaVu );
200
+ }
201
+
202
+ private static void printEnclosedStackTrace (
203
+ Throwable self ,
204
+ PrintWriter s ,
205
+ StackTraceElement [] enclosingTrace ,
206
+ String caption ,
207
+ String prefix ,
208
+ Set <Throwable > dejaVu ) {
209
+
210
+ if (dejaVu .contains (self )) {
211
+ s .println (prefix + caption + "[CIRCULAR REFERENCE: " + self + "]" );
212
+ } else {
213
+ dejaVu .add (self );
214
+ // Compute number of frames in common between this and enclosing trace
215
+ StackTraceElement [] trace = self .getStackTrace ();
216
+ int m = trace .length - 1 ;
217
+ int n = enclosingTrace .length - 1 ;
218
+ while (m >= 0 && n >= 0 && trace [m ].equals (enclosingTrace [n ])) {
219
+ m --;
220
+ n --;
221
+ }
222
+ int framesInCommon = trace .length - 1 - m ;
223
+
224
+ // Print our stack trace
225
+ s .println (prefix + caption + self );
226
+ printTrace (self , trace , m , s );
227
+
228
+ if (framesInCommon != 0 )
229
+ s .println (prefix + "\t ... " + framesInCommon + " more" );
230
+
231
+ // Print suppressed exceptions, if any
232
+ for (Throwable se : self .getSuppressed ())
233
+ printEnclosedStackTrace (se , s , trace , SUPPRESSED_CAPTION ,
234
+ prefix + "\t " , dejaVu );
235
+
236
+ // Print cause, if any
237
+ Throwable ourCause = self .getCause ();
238
+ if (ourCause != null )
239
+ printEnclosedStackTrace (ourCause , s , trace , CAUSE_CAPTION , prefix , dejaVu );
240
+ }
241
+ }
242
+
243
+ private static void printTrace (Throwable self , StackTraceElement [] trace , int limit , PrintWriter s ) {
244
+ if (self instanceof PolyglotException ) {
245
+ int i = 0 ;
246
+ for (PolyglotException .StackFrame stackFrame : ((PolyglotException ) self ).getPolyglotStackTrace ()) {
247
+ if (i ++ == limit ) {
248
+ break ;
249
+ }
250
+ if (stackFrame .isHostFrame ()) {
251
+ s .println ("\t at " + stackFrame );
252
+ } else {
253
+ SourceSection sourceSection = stackFrame .getSourceLocation ();
254
+ if (sourceSection != null ) {
255
+ URI uri = sourceSection .getSource ().getURI ();
256
+
257
+ // TODO: rewrite with sourcemap is available
258
+
259
+ s .println ("\t at <js> " + stackFrame .getRootName ()
260
+ + "(" +
261
+ ("file" .equals (uri .getScheme ()) ? uri .getPath () : uri ) +
262
+ (sourceSection .hasLines () ? ":" + sourceSection .getStartLine () : "" ) +
263
+ (sourceSection .hasColumns () ? ":" + sourceSection .getStartColumn () : "" ) +
264
+ ")" );
265
+ } else {
266
+ s .println ("\t at " + stackFrame );
267
+ }
268
+ }
269
+ }
270
+ } else {
271
+ for (int i = 0 ; i <= limit ; i ++) {
272
+ s .println ("\t at " + trace [i ]);
273
+ }
274
+ }
275
+ }
154
276
}
0 commit comments