Android应用程序框架层和系统运行库层日志系统源代码分析


在开发Android应用程序时,少不了使用Log来监控和调试程序的执行。在上一篇文章Android日志系统驱动程序Logger源代码分析中,我们分析了驱动程序Logger的源代码,在前面的文章浅谈Android系统开发中Log的使用一文,我们也简单介绍在应用程序中使Log的方法,在这篇文章中,我们将详细介绍Android应用程序框架层和系统运行库存层日志系统的源代码,使得我们可以更好地理解Android的日志系统的实现。

我们在Android应用程序,一般是调用应用程序框架层的Java接口(android.util.Log)来使用日志系统,这个Java接口通过JNI方法和系统运行库最终调用内核驱动程序Logger把Log写到内核空间中。按照这个调用过程,我们一步步介绍Android应用程序框架层日志系统的源代码。学习完这个过程之后,我们可以很好地理解Android系统的架构,即应用程序层(Application)的接口是如何一步一步地调用到内核空间的。

一. 应用程序框架层日志系统Java接口的实现。

在浅谈Android系统开发中Log的使用一文中,我们曾经介绍过Android应用程序框架层日志系统的源代码接口。这里,为了描述方便和文章的完整性,我们重新贴一下这部份的代码,在frameworks/base/core/java/android/util/Log.java文件中,实现日志系统的Java接口:

  1. ................................................  
  2.   
  3. public final class Log {  
  4.   
  5. ................................................  
  6.   
  7.     /** 
  8.      * Priority constant for the println method; use Log.v. 
  9.          */  
  10.     public static final int VERBOSE = 2;  
  11.   
  12.     /** 
  13.      * Priority constant for the println method; use Log.d. 
  14.          */  
  15.     public static final int DEBUG = 3;  
  16.   
  17.     /** 
  18.      * Priority constant for the println method; use Log.i. 
  19.          */  
  20.     public static final int INFO = 4;  
  21.   
  22.     /** 
  23.      * Priority constant for the println method; use Log.w. 
  24.          */  
  25.     public static final int WARN = 5;  
  26.   
  27.     /** 
  28.      * Priority constant for the println method; use Log.e. 
  29.          */  
  30.     public static final int ERROR = 6;  
  31.   
  32.     /** 
  33.      * Priority constant for the println method. 
  34.          */  
  35.     public static final int ASSERT = 7;  
  36.   
  37. .....................................................  
  38.   
  39.     public static int v(String tag, String msg) {  
  40.         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);  
  41.     }  
  42.   
  43.     public static int v(String tag, String msg, Throwable tr) {  
  44.         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));  
  45.     }  
  46.   
  47.     public static int d(String tag, String msg) {  
  48.         return println_native(LOG_ID_MAIN, DEBUG, tag, msg);  
  49.     }  
  50.   
  51.     public static int d(String tag, String msg, Throwable tr) {  
  52.         return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));  
  53.     }  
  54.   
  55.     public static int i(String tag, String msg) {  
  56.         return println_native(LOG_ID_MAIN, INFO, tag, msg);  
  57.     }  
  58.   
  59.     public static int i(String tag, String msg, Throwable tr) {  
  60.         return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));  
  61.     }  
  62.   
  63.     public static int w(String tag, String msg) {  
  64.         return println_native(LOG_ID_MAIN, WARN, tag, msg);  
  65.     }  
  66.   
  67.     public static int w(String tag, String msg, Throwable tr) {  
  68.         return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));  
  69.     }  
  70.   
  71.     public static int w(String tag, Throwable tr) {  
  72.         return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));  
  73.     }  
  74.       
  75.     public static int e(String tag, String msg) {  
  76.         return println_native(LOG_ID_MAIN, ERROR, tag, msg);  
  77.     }  
  78.   
  79.     public static int e(String tag, String msg, Throwable tr) {  
  80.         return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));  
  81.     }  
  82.   
  83. ..................................................................  
  84.     /** @hide */ public static native int LOG_ID_MAIN = 0;  
  85.     /** @hide */ public static native int LOG_ID_RADIO = 1;  
  86.     /** @hide */ public static native int LOG_ID_EVENTS = 2;  
  87.     /** @hide */ public static native int LOG_ID_SYSTEM = 3;  
  88.   
  89.     /** @hide */ public static native int println_native(int bufID,  
  90.         int priority, String tag, String msg);  
  91. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容