android开发中怎么通过Log函数输出当前行号和当前函数名
2015-01-10 18:34
513 查看
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信息了 } }
另一种使用形式:
public class DebugInfo extends Exception { public int line() { StackTraceElement[] trace = getStackTrace(); if (trace == null || trace.length == 0) { return -1; } return trace[0].getLineNumber(); } public String fun() { StackTraceElement[] trace = getStackTrace(); if (trace == null || trace.length == 0) { return ""; } return trace[0].getMethodName(); } public DebugInfo() { super(); } @Override public String toString() { return line() + "|" + fun() + "|"; } }
使用方法
Log.d(TAG, new DebugInfo() + "hello world!");
public class DebugInfo { public static int line(StackTraceElement e) { return e.getLineNumber(); } public static String method(StackTraceElement e) { return e.getMethodName(); } public static String info(StackTraceElement e) { String ret = line(e) + "|" + method(e) + "|"; return ret; } }
相关文章推荐
- C应用程序增加调试便利的方法,程序运行过程中输出运行的源文件名称 函数名称和当前行号
- 【iOS-cocos2d-X 游戏开发之十三】详细讲解在Xcode中利用预编译并通过Jni调用Android的Java层代码(cocos2dx里访问调用Android函数)!
- Lua 输出函数名称 和 当前行号
- android开发,使用log在native code中输出日志
- [转]Android系统开发中LOG的输出与使用
- Android系统开发中LOG的输出与使用
- Android系统开发中LOG的输出与使用
- Android开发之————通过代码来得到当前手机屏幕的密度值
- jni开发通过搜集在C++中的log输出日志生成可读的.log文件到手机sd中
- [转]Android系统开发中LOG的输出与使用
- Android system自带 通过kernel 输出上层log的方法
- 可以输出类名、函数名以及所在行号的Log帮助类
- 作为一个程序员怎么通过android开发赚钱
- Android开发相关——Log日志输出显示不完整以及GsonFormat
- android NDK JNI设置自己的log输出函数
- 【iOS-cocos2d-X 游戏开发之十三】详细讲解在Xcode中利用预编译并通过Jni调用Android的Java层代码(cocos2dx里访问调用Android函数)!
- Log输出打印输出LOG所在的函数名以及行号
- Android 开发 Log无输出
- [转]Android系统开发中LOG的输出与使用
- [转]Android系统开发中LOG的输出与使用