Android原始碼解析之(六)-->Log日誌

來源:互聯網
上載者:User

標籤:script   constant   ring   private   info   framework   priority   詳細   git   

轉載請標明出處:一片楓葉的專欄

首先說點題外話,對於想學android framework原始碼的同學,事實上能夠在github中fork一份,詳細地址:platform_frameworks_base
這裡面基本都是android framework層的原始碼了。並且近期發現了一個比較不錯的github外掛程式:OctoTree,它 是一個瀏覽器外掛程式,它能夠讓你在Github 看代碼時,左側邊欄會出現一個樹狀結構。就像我們在IDE 一樣。當我們看一個項目的結構,或者想看詳細的某個檔案,這樣就會非常方便。

怎麼樣這樣查看原始碼的話是不是非常方面?

好了說一下我們今天須要介紹的Log對象,它位於android framework層utils包下,是一個final class類:查看其詳細定義:

public final class Log {    /**     * Priority constant for the println method; use Log.v.     */    public static final int VERBOSE = 2;    /**     * Priority constant for the println method; use Log.d.     */    public static final int DEBUG = 3;    /**     * Priority constant for the println method; use Log.i.     */    public static final int INFO = 4;    /**     * Priority constant for the println method; use Log.w.     */    public static final int WARN = 5;    /**     * Priority constant for the println method; use Log.e.     */    public static final int ERROR = 6;    /**     * Priority constant for the println method.     */    public static final int ASSERT = 7;    private Log() {    }    /**     * 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 msg) {        return println(LOG_ID_MAIN, VERBOSE, tag, msg);    }    /**     * Send a {@link #VERBOSE} 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 msg The message you would like logged.     * @param tr An exception to log     */    public static int v(String tag, String msg, Throwable tr) {        return println(LOG_ID_MAIN, VERBOSE, tag, msg + ‘\n‘ + getStackTraceString(tr));    }    /**     * 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 msg) {        return println(LOG_ID_MAIN, DEBUG, tag, msg);    }    /**     * Send a {@link #DEBUG} 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 msg The message you would like logged.     * @param tr An exception to log     */    public static int d(String tag, String msg, Throwable tr) {        return println(LOG_ID_MAIN, DEBUG, tag, msg + ‘\n‘ + getStackTraceString(tr));    }    /**     * 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 msg) {        return println(LOG_ID_MAIN, INFO, tag, msg);    }    /**     * Send a {@link #INFO} 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 msg The message you would like logged.     * @param tr An exception to log     */    public static int i(String tag, String msg, Throwable tr) {        return println(LOG_ID_MAIN, INFO, tag, msg + ‘\n‘ + getStackTraceString(tr));    }    /**     * 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 msg) {        return println(LOG_ID_MAIN, WARN, tag, msg);    }    /**     * 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 msg The message you would like logged.     * @param tr An exception to log     */    public static int w(String tag, String msg, Throwable tr) {        return println(LOG_ID_MAIN, WARN, tag, msg + ‘\n‘ + getStackTraceString(tr));    }    /*     * 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) {        return println(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));    }    /**     * 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) {        return println(LOG_ID_MAIN, ERROR, tag, msg);    }    /**     * Send a {@link #ERROR} 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 msg The message you would like logged.     * @param tr An exception to log     */    public static int e(String tag, String msg, Throwable tr) {        return println(LOG_ID_MAIN, ERROR, tag, msg + ‘\n‘ + getStackTraceString(tr));    }    /**     * Handy function to get a loggable stack trace from a Throwable     * @param tr An exception to log     */    public static String getStackTraceString(Throwable tr) {        if (tr == null) {            return "";        }        // This is to reduce the amount of log spew that apps do in the non-error        // condition of the network being unavailable.        Throwable t = tr;        while (t != null) {            if (t instanceof UnknownHostException) {                return "";            }            t = t.getCause();        }        StringWriter sw = new StringWriter();        PrintWriter pw = new PrintWriter(sw);        tr.printStackTrace(pw);        pw.flush();        return sw.toString();    }    /**     * 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) {        return println(LOG_ID_MAIN, priority, tag, msg);    }    /** @hide */ public static final int LOG_ID_MAIN = 0;    /** @hide */ public static final int LOG_ID_RADIO = 1;    /** @hide */ public static final int LOG_ID_EVENTS = 2;    /** @hide */ public static final int LOG_ID_SYSTEM = 3;    /** @hide */ public static final int LOG_ID_CRASH = 4;    /** @hide */ @SuppressWarnings("unused")    public static int println(int bufID,            int priority, String tag, String msg) {        return 0;    }}

能夠看到事實上final 類。所以我們不能通過繼承Log類的方式實現自身的日誌工具類,一般的我們能夠通過定義Log成員變數的方式。封裝Log工具方法;

在Log類中我們定義了六種記錄層級,各自是:VERBOSE、DEBUG、INFO、WARN、ERROR、ASSERT等六種層級,可是我們平時使用的僅僅有前五種,即VERBOSE,DEBUG,INFO,WARN,ERROR。

通過查看原始碼我們發現Log類中全部的靜態日誌方法Log.v(),Log.d()。Log.i(),Log.w(),Log.e()等方法都是底層都是調用了println方法,然後在github的原始碼中查看,事實上其內部調用的是println_native方法。也就是通過JNI調用底層的c++輸出日誌;

我們臨時僅僅是分析到這裡。至於底層的c++日誌輸出的詳細實現不作分析。總結一下:

  • Log.java是一個final類。所以我們不能夠繼承Log類來實現自己的日誌架構。可是能夠通過關聯(儲存Log成員變數)的方式實現自己的Log工具類;

  • Log.java中定義了六種記錄層級。可是我們通常僅僅是使用當中的五種記錄層級。分別相應著VERBOSE、DEBUG、INFO、WARN、ERROR,在詳細的使用情境下詳細分析;

  • 有些同學對android內建的日誌架構不太愜意。主要是無法定位日誌位置,這裡能夠查看我寫的一篇實現自己定義日誌架構的文章:github項目解析(五)–>android日誌架構

日誌能夠個人化的展示相關資訊:

另外對android原始碼解析方法感興趣的可參考我的:
android原始碼解析之(一)–>android項目構建過程
android原始碼解析之(二)–>非同步訊息機制
android原始碼解析之(三)–>非同步任務AsyncTask
android原始碼解析之(四)–>HandlerThread
android原始碼解析之(五)–>IntentService

本文以同步至github中:https://github.com/yipianfengye/androidSource,歡迎star和follow

Android原始碼解析之(六)-->Log日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.