Android logcat 命令以及 Log机制


Android logcat命令

    1. logcat -c 清除已有log信息

  2.logcat -b main 显示主缓冲区的log

  logcat -b radio 显示无线缓冲区的log

  logcat -b events 显示事件缓冲区的log

  3.logcat -f [filename] 将log保存到指定的文件中,例如 logcat -b radio -f /data/radio.log

  4.logcat -v 设置logcat输出的格式

  主要有7种输出格式:

  1. brief — Display priority/tag and PID of originating process (the default format).

  2. process — Display PID only.

  3. tag — Display the priority/tag only.

  4. thread — Display process:thread and priority/tag only.

  5. raw — Display the raw log message, with no other metadata fields.

  6. time — Display the date, invocation time, priority/tag, and PID of the originating process.

  7. long — Display all metadata fields and separate messages with a blank lines.

  比较常用的是显示时间:logcat -v time &

  5.logcat -g 查看缓冲区的大小

  logcat -g main

  logcat -g radio

  logcat -g events



Log机制的一些理解:


1. 系统结构

应用程序调用应用程序框架层的Java接口(android.util.Log)来使用日志系统,这个Java接口通过JNI方法和系统运行库最终调用内核驱动程序Logger把Log写到内核空间中。


2. 关键代码及理解

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

代码位置:frameworks/base/core/java/android/util/Log.java文件中,实现日志系统的Java接口:

  1. public final class Log {  
  2.   
  3. /** 
  4.      * Priority constant for the println method; use Log.v. 
  5.      */  
  6.     public static final int VERBOSE = 2;  
  7.   
  8.     /** 
  9.      * Priority constant for the println method; use Log.d. 
  10.      */  
  11.     public static final int DEBUG = 3;  
  12.   
  13.     /** 
  14.      * Priority constant for the println method; use Log.i. 
  15.      */  
  16.     public static final int INFO = 4;  
  17.   
  18.     /** 
  19.      * Priority constant for the println method; use Log.w. 
  20.      */  
  21.     public static final int WARN = 5;  
  22.   
  23.     /** 
  24.      * Priority constant for the println method; use Log.e. 
  25.      */  
  26.     public static final int ERROR = 6;  
  27.   
  28.     /** 
  29.      * Priority constant for the println method. 
  30.      */  
  31.     public static final int ASSERT = 7;  
  32.   
  33.     ........................  
  34.     
  35.     public static int v(String tag, String msg) {  
  36.         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);  
  37.     }  
  38.   
  39.     public static int d(String tag, String msg) {  
  40.         return println_native(LOG_ID_MAIN, DEBUG, tag, msg);  
  41.     }  
  42.   
  43.     public static int i(String tag, String msg) {  
  44.         return println_native(LOG_ID_MAIN, INFO, tag, msg);  
  45.     }  
  46.   
  47.     public static int w(String tag, String msg) {  
  48.         return println_native(LOG_ID_MAIN, WARN, tag, msg);  
  49.     }  
  50.   
  51.     public static int e(String tag, String msg) {  
  52.         return println_native(LOG_ID_MAIN, ERROR, tag, msg);  
  53.     }  
  54.   
  55.     public static int println(int priority, String tag, String msg) {  
  56.         return println_native(LOG_ID_MAIN, priority, tag, msg);  
  57.     }  
  58.   
  59.     ...................  
  60.   
  61.     /** @hide */ public static final int LOG_ID_MAIN = 0;  
  62.     /** @hide */ public static final int LOG_ID_RADIO = 1;  
  63.     /** @hide */ public static final int LOG_ID_EVENTS = 2;  
  64.     /** @hide */ public static final int LOG_ID_SYSTEM = 3;  
  65.   
  66.     /** @hide */ public static native int println_native(int bufID,  
  67.             int priority, String tag, String msg);  
  68. }  
其中 VERBOSE ,DEBUG, INFO,WARN,ERROR,ASSERT代表Log优先级,分别由对应的v,d,i,w,e,a方法实现写入。

在logcat中用 

logcat  ActivityManager:w  

命令显示ActivityManager标签中等于或高于WARN级别的Log


其中

  1. <span style="font-size: 13px; line-height: 19px; "></span><pre name="code" class="java" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">LOG_ID_MAIN = 0  LOG_ID_RADIO = 1  LOG_ID_EVENTS = 2 LOG_ID_SYSTEM = 3;  
为日志缓冲区,在底层定义了3个设备文件分别为:/dev/log/main、/dev/log/events和/dev/log/radio(),第4个日志缓冲区LOG_ID_SYSTEM并没有对应的设备文件,在这种情况下,它和LOG_ID_MAIN对应同一个缓冲区ID(为什么这样下面会提到)。 

在整个Log接口中,最关键的地方声明了println_native本地方法,所有的Log接口都是通过调用这个本地方法来实现Log的注入。下面我们就继续分析这个本地方法println_native。

  • 1
  • 2
  • 下一页

相关内容