Skip to content

Commit

Permalink
Make StackTraces less ass.
Browse files Browse the repository at this point in the history
  • Loading branch information
LexManos committed Oct 4, 2024
1 parent c445ce5 commit c712cba
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void addPluginCustomAuditTrail(String clazz, ILaunchPluginService plugin,
}

public void addPluginAuditTrail(String clazz, ILaunchPluginService plugin, ILaunchPluginService.Phase phase) {
getTransformerActivities(clazz).add(new TransformerActivity(ITransformerActivity.Type.PLUGIN, plugin.name(), phase.name().substring(0,1)));
getTransformerActivities(clazz).add(new TransformerActivity(ITransformerActivity.Type.PLUGIN, plugin.name(), phase.name()));
}

public void addTransformerAuditTrail(String clazz, ITransformationService transformService, ITransformer<?> transformer) {
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/cpw/mods/modlauncher/log/ExtraDataTextRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@

package cpw.mods.modlauncher.log;

import cpw.mods.modlauncher.Launcher;
import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.ITransformerAuditTrail;
import org.apache.logging.log4j.core.pattern.TextRenderer;

import java.util.Optional;

@Deprecated(forRemoval = true, since = "10.2.2")
public class ExtraDataTextRenderer implements TextRenderer {
private final TextRenderer wrapped;
private final Optional<ITransformerAuditTrail> auditData;
private final ITransformerAuditTrail trail;
private final ThreadLocal<TransformerContext> currentClass = new ThreadLocal<>();

ExtraDataTextRenderer(final TextRenderer wrapped) {
ExtraDataTextRenderer(final TextRenderer wrapped, ITransformerAuditTrail trail) {
this.wrapped = wrapped;
this.auditData = Optional.ofNullable(Launcher.INSTANCE).
map(Launcher::environment).
flatMap(env -> env.getProperty(IEnvironment.Keys.AUDITTRAIL.get()));
this.trail = trail;
}

@Override
Expand All @@ -38,8 +33,8 @@ public void render(final String input, final StringBuilder output, final String
final TransformerContext classContext = currentClass.get();
currentClass.remove();
if (classContext != null) {
final Optional<String> auditLine = auditData.map(data -> data.getAuditString(classContext.getClassName()));
wrapped.render(" {"+ auditLine.orElse("") +"}", output, "StackTraceElement.Transformers");
final String auditLine = trail == null ? "" : trail.getAuditString(classContext.getClassName());
wrapped.render(" {" + auditLine + "}", output, "StackTraceElement.Transformers");
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.apache.logging.log4j.core.pattern.*;

import cpw.mods.modlauncher.Launcher;
import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.ITransformerAuditTrail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;

/**
* Started as a copy of {@link org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter} because
Expand All @@ -21,7 +30,9 @@
@Plugin(name = "TransformingThrowablePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({ "tEx" })
public class TransformingThrowablePatternConverter extends ThrowablePatternConverter {
static final String SUFFIXFLAG="☃☃☃☃☃SUFFIXFLAG☃☃☃☃☃";
// Logger needs a suffix to trigger our 'enhancement' so pass in something that isn't empty.
@SuppressWarnings("unused")
private static final String SUFFIXFLAG = "{}";
/**
* @param name Name of converter.
* @param style CSS style for output.
Expand All @@ -36,22 +47,75 @@ protected TransformingThrowablePatternConverter(final Configuration config, fina
* {@inheritDoc}
*/
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
final ThrowableProxy proxy = event.getThrownProxy();
final Throwable throwable = event.getThrown();
if ((throwable != null || proxy != null) && options.anyLines()) {
if (proxy == null) {
super.format(event, toAppendTo);
return;
}
final int len = toAppendTo.length();
if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) {
toAppendTo.append(' ');
}
final TextRenderer textRenderer = new ExtraDataTextRenderer(options.getTextRenderer());
proxy.formatExtendedStackTraceTo(toAppendTo, options.getIgnorePackages(),
textRenderer, SUFFIXFLAG, options.getSeparator());
public void format(@SuppressWarnings("exports") final LogEvent event, final StringBuilder buf) {
var proxy = event.getThrownProxy();
if (!options.anyLines())
return;

if (proxy == null) {
super.format(event, buf);
return;
}

var len = buf.length();
if (len > 0 && !Character.isWhitespace(buf.charAt(len - 1)))
buf.append(' ');

var trail = Optional.ofNullable(Launcher.INSTANCE)
.map(Launcher::environment)
.flatMap(env -> env.getProperty(IEnvironment.Keys.AUDITTRAIL.get()))
.orElse(null);

var nl = options.getSeparator();
var renderer = options.getTextRenderer();
var suffix = "";
//renderer = new ExtraDataTextRenderer(renderer, trail);
//suffix = SUFFIXFLAG;
proxy.formatExtendedStackTraceTo(buf, options.getIgnorePackages(), renderer, suffix, nl);

if (trail == null)
return;

final Map<String, List<String>> audit = new TreeMap<>();
buildAuditMap(trail, audit, proxy);

line(buf, "Transformer Audit:");
for (var cls : audit.keySet()) {
line(buf, " ", cls);
for (var line : audit.get(cls))
line(buf, " ", line);
}
System.currentTimeMillis();
}

private void line(StringBuilder sb, String... parts) {
var renderer = options.getTextRenderer();
for (var pt : parts)
renderer.render(pt, sb, "Text");
renderer.render(options.getSeparator(), sb, "Text");
}

private static void buildAuditMap(ITransformerAuditTrail trail, Map<String, List<String>> map, ThrowableProxy proxy) {
if (proxy == null)
return;

for (var element : proxy.getExtendedStackTrace()) {
var cls = element.getClassName();
if (map.containsKey(cls))
continue;

var lines = new ArrayList<String>();
for (var activity : trail.getActivityFor(cls))
lines.add(activity.getType().name() + ": " + String.join(":", activity.getContext()));

if (!lines.isEmpty())
map.put(cls, lines);
}

for (final ThrowableProxy suppressed : proxy.getSuppressedProxies())
buildAuditMap(trail, map, suppressed);

buildAuditMap(trail, map, proxy.getCauseProxy());
}

/**
Expand All @@ -62,17 +126,39 @@ public void format(final LogEvent event, final StringBuilder toAppendTo) {
* only the first line of the throwable will be formatted.
* @return instance of class.
*/
public static TransformingThrowablePatternConverter newInstance(final Configuration config, final String[] options) {
public static TransformingThrowablePatternConverter newInstance(@SuppressWarnings("exports") final Configuration config, final String[] options) {
return new TransformingThrowablePatternConverter(config, options);
}


@Deprecated(forRemoval = true, since = "10.2.2")
public static String generateEnhancedStackTrace(final Throwable throwable) {
final ThrowableProxy proxy = new ThrowableProxy(throwable);
final StringBuilder buffer = new StringBuilder();
final TextRenderer textRenderer = new ExtraDataTextRenderer(PlainTextRenderer.getInstance());
proxy.formatExtendedStackTraceTo(buffer, Collections.emptyList(),
textRenderer, SUFFIXFLAG, Strings.LINE_SEPARATOR);
return buffer.toString();
var proxy = new ThrowableProxy(throwable);
var buf = new StringBuilder();

var trail = Optional.ofNullable(Launcher.INSTANCE)
.map(Launcher::environment)
.flatMap(env -> env.getProperty(IEnvironment.Keys.AUDITTRAIL.get()))
.orElse(null);

var nl = Strings.LINE_SEPARATOR;
var renderer = PlainTextRenderer.getInstance();
var suffix = "";
//renderer = new ExtraDataTextRenderer(renderer, trail);
//suffix = SUFFIXFLAG;
proxy.formatExtendedStackTraceTo(buf, Collections.emptyList(), renderer, suffix, nl);

if (trail != null) {
final Map<String, List<String>> audit = new TreeMap<>();
buildAuditMap(trail, audit, proxy);

buf.append("Transformer Audit:").append(nl);
for (var cls : audit.keySet()) {
buf.append(" ").append(cls).append(nl);
for (var line : audit.get(cls))
buf.append(" ").append(line).append(nl);
}
}

return buf.toString();
}
}

0 comments on commit c712cba

Please sign in to comment.