android中追踪代码具体到某行的功能代码

来源:爱站网时间:2021-05-24编辑:网友分享
当程序员在进行android开发时候,都会运用到追踪代码到函数具体某行的功能,因为运用这项功能可以帮程序员追踪代码具体到函数某行代码的错误内容,那么具体的代码是怎么样的呢?接下来小编将为你详细介绍。

代码如下:

 

package xiaogang.enif.utils;

/**
* The Class LogUtils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class LogUtils {

private final static int VERBOSE = 0;
private final static int DEBUG = 1;
private final static int INFO = 2;
private final static int WARN = 3;
private final static int ERROR = 4;
private final static int DEFAULT_LEVEL = -1;

private int level;

private final String clazz;

private static final String TAG = "LogUtils";

public static LogUtils getDebugLog(Class> clazz, int l) {
LogUtils log = new LogUtils(clazz);
log.level = l;
return log;
}

public static LogUtils getLog(Class> clazz) {
return new LogUtils(clazz);
}

public LogUtils(Class> clazz) {
this.clazz = "[" + clazz.getSimpleName() + "] ";
level = DEFAULT_LEVEL;
}

public void verbose(String message) {
verbose(message, null);
}

public void debug(String message) {
debug(message, null);
}

public void info(String message) {
info(message, null);
}

public void warn(String message) {
warn(message, null);
}

public void error(String message) {
error(message, null);
}

public void verbose(String message, Throwable t) {
if (VERBOSE return;
if (message != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void debug(String message, Throwable t) {
if (DEBUG return;
if (message != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void info(String message, Throwable t) {
if (INFO return;
if (message != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void warn(String message, Throwable t) {
if (WARN return;
if (message != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

public void error(String message, Throwable t) {
if (ERROR return;
if (message != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}

private static int getLineNumber() {
return Thread.currentThread().getStackTrace()[5].getLineNumber();
}
}


以上就是爱站技术频道小编为大家分享的关于android上一个可追踪代码具体到函数某行的日志类的代码详解,这项功能可是很多开发者都希望有的,有需要的开发者可以直接参考使用。

上一篇:如何用Mono for Android 高效的应用导航的源代码

下一篇:Android应用中如何通过手势实现缩放效果

相关阅读

热门软件源码

最新软件源码下载