From 477290cb92442e57f116d366f0ee74c4728271d4 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 1 Aug 2002 20:15:43 +0000 Subject: [PATCH] Added a stackTrace method. Possibly needs renaming. It's in common use in other Jakarta projects such as Ant. Turns a Throwable into a String. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@136952 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 29eab4597..bef2fb1fd 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -60,6 +60,7 @@ import java.io.OutputStreamWriter; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.StringWriter; import java.io.IOException; import java.util.NoSuchElementException; import java.util.StringTokenizer; @@ -80,7 +81,7 @@ * @author Ed Korthof * @author Stephen Colebourne - * @version $Id: StringUtils.java,v 1.5 2002/07/26 01:40:11 dlr Exp $ + * @version $Id: StringUtils.java,v 1.6 2002/08/01 20:15:43 bayard Exp $ */ public class StringUtils { @@ -1566,6 +1567,19 @@ public static int getLevenshteinDistance(String s, String t) { return d[n][m]; } + /** + * Convenient method to retrieve the full stacktrace from a given exception. * @param t the exception to get the stacktrace from. + * @return the stacktrace from the given exception. + */ + public static String getStackTrace(Throwable t) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw, true); + t.printStackTrace(pw); + pw.flush(); + pw.close(); + return sw.toString(); + } + // these are not really of use in the Java world. Only if you're a C afficionado // public static String sprintf(String format, Object[] list); // public static Object[] sscanf(String str, String format);