From 780d59d8a78273f6d663ff8940d04f6d15b3c1ba Mon Sep 17 00:00:00 2001 From: asofold Date: Tue, 11 Nov 2014 20:25:28 +0100 Subject: [PATCH] Use a utility method to get a String of a Throwable. --- .../nocheatplus/utilities/StringUtil.java | 31 +++++++++++++++++++ .../nocheatplus/logging/LogUtil.java | 5 +-- .../nocheatplus/logging/StaticLogFile.java | 6 ++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/StringUtil.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/StringUtil.java index fcacfaf4..05d7a7f6 100644 --- a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/StringUtil.java +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/StringUtil.java @@ -164,5 +164,36 @@ public class StringUtil { return p[n]; } + + /** + * Just return the stack trace as new-line-separated string. + * @param t + * @param header Add a header about the exception itself, if set to true. + * @return + */ + public static final String stackTraceToString(final Throwable t, final boolean header) { + // TODO: Consider to use System.getProperty("line.separator"). + final StringBuilder b = new StringBuilder(325); + if (header) { + b.append(t.toString()); // TODO: Check. + b.append("\n"); + } + final StackTraceElement[] elements = t.getStackTrace(); + for (int i = 0; i < elements.length; i++) { + b.append(elements[i]); + b.append("\n"); + } + return b.toString(); + } + + /** + * Convenience method for stackTraceToString(t). + * @param t + * @return + */ + public static final String throwableToString(final Throwable t) { + return stackTraceToString(t, true); + } + } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/LogUtil.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/LogUtil.java index 627ffa32..378ad211 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/LogUtil.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/LogUtil.java @@ -28,10 +28,7 @@ public class LogUtil { } public static String toString(final Throwable t){ - // TODO: Find the fastest way. - final StringWriter w = new StringWriter(); - t.printStackTrace(new PrintWriter(w)); - return w.toString(); + return StringUtil.throwableToString(t); } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/StaticLogFile.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/StaticLogFile.java index 8a2bcf5d..8a1007e1 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/StaticLogFile.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/logging/StaticLogFile.java @@ -11,6 +11,8 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import fr.neatmonster.nocheatplus.utilities.StringUtil; + /** * Could not think of anything better, likely a refactoring stage. * @author mc_dev @@ -58,9 +60,7 @@ public class StaticLogFile { builder.append('\n'); if (ex != null) { - final StringWriter writer = new StringWriter(); - ex.printStackTrace(new PrintWriter(writer)); - builder.append(writer); + builder.append(StringUtil.throwableToString(ex)); } return builder.toString();