LOGCAT的用法总结

logcat使用方法如下所示:
[adb] logcat [<option>] ... [<filter-spec>] ...

[options]命令包括如下选项:
-s 设置过滤器,例如指定 '*:s'
-f <filename> 输出到文件,默认情况是标准输出。
-r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f
-n <count> Sets max number of rotated logs to <count>, default 4
-v <format> 设置log的打印格式, <format> 是下面的一种:
brief process tag thread raw time threadtime long
-c 清除所有log并退出
-d 得到所有log并退出 (不阻塞)
-g 得到环形 缓冲区的大小并退出
-b <buffer> 请求不同的环形缓冲区 ('main', 'system', 'radio', 'events',默认为"-b main -b system")
-B 输出log到二进制中。

过滤器的格式是一个这样的串:
<tag>[:priority]
其中 <tag> 表示log的component, tag (或者使用 * 表示所有) , priority 从低到高如下所示:
V Verbose
D Debug
I Info
W Warn
E Error
F Fatal
S Silent   (highest priority, on which nothing is ever printed)

在运行logcat的时候在前两列的信息中你就可以看到 logcat 的标签列表和优先级别,它是这样标出的:<priority>/<tag> 

如 adb logcat ActivityManager:I MyApp:D *:S上面表达式的最后的元素 *:S ,,是设置所有的标签为”silent”,所有日志只显示有”View” and “MyApp”,如果不加*:S,那么仍然会显示其他TAG的所有信息,起不到过滤的作用.

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接口:

[java]  view plain copy
  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  *:S

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值