Java Stacktrace to String stackTraceToString(final Throwable t, final boolean header, final boolean trim)

Here you can find the source of stackTraceToString(final Throwable t, final boolean header, final boolean trim)

Description

Just return the stack trace as new-line-separated string.

License

Open Source License

Parameter

Parameter Description
t a parameter
header Add a header about the exception itself, if set to true.
trim If to make the output more compact in case of repetitions.

Declaration

public static final String stackTraceToString(final Throwable t, final boolean header, final boolean trim) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  w  w w.  j  ava 2  s.co  m*/
     * 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.
     * @param trim If to make the output more compact in case of repetitions.
     * @return
     */
    public static final String stackTraceToString(final Throwable t, final boolean header, final boolean trim) {
        // TODO: Consider to use System.getProperty("line.separator").
        // TODO: Consider to add a trimDepth argument, for repetition of a sequence of elements.
        final StringBuilder b = new StringBuilder(325);
        if (header) {
            b.append(t.toString()); // TODO: Check.
            b.append("\n");
        }
        final StackTraceElement[] elements = t.getStackTrace();
        StackTraceElement last = null; // Assume this is faster than operating on the strings.
        int repetition = 0;
        for (int i = 0; i < elements.length; i++) {
            final StackTraceElement element = elements[i];
            if (trim) {
                if (element.equals(last)) {
                    repetition += 1;
                    continue;
                } else {
                    if (repetition > 0) {
                        if (header) {
                            b.append("\t");
                        }
                        b.append("(... repeated " + repetition + " times.)\n");
                        repetition = 0;
                    }
                    last = element;
                }
            }
            if (header) {
                b.append("\t");
            }
            b.append(element);
            b.append("\n");
        }
        if (repetition > 0) {
            if (header) {
                b.append("\t");
            }
            b.append("(... repeated " + repetition + " times.)\n");
        }
        return b.toString();
    }
}

Related

  1. stackTraceToString(Exception e)
  2. stackTraceToString(Exception e, int nBack)
  3. stackTraceToString(Exception ex, String message)
  4. stackTraceToString(Exception exception)
  5. stackTraceToString(final StackTraceElement[] stackTrace)
  6. stacktraceToString(final Throwable throwable)
  7. stacktraceToString(StackTraceElement[] s)
  8. stackTraceToString(StackTraceElement[] stackTrace)
  9. stackTraceToString(StackTraceElement[] stackTrace)