From 2e25ae218416ab62c588ea17fe2aec4f8d033515 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Wed, 04 Dec 2013 09:12:41 +0000
Subject: [PATCH] Add two utility methods in StaticUtils class to handle stack traces: stackTraceToSingleLineString()
---
opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java | 93 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index 30f05e4..d71bc15 100644
--- a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -1818,6 +1818,99 @@
}
/**
+ * Retrieves a stack trace from the provided exception as a single-line
+ * string.
+ *
+ * @param throwable
+ * The exception for which to retrieve the stack trace.
+ * @param isDebug
+ * If {@code true}, provides the full stack trace, otherwise
+ * provides a limited extract of stack trace
+ * @return A stack trace from the provided exception as a single-line
+ * string.
+ */
+ public static String stackTraceToSingleLineString(Throwable throwable, boolean isDebug) {
+ StringBuilder buffer = new StringBuilder();
+ stackTraceToSingleLineString(buffer, throwable, isDebug);
+ return buffer.toString();
+ }
+
+
+ /**
+ * Appends a single-line string representation of the provided exception to
+ * the given buffer.
+ *
+ * @param buffer
+ * The buffer to which the information is to be appended.
+ * @param throwable
+ * The exception for which to retrieve the stack trace.
+ * @param isDebug
+ * If {@code true}, provides the full stack trace, otherwise
+ * provides a limited extract of stack trace
+ */
+ public static void stackTraceToSingleLineString(StringBuilder buffer, Throwable throwable, boolean isDebug) {
+ if (throwable == null) {
+ return;
+ }
+ if (isDebug) {
+ buffer.append(throwable);
+ // add first-level stack trace
+ for (StackTraceElement e : throwable.getStackTrace()) {
+ buffer.append(" / ");
+ buffer.append(e.getFileName());
+ buffer.append(":");
+ buffer.append(e.getLineNumber());
+ }
+ // add stack trace of all underlying causes
+ while (throwable.getCause() != null) {
+ throwable = throwable.getCause();
+ buffer.append("; caused by ");
+ buffer.append(throwable);
+
+ for (StackTraceElement e : throwable.getStackTrace()) {
+ buffer.append(" / ");
+ buffer.append(e.getFileName());
+ buffer.append(":");
+ buffer.append(e.getLineNumber());
+ }
+ }
+ } else {
+ if ((throwable instanceof InvocationTargetException) && (throwable.getCause() != null)) {
+ throwable = throwable.getCause();
+ }
+ // add class name and message of the exception
+ String message = throwable.getMessage();
+ if ((message == null) || (message.length() == 0)) {
+ String className = throwable.getClass().getName();
+ try {
+ className = className.substring(className.lastIndexOf('.') + 1);
+ } catch (Exception e) { /* ignored */
+ }
+ buffer.append(className);
+ } else {
+ buffer.append(message);
+ }
+ // add first 20 items of the first-level stack trace
+ int i = 0;
+ buffer.append(" (");
+ for (StackTraceElement e : throwable.getStackTrace()) {
+ if (i > 20) {
+ buffer.append(" ...");
+ break;
+ } else if (i > 0) {
+ buffer.append(" ");
+ }
+
+ buffer.append(e.getFileName());
+ buffer.append(":");
+ buffer.append(e.getLineNumber());
+ i++;
+ }
+ buffer.append(")");
+ }
+ }
+
+ /**
* Returns a string representation of the contents of the provided byte
* sequence using hexadecimal characters and a space between each byte.
*
--
Gitblit v1.10.0