Android L log system 2 - JavaAPI and liblog

After Android L (including Android L), Andoird uses a new log system, which is not the previous method of combining Kernel Ring Buffer to store, read and write Log. Instead use the new logging mechanism Logd. Therefore, several devices created under /dev/log/ are not used at all! did not use!

In fact, when init created them, there were instructions, but they didn't notice it.

INFO("kernel logger is deprecated\n");

Let's analyze the log system of Android L.

From the analysis of the previous article "Kernel's Ring Buffer (Ring Buffer) - Taking Logger Buffer as an example", we can see that the Log of the Android system is stored and managed with a ring buffer. After replacing it with Logd, it should also pass through the Ring Buffer. To manage, just change from Kernel space to user space. So now let's take a look at how the user layer writes the Log to this buffer and reads the Log from this buffer.

When writing an APP in the Java layer, some static methods of the package android.util.Log are generally called to print the Log;

 
 
  
  
  1. java.lang.Object
  2. ↳ android.util.Log
  3. API for sending log output.
  4. Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e() methods.

Analyze Log.java, Log.v() Log.d() Log.i, etc. are finally called to

 
 
  
  
  1. public static int v(String tag, String msg) {
  2. return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
  3. }

="Call android_util_Log.cpp through JNI

 
 
  
  
  1. { "println_native", "(IILjava/lang/String;Ljava/lang/String;)I", (void*) android_util_Log_println_native },
  2. ...>
  3. static jint android_util_Log_println_native(JNIEnv* env, jobject clazz,
  4. jint bufID, jint priority, jstring tagObj, jstring msgObj)
  5. {
  6.     //先判断ID是否是一个合法LOG_ID_MAX,这个变量定义在system/下面的Log.h里面 
  7. if (bufID < 0 || bufID >= LOG_ID_MAX) {
  8. jniThrowNullPointerException(env, "bad bufID");
  9. return -1;
  10. }
  11. //Take out the TAG
  12. if (tagObj != NULL)
  13. tag = env->GetStringUTFChars(tagObj, NULL);
  14. //Take out the Message to be written
  15.      msg = env->GetStringUTFChars(msgObj, NULL);
  16. //调用__android_log_buf_write来写入到buffer
  17. int res = __android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);
  18. if (tag != NULL)
  19. env->ReleaseStringUTFChars(tagObj, tag);
  20. env->ReleaseStringUTFChars(msgObj, msg);
  21. return res;
  22. }

__android_log_buf_write is implemented in liblog.

In liblog, the log will be written to logd for processing through sokect communication. The general process is as follows:

3e8b95c5-c57e-4db1-831c-4398117c66f5

Guess you like

Origin blog.csdn.net/koffuxu/article/details/53997348