Java Stacktrace Print formatStackTrace(Throwable t, String prefix)

Here you can find the source of formatStackTrace(Throwable t, String prefix)

Description

Formats the stack trace and returns the result.

License

LGPL

Parameter

Parameter Description
prefix the prefix shown in front of each line of the stack trace; null to denote empty

Declaration

public static String formatStackTrace(Throwable t, String prefix) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    /**//from   w w  w.j  a va2 s  .c o  m
     * Formats the stack trace and returns the result.
     * Currently, it only adds the prefix to each line.
     *
     * @param prefix the prefix shown in front of each line of the stack trace;
     *               null to denote empty
     */
    public static String formatStackTrace(Throwable t, String prefix) {
        return formatStackTrace(null, t, prefix).toString();
    }

    /**
     * Formats the stack trace and appends it to the specified string buffer.
     *
     * @param sb     the string buffer to append the stack trace. A string buffer
     *               will be created if null.
     * @param prefix the prefix shown in front of each line of the stack trace;
     *               null to denote empty
     */
    public static StringBuffer formatStackTrace(StringBuffer sb, Throwable t, String prefix) {
        return formatStackTrace(sb, t, prefix, 0);
    }

    /**
     * Formats the stack trace and appends it to the specified string buffer,
     * but only display at most maxcnt lines.
     * <p/>
     * <p>The maximal allowed number of lines is controlled by
     * maxcnt. Note: a stack frame is not counted, if it belongs
     * to java.*, javax.* or sun.*.
     *
     * @param sb     the string buffer to append the stack trace. A string buffer
     *               will be created if null.
     * @param prefix the prefix shown in front of each line of the stack trace;
     *               null to denote empty
     * @param maxcnt the maximal allowed number of lines to dump (<=0: no limit)
     */
    public static StringBuffer formatStackTrace(StringBuffer sb, final Throwable t, String prefix, int maxcnt) {
        final StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        final StringBuffer trace = sw.getBuffer();

        if (prefix == null) {
            prefix = "";
        }
        if (maxcnt > 0 || prefix.length() > 0) {
            final int len = trace.length();
            if (sb == null) {
                sb = new StringBuffer(len + 256);
            }
            if (maxcnt <= 0) {
                maxcnt = Integer.MAX_VALUE;
            }
            boolean ignoreCount = false;
            for (int j = 0; j < len;) { //for each line
                if (!ignoreCount && --maxcnt < 0) {
                    sb.append(prefix).append("...");
                    break;
                }

                //StringBuffer has no indexOf(char,j), so...
                int k = j;
                while (k < len && trace.charAt(k++) != '\n') {
                    ; //point k to the char after \n
                }
                String frame = trace.substring(j, k);
                sb.append(prefix).append(frame);
                j = k;

                ignoreCount = inStack(frame, "java.") || inStack(frame, "javax.") || inStack(frame, "sun.");
            }
        } else {
            if (sb == null) {
                return trace;
            }
            sb.append(trace);
        }
        return sb;
    }

    private static boolean inStack(String frame, String sub) {
        final int j = frame.indexOf(sub);
        if (j < 0) {
            return false;
        }
        if (j == 0) {
            return true;
        }

        final char cc = frame.charAt(j - 1);
        return (cc < 'a' || cc > 'z') && (cc < 'A' || cc > 'Z');
    }
}

Related

  1. formatStackTrace(Thread thread)
  2. formatStackTrace(Throwable t)
  3. formatStackTrace(Throwable t)
  4. formatStackTraceToString(Exception ex)
  5. printStackTrace()
  6. printStackTrace()
  7. printStackTrace()