public class Debug { public static int line(Exception e) { StackTraceElement[] trace = e.getStackTrace(); if (trace == null || trace.length == 0) return -1; // return trace[0].getLineNumber(); } public static String fun(Exception e) { StackTraceElement[] trace = e.getStackTrace(); if (trace == null) return ""; // return trace[0].getMethodName(); } }
使用场景:
public class test { public static String DI(Exception e) { return Debug.line(e)+"|"+Debug.fun(e)+"|"; } public test() { Log.d(TAG, DI(new Exception())); //这里就输出我们需要的debug信息了 } }
时间: 2024-10-12 19:47:21