diff --git a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java index d26956f7..3da693ad 100644 --- a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java +++ b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java @@ -26,12 +26,14 @@ package org.spongepowered.asm.util; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.google.common.base.Strings; @@ -168,11 +170,22 @@ public class PrettyPrinter { this.width = width; } + /** + * Set the wrap width (default 80 columns) + * + * @param wrapWidth new width (in characters) to wrap to + * @return fluent interface + */ public PrettyPrinter wrapTo(int wrapWidth) { this.wrapWidth = wrapWidth; return this; } + /** + * Get the current wrap width + * + * @return the current wrap width + */ public int wrapTo() { return this.wrapWidth; } @@ -186,6 +199,18 @@ public class PrettyPrinter { this.lines.add(""); return this; } + + /** + * Adds a string line to the output + * + * @param string format string + * @return fluent interface + */ + public PrettyPrinter add(String string) { + this.lines.add(string); + this.width = Math.max(this.width, string.length()); + return this; + } /** * Adds a formatted line to the output @@ -227,6 +252,39 @@ public class PrettyPrinter { return this; } + /** + * Add elements of the array to the output, one per line, with array indices + * + * @param array Array of objects to print + * @return fluent interface + */ + public PrettyPrinter addIndexed(Object[] array) { + int indexWidth = String.valueOf(array.length - 1).length(); + String format = "[%" + indexWidth + "d] %s"; + for (int index = 0; index < array.length; index++) { + this.add(format, index, array[index]); + } + + return this; + } + + /** + * Add elements of the collection to the output, one per line, with indices + * + * @param c Collection of objects to print + * @return fluent interface + */ + public PrettyPrinter addWithIndices(Collection c) { + return this.addIndexed(c.toArray(new Object[0])); + } + + /** + * Adds a pretty-printable object to the output, the object is responsible + * for adding its own representation to this printer + * + * @param printable object to add + * @return fluent interface + */ public PrettyPrinter add(IPrettyPrintable printable) { if (printable != null) { printable.print(this); @@ -262,6 +320,58 @@ public class PrettyPrinter { return this; } + /** + * Adds the specified object to the output + * + * @param object object to add + * @return fluent interface + */ + public PrettyPrinter add(Object object) { + return this.add(object, 0); + } + + /** + * Adds the specified object to the output + * + * @param object object to add + * @param indent indent amount + * @return fluent interface + */ + public PrettyPrinter add(Object object, int indent) { + String margin = Strings.repeat(" ", indent); + return this.append(object, indent, margin); + } + + private PrettyPrinter append(Object object, int indent, String margin) { + if (object instanceof String) { + return this.add("%s%s", margin, object); + } else if (object instanceof Iterable) { + for (Object entry : (Iterable)object) { + this.append(entry, indent, margin); + } + return this; + } else if (object instanceof Map) { + this.kvWidth(indent); + return this.add((Map)object); + } else if (object instanceof IPrettyPrintable) { + return this.add((IPrettyPrintable)object); + } else if (object instanceof Throwable) { + return this.add((Throwable)object, indent); + } else if (object.getClass().isArray()) { + return this.add((Object[])object, indent + "%s"); + } + return this.add("%s%s", margin, object); + } + + /** + * Adds a formatted line to the output, and attempts to wrap the line + * content to the current wrap width + * + * @param format format string + * @param args arguments + * + * @return fluent interface + */ public PrettyPrinter addWrapped(String format, Object... args) { return this.addWrapped(this.wrapWidth, format, args); } @@ -402,13 +512,165 @@ public class PrettyPrinter { } return this; } + + /** + * Outputs this printer to stderr and to a logger decorated with the calling + * class name with level {@link Level#DEBUG} + * + * @return fluent interface + */ + public PrettyPrinter trace() { + return this.trace(PrettyPrinter.getDefaultLoggerName()); + } + /** + * Outputs this printer to stderr and to a logger decorated with the calling + * class name at the specified level + * + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(Level level) { + return this.trace(PrettyPrinter.getDefaultLoggerName(), level); + } + + /** + * Outputs this printer to stderr and to a logger decorated with specified + * name with level {@link Level#DEBUG} + * + * @param logger Logger name to write to + * @return fluent interface + */ + public PrettyPrinter trace(String logger) { + return this.trace(System.err, LogManager.getLogger(logger)); + } + + /** + * Outputs this printer to stderr and to a logger decorated with specified + * name with the specified level + * + * @param logger Logger name to write to + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(String logger, Level level) { + return this.trace(System.err, LogManager.getLogger(logger), level); + } + + /** + * Outputs this printer to stderr and to the supplied logger with level + * {@link Level#DEBUG} + * + * @param logger Logger to write to + * @return fluent interface + */ + public PrettyPrinter trace(Logger logger) { + return this.trace(System.err, logger); + } + + /** + * Outputs this printer to stderr and to the supplied logger with the + * specified level + * + * @param logger Logger to write to + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(Logger logger, Level level) { + return this.trace(System.err, logger, level); + } + + /** + * Outputs this printer to the specified stream and to a logger decorated + * with the calling class name with level {@link Level#DEBUG} + * + * @param stream Output stream to print to + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream) { + return this.trace(stream, PrettyPrinter.getDefaultLoggerName()); + } + + /** + * Outputs this printer to the specified stream and to a logger decorated + * with the calling class name with the specified level + * + * @param stream Output stream to print to + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream, Level level) { + return this.trace(stream, PrettyPrinter.getDefaultLoggerName(), level); + } + + /** + * Outputs this printer to the specified stream and to a logger with the + * specified name with level {@link Level#DEBUG} + * + * @param stream Output stream to print to + * @param logger Logger name to write to + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream, String logger) { + return this.trace(stream, LogManager.getLogger(logger)); + } + + /** + * Outputs this printer to the specified stream and to a logger with the + * specified name at the specified level + * + * @param stream Output stream to print to + * @param logger Logger name to write to + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream, String logger, Level level) { + return this.trace(stream, LogManager.getLogger(logger), level); + } + + /** + * Outputs this printer to the specified stream and to the supplied logger + * with level {@link Level#DEBUG} + * + * @param stream Output stream to print to + * @param logger Logger to write to + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream, Logger logger) { + return this.trace(stream, logger, Level.DEBUG); + } + + /** + * Outputs this printer to the specified stream and to the supplied logger + * with at the specified level + * + * @param stream Output stream to print to + * @param logger Logger to write to + * @param level Log level to write messages + * @return fluent interface + */ + public PrettyPrinter trace(PrintStream stream, Logger logger, Level level) { + this.log(logger, level); + this.print(stream); + return this; + } + + /** + * Print this printer to stderr + * + * @return fluent interface + */ + public PrettyPrinter print() { + return this.print(System.err); + } + /** * Print this printer to the specified output * * @param stream stream to print to + * @return fluent interface */ - public void print(PrintStream stream) { + public PrettyPrinter print(PrintStream stream) { this.updateWidth(); this.printSpecial(stream, this.horizontalRule); for (Object line : this.lines) { @@ -419,6 +681,7 @@ public class PrettyPrinter { } } this.printSpecial(stream, this.horizontalRule); + return this; } private void printSpecial(PrintStream stream, ISpecialEntry line) { @@ -429,8 +692,14 @@ public class PrettyPrinter { stream.printf("/* %-" + this.width + "s */\n", string); } - public void log(Logger logger) { - this.log(logger, Level.INFO); + /** + * Write this printer to the specified logger at {@link Level#INFO} + * + * @param logger logger to log to + * @return fluent interface + */ + public PrettyPrinter log(Logger logger) { + return this.log(logger, Level.INFO); } /** @@ -438,8 +707,9 @@ public class PrettyPrinter { * * @param logger logger to log to * @param level log level + * @return fluent interface */ - public void log(Logger logger, Level level) { + public PrettyPrinter log(Logger logger, Level level) { this.updateWidth(); this.logSpecial(logger, level, this.horizontalRule); for (Object line : this.lines) { @@ -450,10 +720,11 @@ public class PrettyPrinter { } } this.logSpecial(logger, level, this.horizontalRule); + return this; } private void logSpecial(Logger logger, Level level, ISpecialEntry line) { - logger.log(level, "/*{}*/\n", line.toString()); + logger.log(level, "/*{}*/", line.toString()); } private void logString(Logger logger, Level level, String line) { @@ -475,10 +746,25 @@ public class PrettyPrinter { return String.format("%%%ds : %%s", keyWidth); } + private static String getDefaultLoggerName() { + String name = new Throwable().getStackTrace()[2].getClassName(); + int pos = name.lastIndexOf('.'); + return pos == -1 ? name : name.substring(pos + 1); + } + + /** + * Convenience method, alternative to using Thread.dumpStack which + * prints to stderr in pretty-printed format. + */ public static void dumpStack() { new PrettyPrinter().add(new Exception("Stack trace")).print(System.err); } + /** + * Convenience methods, pretty-prints the specified throwable to stderr + * + * @param th Throwable to log + */ public static void print(Throwable th) { new PrettyPrinter().add(th).print(System.err); }