|
17 | 17 | package org.panda_lang.language.interpreter.logging;
|
18 | 18 |
|
19 | 19 | import org.panda_lang.language.Failure;
|
| 20 | +import org.panda_lang.language.PandaFrameworkConstants; |
| 21 | +import org.panda_lang.language.interpreter.source.IndicatedSource; |
| 22 | +import org.panda_lang.language.interpreter.source.Location; |
| 23 | +import org.panda_lang.language.interpreter.token.Snippet; |
| 24 | +import org.panda_lang.language.interpreter.token.Snippetable; |
20 | 25 | import org.panda_lang.utilities.commons.StackTraceUtils;
|
| 26 | +import org.panda_lang.utilities.commons.StringUtils; |
| 27 | +import org.panda_lang.utilities.commons.console.Colored; |
21 | 28 | import org.panda_lang.utilities.commons.console.Effect;
|
22 | 29 |
|
23 | 30 | public final class SystemLogger implements Logger {
|
@@ -49,26 +56,72 @@ public void exception(Throwable throwable) {
|
49 | 56 | StackTraceElement[] stackTrace = StackTraceUtils.startsWith(throwable.getStackTrace(), element -> element.toString().contains("org.junit"));
|
50 | 57 |
|
51 | 58 | if (throwable instanceof Failure) {
|
| 59 | + Failure failure = (Failure) throwable; |
52 | 60 |
|
53 |
| - } |
| 61 | + error(""); |
| 62 | + error("&b- - ~ ~< Failure >~ ~ - -&r"); |
| 63 | + error(""); |
| 64 | + error("&1" + failure.getMessage()); |
| 65 | + error(""); |
54 | 66 |
|
55 |
| - error(""); |
56 |
| - error("&b- - ~ ~< Exception >~ ~ - -&r"); |
57 |
| - error(""); |
58 |
| - error("Given:"); |
59 |
| - error(" Message:&1 " + throwable.getMessage()); |
60 |
| - error(" In:&1 " + stackTrace[0].toString()); |
61 |
| - error(" By:&1 " + throwable.getClass()); |
62 |
| - error(""); |
63 |
| - error("Stacktrace:"); |
| 67 | + failure.getNote().peek(note -> { |
| 68 | + error("Note:"); |
| 69 | + error(" &1" + note); |
| 70 | + error(""); |
| 71 | + }); |
| 72 | + |
| 73 | + IndicatedSource indicatedSource = failure.getIndicatedSource(); |
| 74 | + Location location = indicatedSource.getIndicated().getLocation(); |
| 75 | + |
| 76 | + String source = getCurrentLine(indicatedSource.getSource(), indicatedSource.getIndicated()).toString(); |
| 77 | + String element = getCurrentLine(indicatedSource.getIndicated(), indicatedSource.getIndicated()).toString(); |
| 78 | + |
| 79 | + int elementIndex = source.indexOf(element); |
| 80 | + int endIndex = elementIndex + element.length(); |
64 | 81 |
|
65 |
| - for (StackTraceElement element : stackTrace) { |
66 |
| - error(" at " + element.toString()); |
| 82 | + String content = elementIndex < 0 ? source : source.substring(0, elementIndex) |
| 83 | + + Colored.on(source.substring(elementIndex, endIndex)).effect(Effect.RED) |
| 84 | + + source.substring(endIndex); |
| 85 | + |
| 86 | + |
| 87 | + error("Source:"); |
| 88 | + error(" " + content); |
| 89 | + error(" " + StringUtils.buildSpace(source.indexOf(element)) + Colored.on("^").effect(Effect.BOLD)); |
| 90 | + error("Location:"); |
| 91 | + error(" Panda: &b" + location.getSource().getId() + "&r at line &1" + location.getDisplayLine() + "&r:&1" + location.getIndex()); |
| 92 | + error(""); |
| 93 | + error("Stacktrace:"); |
| 94 | + |
| 95 | + for (int index = 0; index < 2 && index < stackTrace.length; index++) { |
| 96 | + StackTraceElement stackTraceElement = stackTrace[index]; |
| 97 | + error(" at " + stackTraceElement.toString()); |
| 98 | + } |
| 99 | + } |
| 100 | + else { |
| 101 | + error(""); |
| 102 | + error("&b- - ~ ~< Exception >~ ~ - -&r"); |
| 103 | + error(""); |
| 104 | + error("Given:"); |
| 105 | + error(" Message:&1 " + throwable.getMessage()); |
| 106 | + error(" In:&1 " + stackTrace[0].toString()); |
| 107 | + error(" By:&1 " + throwable.getClass()); |
| 108 | + error(""); |
| 109 | + error("Stacktrace:"); |
| 110 | + |
| 111 | + for (StackTraceElement stackTraceElement : stackTrace) { |
| 112 | + error(" at " + stackTraceElement); |
| 113 | + } |
67 | 114 | }
|
68 | 115 |
|
69 | 116 | error("");
|
70 | 117 | error("Environment:");
|
| 118 | + error(" Panda: " + PandaFrameworkConstants.VERSION); |
| 119 | + error(" Java: " + System.getProperty("java.version") + " (" + System.getProperty("os.name") + ")"); |
71 | 120 | error("");
|
72 | 121 | }
|
73 | 122 |
|
| 123 | + private Snippet getCurrentLine(Snippetable source, Snippetable indicated) { |
| 124 | + return source.toSnippet().getLine(indicated.toSnippet().getFirst().getLocation().getLine()); |
| 125 | + } |
| 126 | + |
74 | 127 | }
|
0 commit comments