Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android上一個可追蹤代碼到函數具體某行的日志類

android上一個可追蹤代碼到函數具體某行的日志類

編輯:關於Android編程

代碼如下:
[java] 
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 < level) 
            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 < level) 
            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 < level) 
            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 < level) 
            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 < level) 
            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()); 
    }  www.2cto.com
 
    private static int getLineNumber() { 
        return Thread.currentThread().getStackTrace()[5].getLineNumber(); 
    } 

 
好用的話,記得給好評,嘿嘿!
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved