Android Log 调试显示类名和行数

在Android开发中,对日志的调试中可能有着非常众多的花样,这里介绍的是最常用的做法,显示日志的类名和行数。
参考代码如下:
public final class Ln {
private static boolean isDebug;

private Ln () {
}

public static void init (Context context) {
ApplicationInfo info = context.getApplicationInfo();
// 判断 APP 是否处在调试模式
isDebug = (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 ;
}

public static boolean isDebugEnabled () {
return isDebug;
}

public static void d (Object s) {
if (isDebugEnabled()) {
Log.println(Log.DEBUG, getTag(), " " + String.valueOf(s));
}
}

public static void e (Object s) {
Log.println(Log.ERROR, getTag(), " " + String.valueOf(s));
}

private static String getTag () {
final int skipDepth = 4 ; // skip 6 stackframes to find the location where this was called
if (isDebugEnabled()) {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
final StackTraceElement trace = elements[skipDepth];
return trace.getFileName() + trace.getLineNumber();
}
return "" ;
}
}


猜你喜欢

转载自blog.csdn.net/polo2044/article/details/80655316