转载请标明出处:一片枫叶的专栏

首先说点题外话,对于想学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日志的更多相关文章

  1. Android源代码解析之(七)-->LruCache缓存类

    转载请标明出处:一片枫叶的专栏 android开发过程中常常会用到缓存.如今主流的app中图片等资源的缓存策略通常是分两级.一个是内存级别的缓存,一个是磁盘级别的缓存. 作为android系统的维护者 ...

  2. Android源代码解析之(十三)-->apk安装流程

    转载请标明出处:一片枫叶的专栏 上一篇文章中给大家分析了一下android系统启动之后调用PackageManagerService服务并解析系统特定文件夹.解析apk文件并安装的过程,这个安装过程实 ...

  3. Android源代码解析之(四)-->HandlerThread

    转载请标明出处:一片枫叶的专栏 上一篇文章中我们解说了AsyncTast的基本使用以及实现原理,我们知道AsyncTask内部是通过线程池和Handler实现的.通过对线程池和handler的封装实现 ...

  4. Android源代码解析之(三)-->异步任务AsyncTask

    转载请标明出处:一片枫叶的专栏 上一篇文章中我们解说了android中的异步消息机制. 主要解说了Handler对象的使用方式.消息的发送流程等.android的异步消息机制是android中多任务处 ...

  5. Android开发华为手机无法看log日志解决方法

    Android开发华为手机无法看log日志解决方法 上班的时候,由于开发工具由Eclipse改成Android Studio后,原本的华为手机突然无法查看崩溃日志了,大家都知道,若是无法查看日志要它毛 ...

  6. Xposed框架Hook Android应用的所有类方法打印Log日志

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80954759 在进行Android程序的逆向分析的时候,经常需要Android程 ...

  7. Android 源代码解析 之 setContentView

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41894125,本文出自:[张鸿洋的博客] 大家在平时的开发中.对于setCont ...

  8. Android View体系(八)从源代码解析View的layout和draw流程

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

  9. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

随机推荐

  1. 字符串函数 (strfun)

    字符串函数 (strfun) 题目描述 两个等长的由大写英文字母构成的字符串a和b,从a中选择连续子串x,从b中选出连续子串y.子串x与子串y的长度相等. 定义函数f(x,y)为满足条件xi=yi(1 ...

  2. ReactJS 学习路线

    Node.js: 推荐采用nvm的方式安装,nvm(Node Version Manager)用于Node的版本管理,方便不同版本的Node之间的切换 安装npm,npm(Node Package M ...

  3. :nth-child :nth-type-of用法详解

     ele:nth-of-type(n) 是指父元素下ele元素里的第n个ele:nth-child(n) 是指父元素下第n个元素且这个元素为ele 

  4. hihocoder #1419 : 后缀数组四·重复旋律4

    #1419 : 后缀数组四·重复旋律4 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构 ...

  5. set与map的区别

    STL中:MAP的节点是一对数据.   SET的节点是一个数据. Map使用关键值Key来唯一标识每一个成员 map可以重复.set是集合 都属于关联容器   只不过,   map的形式   map& ...

  6. [TYVJ1930]编年史

    现在 applepi 手上有一本十分古老的编年史,这本史书记录了很多著名的历史事件.于是applepi 有了一个奇怪的想法……他想知道那些有名的历史事件都是在星期几发生的.现在轮到你了,你要帮助app ...

  7. WML标签速查手册 wap标签

    原文发布时间为:2010-08-19 -- 来源于本人的百度文章 [由搬家工具导入] WML标签速查手册(转)比较方便便于速查   结构相关标签 语法及属性 <wml> <wml x ...

  8. JavaScript日历控件!JS兼容IE6.7.FF.可挡住下拉控件

    原文发布时间为:2009-08-22 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  9. poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp

    题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...

  10. duilib入门简明教程 -- 响应按钮事件(4) (转)

    原文转自 http://www.cnblogs.com/Alberl/p/3343610.html     上一个Hello World的教程里有一句代码是这样的:CControlUI *pWnd = ...