dump Stack Trace from Thread.currentThread().getStackTrace() via StackTraceElement - Android java.lang

Android examples for java.lang:Thread

Description

dump Stack Trace from Thread.currentThread().getStackTrace() via StackTraceElement

Demo Code


public class Main{

    /**//from  ww w. j  ava2s. c o m
     * Dumps the stack trace.
     *
     * @param level How many levels of the stack are dumped. 0 means all.
     * @return A {@link java.lang.String} of all the output with newline
     * between each.
     */
    public static String dumpStackTrace(int level) {
        StackTraceElement[] elems = Thread.currentThread().getStackTrace();
        // Ignore the first 3 elements.
        level = (level == 0 ? elems.length : Math.min(level + 3,
                elems.length));
        String ret = new String();
        for (int i = 3; i < level; i++) {
            ret = ret + "\t" + elems[i].toString() + '\n';
        }
        return ret;
    }

}

Related Tutorials