get Stack Trace Message by count and level - Android java.lang

Android examples for java.lang:Throwable

Description

get Stack Trace Message by count and level

Demo Code


//package com.java2s;

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

public class Main {
    public static String getStackTraceMessage(int count, int skip) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        StackTraceElement stack[] = (new Throwable()).getStackTrace();
        int ix = skip + 1;
        int c = count;
        while (ix < stack.length) {
            StackTraceElement frame = stack[ix];
            String cname = frame.getClassName();
            String mname = frame.getMethodName();
            if (cname.equals("java.util.logging.Logger")) {
                break;
            }/*from  www . j  a v a 2s .c o  m*/
            int linenumber = frame.getLineNumber();
            pw.println("");
            pw.print(cname + ":" + mname + ":" + linenumber);
            c--;
            if (c < 1)
                break;
            ix++;
        }
        return sw.toString();
    }
}

Related Tutorials