Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Androd封裝一個Log打印工具一鍵實現打印不打印

Androd封裝一個Log打印工具一鍵實現打印不打印

編輯:關於Android編程

在寫項目的時候,我們是避免不了用到安卓裡面的Log打印工具的,但是當代碼越寫越多的時候我們這加個Log 那加個Log,當我們項目要上線的時候,我們總會忘記哪有Log,很麻煩啊,當時殺人的心都有了,現在封裝一個Log工具,只需要定義boolean就可以一鍵實現打印和不打印功能,直接復制過去拿到項目裡面用就行!

不要感謝我,請叫我雷鋒

package com.example.qlog;

import java.util.Calendar;

public final class QLog {
	public static final boolean DEBUG = true;//是否打印

    /**
     * Send a {@link #VERBOSE} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int v(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.v(tag, format);
            }
            return android.util.Log.v(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #DEBUG} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int d(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.d(tag, format);
            }
            
            return android.util.Log.d(tag, String.format(format, args));
        }
        return 0;
    }
    

    /**
     * Send an {@link #INFO} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int i(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.i(tag, format);
            }
            return android.util.Log.i(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #WARN} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int w(String tag, String format, Object... args) {
        if (DEBUG) {
            if (args == null || args.length == 0) {
                return android.util.Log.w(tag, format);
            }
            return android.util.Log.w(tag, String.format(format, args));
        }
        return 0;
    }

    /**
     * Send a {@link #WARN} log message and log the exception.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param tr An exception to log
     */
    public static int w(String tag, Throwable tr) {
        if (DEBUG) {
            return android.util.Log.w(tag, tr);
        }
        return 0;
    }

    /**
     * Send an {@link #ERROR} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int e(String tag, String msg, Throwable e) {
        if (DEBUG) {
            return android.util.Log.e(tag, msg, e);
        }
        return 0;
    }

    /**
     * Send an {@link #ERROR} log message.
     * 
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     */
    public static int e(String tag, String msg) {
        if (DEBUG) {
            return android.util.Log.e(tag, msg);
        }
        return 0;
    }

    /**
     * Low-level logging call.
     * 
     * @param priority The priority/type of this log message
     * @param tag Used to identify the source of a log message. It usually identifies the class or activity where the
     *            log call occurs.
     * @param msg The message you would like logged.
     * @return The number of bytes written.
     */
    public static int println(int priority, String tag, String msg) {
        if (DEBUG) {
            return android.util.Log.println(priority, tag, msg);
        }
        return 0;
    }

    /**
     * 打印調用時間
     * 
     * @param cur ????的時??
     * @return 調用時間
     */
    public static long debugDuration(long cur) {
        long sec = System.currentTimeMillis();
        StackTraceElement elem = Thread.currentThread().getStackTrace()[3];
        d("Performance", elem.getFileName() + "_" + elem.getLineNumber() + ":" + (sec - cur));
        return sec;
    }

    /**
     * 打印當前調用的位置: 文件 行號 方法
     * 
     * @param tag
     * @return
     */
    public static int printLogPos(String tag) {
        StackTraceElement elem = Thread.currentThread().getStackTrace()[3];
        return d(tag, elem.getClassName() + ":" + elem.getLineNumber() + "::" + elem.getMethodName());
    }

    /**
     * 獲得當前調用位置??類名
     * 
     * @param depth 堆棧深度
     * @return
     */
    public static String getLogPos(int depth) {
        StackTraceElement elem = Thread.currentThread().getStackTrace()[depth];
        return elem.getClassName();
    }

    /**
     * depth = 4
     * 
     * @see #getLogPos(int)
     * @return
     */
    public static String getLogPos() {
        return getLogPos(4);
    }

    /**
     * 獲得當前??code>from????的秒??     * 
     * @param from
     * @return
     */
    public static long getSecond(Calendar from) {
        return (System.currentTimeMillis() - from.getTimeInMillis()) / 1000;
    }

    private static final Calendar _20120101 = Calendar.getInstance();
    static {
        _20120101.set(2012, 0, 0, 0, 0, 0);
    }

    /**
     * from = 20120101
     * 
     * @see #getSecond(Calendar)
     * @return
     */
    public static long getSecond() {
        return getSecond(_20120101);
    }

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved