android开发中怎么通过Log函数输出当前行号和当前函数名
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
使用场景:
| 1 2 3 4 5 6 7 8 |
|
另一种使用形式:

1 public class DebugInfo extends Exception {
2 public int line() {
3 StackTraceElement[] trace = getStackTrace();
4 if (trace == null || trace.length == 0) {
5 return -1;
6 }
7 return trace[0].getLineNumber();
8 }
9
10 public String fun() {
11 StackTraceElement[] trace = getStackTrace();
12 if (trace == null || trace.length == 0) {
13 return "";
14 }
15 return trace[0].getMethodName();
16 }
17
18 public DebugInfo() {
19 super();
20 }
21
22 @Override
23 public String toString() {
24 return line() + "|" + fun() + "|";
25 }
26 }

使用方法
Log.d(TAG, new DebugInfo() + "hello world!");

1 public class DebugInfo {
2 public static int line(StackTraceElement e) {
3 return e.getLineNumber();
4 }
5
6 public static String method(StackTraceElement e) {
7 return e.getMethodName();
8 }
9
10 public static String info(StackTraceElement e) {
11 String ret = line(e) + "|" + method(e) + "|";
12 return ret;
13 }
14 }

本文介绍了一种在Android开发中使用Log函数输出当前行号和函数名的方法。通过创建Debug类,利用Exception的StackTrace特性,可以方便地获取并打印出这些调试信息,便于开发者追踪和定位问题。
1077

被折叠的 条评论
为什么被折叠?



