Android provides a user-level lightweight LOG Mechanism. Its implementation runs through Java, JNI, local c/c ++ implementation, Linux kernel driver, and Other Android layers, it is simple and clear enough to explain the case. This series of articles explains the internal implementation mechanism of the LOG mechanism. This article is the second of this series. It explains how the Android Java program outputs LOG information to the LOG system.
LOG output help class
The Android Java program outputs logs through the android. util. Log class and lists the commonly used static Log methods.
Generally, to output Log information, you can directly call Log. v ()/Log. d ()/Log. I ()/Log. w ()/Log. e. There are so many different methods here, which is also the Log classification. Log classification is defined as the static constant member of Log, and the priority of Log is sorted by number. The priority of a large number is higher. While Log. what a Terrible Failure is recorded by wtf (), and this error is reported not only in logs, but also on the interface, the current process may be killed.
If the LOG priority to be output is lower than the current priority, the Log information is not displayed. Generally, before printing logs using the Log method in Java programs, you should first use isLoggable () to determine whether this level can be recorded.
In addition, Log. println () can be used to match Log. v ()/Log. d ()/... And other methods, but when using it, you must specify the corresponding priority.
Log-like implementation
The implementation of android. util. Log is relatively simple.
The constructors of the android. util. Log class are private and will not be instantiated. They only provide static attributes and methods.
Android. util. the implementation of various Log record methods of Log relies on the Implementation of native println_native (), Log. v ()/Log. d ()/Log. I ()/Log. w ()/Log. e () eventually calls println_native (). For example, the implementation of Log. d:
Public static int d (String tag, String msg ){
Return println_native (LOG_ID_MAIN, DEBUG, tag, msg );
}
THE Native METHOD println_native () is implemented in c/c ++ through JNI. For details, see section 3: JNI and c/c ++ in this series.