Android source code parsing (5) --) Log Introduction
This is basically the source code of the android framework layer. In addition, we recently found a good github plug-in: OctoTree, which is a browser plug-in that allows you to see the code on Github, and a tree structure will appear on the Left bar, just like in IDE. It is convenient to view the structure of a project or a specific file.
How can I check the source code in this way? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/zo6zL/release/M/release + 0xuS + 38zltqjS5aO6PC9wPg0KPHByZSBjbGFzcz0 = "brush: java;"> 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;}}
We can see the final class, so we cannot inherit the Log class to implement our own Log tool class. Generally, we can encapsulate the Log tool method by defining the Log member variables;
In the Log class, we define six Log levels: VERBOSE, DEBUG, INFO, WARN, ERROR, and ASSERT. However, we usually only use the first five levels, VERBOSE, DEBUG, INFO, WARN, and ERROR.
By viewing the source code, we can find all the static Log methods in the Log class. v (), Log. d (), Log. I (), Log. w (), Log. e () and other methods all call the println method at the underlying layer, and then view it in the source code of github. In fact, the println_native method is called internally, that is, calling the underlying c ++ to output logs through JNI;
For the time being, we will only analyze the specific implementation of the underlying c ++ log output. Here we will summarize:
Log. java is a final class, so we cannot inherit the Log class to implement our own Log framework, but we can implement our own Log tool class by associating (saving Log member variables;
Log. java defines six log levels, but we usually only use five of them, which correspond to VERBOSE, DEBUG, INFO, WARN, and ERROR, respectively, specific analysis in specific use cases;
Some users are not satisfied with the log framework provided by android, mainly because they cannot locate the log location. Logs can display related information in a personalized manner: