2015-10-30-03-Android Log使用心得
16lz
2021-01-23
- android 如何打印堆栈信息
- Log扩展类自动打印类名方法名称
android 如何打印堆栈信息
通过如下方法,可以直接打印出程序运行到当前点的堆栈信息,方便调试:
RuntimeException here = new RuntimeException("here");here.fillInStackTrace();Log.i(TAG, "test", here);
Log扩展类,自动打印类名,方法名称
import android.util.Log; /** * Log打印工具,可以快速打印log及其所在的方法名称。 * 调用示例LogExt.LogD(this, Thread.currentThread().getStackTrace(), ...); * @author yyxz */ public class LogExt { private static final String TAG= "yyxz"; private static boolean CanShowLog(){return isShowLog;}; private static boolean isShowLog = true; // Log Debug public static void LogD(Object obj, StackTraceElement[] sTE) { LogD(obj, sTE, null); } public static void LogD(Object obj, StackTraceElement[] sTE, String strInfo) { LogD(obj, sTE, strInfo, null); } public static void LogD(Object obj, StackTraceElement[] sTE, String strInfo, Throwable tr) { if(!CanShowLog()) { return ; } Class cls; if (obj instanceof Class) { cls = ( Class)obj; } else { cls = obj.getClass(); } String methodName = sTE[2].getMethodName(); if ((cls == null) || (methodName == null)) { Log. e(TAG, "Input Class or MethodName Error(null)" ); } else { if (strInfo == null) { if (tr == null) { Log. d(TAG, cls.toString() + "::" + methodName); } else { Log. d(TAG, cls.toString() + "::" + methodName, tr); } } else { if (tr == null) { Log. d(TAG, cls.toString() + "::" + methodName + "(), " + strInfo); } else { Log. d(TAG, cls.toString() + "::" + methodName + "(), " + strInfo, tr); } } } } // Log Error public static void LogE(Object obj, StackTraceElement[] sTE) { LogE(obj, sTE, null); } public static void LogE(Object obj, StackTraceElement[] sTE, String strInfo) { LogE(obj, sTE, strInfo, null); } public static void LogE(Object obj, StackTraceElement[] sTE, String strInfo, Throwable tr) { if(!CanShowLog()) { return ; } Class cls; if (obj instanceof Class) { cls = ( Class)obj; } else { cls = obj.getClass(); } String methodName = sTE[2].getMethodName(); if ((cls == null) || (methodName == null)) { Log. e(TAG, "Input Class or MethodName Error(null)" ); } else { if (strInfo == null) { if (tr == null) { Log. e(TAG, cls.toString() + "::" + methodName); } else { Log. e(TAG, cls.toString() + "::" + methodName, tr); } } else { if (tr == null) { Log. e(TAG, cls.toString() + "::" + methodName + "(), " + strInfo); } else { Log. e(TAG, cls.toString() + "::" + methodName + "(), " + strInfo, tr); } } } } // Log Info public static void LogI(Object obj, StackTraceElement[] sTE) { LogI(obj, sTE, null); } public static void LogI(Object obj, StackTraceElement[] sTE, String strInfo) { LogI(obj, sTE, strInfo, null); } public static void LogI(Object obj, StackTraceElement[] sTE, String strInfo, Throwable tr) { if(!CanShowLog()) { return ; } Class cls; if (obj instanceof Class) { cls = ( Class)obj; } else { cls = obj.getClass(); } String methodName = sTE[2].getMethodName(); if ((cls == null) || (methodName == null)) { Log. e(TAG, "Input Class or MethodName Error(null)" ); } else { if (strInfo == null) { if (tr == null) { Log. i(TAG, cls.toString() + "::" + methodName); } else { Log. i(TAG, cls.toString() + "::" + methodName, tr); } } else { if (tr == null) { Log. i(TAG, cls.toString() + "::" + methodName + "(), " + strInfo); } else { Log. i(TAG, cls.toString() + "::" + methodName + "(), " + strInfo, tr); } } } } // Log Version public static void LogV(Object obj, StackTraceElement[] sTE) { LogV(obj, sTE, null); } public static void LogV(Object obj, StackTraceElement[] sTE, String strInfo) { LogV(obj, sTE, strInfo, null); } public static void LogV(Object obj, StackTraceElement[] sTE, String strInfo, Throwable tr) { if(!CanShowLog()) { return ; } Class cls; if (obj instanceof Class) { cls = ( Class)obj; } else { cls = obj.getClass(); } String methodName = sTE[2].getMethodName(); if ((cls == null) || (methodName == null)) { Log. v(TAG, "Input Class or MethodName Error(null)" ); } else { if (strInfo == null) { if (tr == null) { Log. v(TAG, cls.toString() + "::" + methodName); } else { Log. v(TAG, cls.toString() + "::" + methodName, tr); } } else { if (tr == null) { Log. v(TAG, cls.toString() + "::" + methodName + "(), " + strInfo); } else { Log. v(TAG, cls.toString() + "::" + methodName + "(), " + strInfo, tr); } } } } // Log Warn public static void LogW(Object obj, StackTraceElement[] sTE) { LogW(obj, sTE, null); } public static void LogW(Object obj, StackTraceElement[] sTE, String strInfo) { LogW(obj, sTE, strInfo, null); } public static void LogW(Object obj, StackTraceElement[] sTE, String strInfo, Throwable tr) { if(!CanShowLog()) { return ; } Class cls; if (obj instanceof Class) { cls = ( Class)obj; } else { cls = obj.getClass(); } String methodName = sTE[2].getMethodName(); if ((cls == null) || (methodName == null)) { Log. e(TAG, "Input Class or MethodName Error(null)" ); } else { if (strInfo == null) { if (tr == null) { Log. w(TAG, cls.toString() + "::" + methodName); } else { Log. w(TAG, cls.toString() + "::" + methodName, tr); } } else { if (tr == null) { Log. w(TAG, cls.toString() + "::" + methodName + "(), " + strInfo); } else { Log. w(TAG, cls.toString() + "::" + methodName + "(), " + strInfo, tr); } } } } }
更多相关文章
- HorizontalScrollView不显示滚动条,布局完全填充的方法
- Android数据存储的方法
- 在Android命令行启动程序的方法
- Android sdk配置 常见问题及处理方法
- Android之自定义Dialog屏幕旋转时自动关闭解决方法
- react native修改APP的名称和图标
- Android客户端和服务器端数据交互的第一种方法