-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Phillip Kruger <phillip.kruger@gmail.com>
- Loading branch information
1 parent
a02d772
commit 8a1d22b
Showing
23 changed files
with
447 additions
and
398 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingDecorateBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.deployment.logging; | ||
|
||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.CompositeIndex; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
|
||
/** | ||
* Contains information to decorate the Log output. Can be used by extensions that output the log / stacktraces, | ||
* for example the error page. | ||
* | ||
* Also see io.quarkus.runtime.logging.DecorateStackUtil to assist with the decoration | ||
*/ | ||
public final class LoggingDecorateBuildItem extends SimpleBuildItem { | ||
private final Path srcMainJava; | ||
private final CompositeIndex knowClassesIndex; | ||
|
||
public LoggingDecorateBuildItem(Path srcMainJava, CompositeIndex knowClassesIndex) { | ||
this.srcMainJava = srcMainJava; | ||
this.knowClassesIndex = knowClassesIndex; | ||
} | ||
|
||
public Path getSrcMainJava() { | ||
return srcMainJava; | ||
} | ||
|
||
public CompositeIndex getKnowClassesIndex() { | ||
return knowClassesIndex; | ||
} | ||
|
||
public List<String> getKnowClasses() { | ||
List<String> knowClasses = new ArrayList<>(); | ||
Collection<ClassInfo> knownClasses = knowClassesIndex.getKnownClasses(); | ||
for (ClassInfo ci : knownClasses) { | ||
knowClasses.add(ci.name().toString()); | ||
} | ||
return knowClasses; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/runtime/src/main/java/io/quarkus/runtime/logging/DecorateStackUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.quarkus.runtime.logging; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Deque; | ||
import java.util.List; | ||
|
||
public class DecorateStackUtil { | ||
|
||
public static String getDecoratedString(final Throwable throwable, String srcMainJava, List<String> knowClasses) { | ||
if (srcMainJava != null) { | ||
return DecorateStackUtil.getDecoratedString(throwable, Path.of(srcMainJava), knowClasses); | ||
} | ||
return null; | ||
} | ||
|
||
public static String getDecoratedString(final Throwable throwable, Path srcMainJava, List<String> knowClasses) { | ||
if (knowClasses != null && !knowClasses.isEmpty() && throwable != null) { | ||
StackTraceElement[] stackTrace = throwable.getStackTrace(); | ||
for (int i = 0; i < stackTrace.length; ++i) { | ||
StackTraceElement elem = stackTrace[i]; | ||
if (knowClasses.contains(elem.getClassName())) { | ||
String decoratedString = DecorateStackUtil.getDecoratedString(srcMainJava, elem); | ||
if (decoratedString != null) { | ||
return decoratedString; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static String getDecoratedString(Path srcMainJava, StackTraceElement stackTraceElement) { | ||
int lineNumber = stackTraceElement.getLineNumber(); | ||
if (lineNumber > 0 && srcMainJava != null) { | ||
String fullJavaFileName = getFullPath(stackTraceElement.getClassName(), stackTraceElement.getFileName()); | ||
Path f = srcMainJava.resolve(fullJavaFileName); | ||
try { | ||
List<String> contextLines = DecorateStackUtil.getRelatedLinesInSource(f, lineNumber, 2); | ||
String header = "Exception in " + stackTraceElement.getFileName() + ":" + stackTraceElement.getLineNumber(); | ||
return header + "\n" + String.join("\n", contextLines); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static List<String> getRelatedLinesInSource(Path filePath, int lineNumber, int contextRange) throws IOException { | ||
List<String> resultLines = new ArrayList<>(); | ||
Deque<String> contextQueue = new ArrayDeque<>(2 * contextRange + 1); | ||
try (BufferedReader reader = Files.newBufferedReader(filePath)) { | ||
String line; | ||
int currentLine = 1; | ||
while ((line = reader.readLine()) != null) { | ||
if (currentLine >= lineNumber - contextRange) { | ||
String ln = String.valueOf(currentLine); | ||
if (currentLine == lineNumber) { | ||
ln = "-> " + ln + " "; | ||
} else { | ||
ln = " " + ln + " "; | ||
} | ||
|
||
contextQueue.add("\t" + ln + line); | ||
} | ||
if (currentLine >= lineNumber + contextRange) { | ||
break; | ||
} | ||
currentLine++; | ||
} | ||
resultLines.addAll(contextQueue); | ||
} | ||
return resultLines; | ||
} | ||
|
||
private static String getFullPath(String fullClassName, String fileName) { | ||
int lastDotIndex = fullClassName.lastIndexOf("."); | ||
String packageName = fullClassName.substring(0, lastDotIndex); | ||
String path = packageName.replace('.', '/'); | ||
return path + "/" + fileName; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/runtime/src/main/resources/META-INF/template-html-builder.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.