android——adb logcat常用命令總結

分類:編程 時間:2016-11-06
[摘要:1、檢察adb logcat贊助疑息 C:\Users\000>adb logcat --help Usage: logcat [options] [filterspecs] options include: -s Set defau]

1、查看adb logcat幫助信息

C:\Users\000>adb logcat --help
Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:s'
  -f <filename>   Log to file. Default to stdout
  -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>     Sets the log print format, where <format> is one of:

                  brief process tag thread raw time threadtime long

  -c              clear (flush) the entire log and exit
  -d              dump the log and then exit (don't block)
  -t <count>      print only the most recent <count> lines (implies -d)
  -g              get the size of the log's ring buffer and exit
  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'
                  or 'events'. Multiple -b parameters are allowed and the
                  results are interleaved. The default is -b main -b system.
  -B              output the log in binary
filterspecs are a series of
  <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (supress all output)

'*' means '*:d' and <tag> by itself means <tag>:v

If not specified on the commandline, filterspec is set from android_LOG_TAGS.
If no filterspec is found, filter defaults to '*:I'

If not specified with -v, format is set from ANDROID_PRINTF_LOG
or defaults to "brief"

2、解析選項

– “-s”選項 : 設置輸出日誌的標簽, 只顯示該標簽的日誌;

–”-f”選項 : 將日誌輸出到文件, 默認輸出到標準輸出流中;

–”-r”選項 : 按照每千字節輸出日誌, 需要 -f 參數;

–”-n”選項 : 設置日誌輸出的最大數目, 需要 -r 參數;

–”-v”選項 : 設置日誌的輸出格式, 註意只能設置一項;

–”-c”選項 : 清空所有的日誌緩存信息;

–”-d”選項 : 將緩存的日誌輸出到屏幕上, 並且不會阻塞;

–”-t”選項 : 輸出最近的幾行日誌, 輸出完退出, 不阻塞;

–”-g”選項 : 查看日誌緩沖區信息;

–”-b”選項 : 加載一個日誌緩沖區, 默認是 main, 下面詳解;

–”-B”選項 : 以二進制形式輸出日誌;

3、輸出指定標簽的日誌

方式1:設置默認的過濾器,-s選項+標簽名
adb logcat -s TAG標簽名 TAG標簽名
如:

adb logcat -s VideoFragment VLCVideoPlayerActivity

方式2:自定義過濾器,指定日誌級別
LEVEL可以選擇:

– V : Verbose (明細);
– D : Debug (調試);
– I : Info (信息);
– W : Warn (警告);
– E : Error (錯誤);
– F: Fatal (嚴重錯誤);
– S : Silent(Super all output) (最高的優先級, 可能不會記載東西);

TAG:X 的作用為: 輸出標簽為TAG的log級別大於X的信息,例如:

adb logcat VideoFragment:D

輸出 VideoFragment的D 和D級別以上的log,包括 D, I, W, E,F, S
註意:
(1)可以指定多個[TAG:LEVEL ]
(2) level : S 表示為不輸出該標簽的日誌,應為沒有大於S級別的日誌了
(3) [TAG:LEVEL ] 不會影響其他標簽的日誌, 所以如果要屏蔽其他log請使用 *:S,例如:

adb logcat VideoFragment:D VLCVideoPlayerActivity:D *:S

方式3:使用管道過濾日誌
linux系統下:

adb logcat | grep -e "VideoFragment\|VLCVideoPlayerActivity"
adb logcat | grep -e "VideoFragment|VLCVideoPlayerActivity"
adb logcat |grep -e VideoFragment -e VLCVideoPlayerActivity

Windows系統下:
多個條件直接用空格

adb logcat | findstr "VideoFragment VLCVideoPlayerActivity"

4、輸出日誌信息到文件

方式1:-f選項

adb logcat -f /sdcard/log.txt 

方式2:輸出重定向

adb logcat > /sdcard/log.txt  

後臺執行

logcat -f /sdcard/log.txt &   #這裏的&符號表示後臺執行,不能少

註意合適的時候需要停止掉以上命令,必須有多個logcat在寫同一個文件。
停止方法:

adb shell kill -9 <logcat_pid> 

其中logcat_pid 通過 如下命令獲取

adb shell ps | grep logcat          # linux 平臺
adb shell ps | findstr "logcat"    #Windows平臺

5、-v 日誌常用輸出格式

1、“tag”格式 : ” 優先級 / 標簽 : 日誌信息”,例如:

C:\Users\000>adb logcat -v tag -s VideoFragment
D/VideoFragment: ====== onCreate
D/VideoFragment: =======bindVideoPlayerService
D/VideoFragment: updateAdapterTask doInBackground
D/VideoFragment: ====== onStart
D/VideoFragment: UpdateAdapterTask onPostExecute
D/VideoFragment: =====UpdateAdapterTask notifyDataSetChanged
D/VideoFragment: =====  PlayerService onServiceConnected

2、“time”格式 : “日期 時間 優先級 / 標簽 (進程ID) : 進程名稱 : 日誌信息 “, 例如:

C:\Users\000>adb logcat -v time -s VideoFragment
04-25 15:04:39.591 D/VideoFragment( 5859): ====== onCreate
04-25 15:04:39.601 D/VideoFragment( 5859): =======bindVideoPlayerService
04-25 15:04:39.621 D/VideoFragment( 5859): UpdateAdapterTask doInBackground
04-25 15:04:39.621 D/VideoFragment( 5859): ====== onStart
04-25 15:04:39.641 D/VideoFragment( 5859): UpdateAdapterTask onPostExecute
04-25 15:04:39.641 D/VideoFragment( 5859): =====UpdateAdapterTask notifyDataSetChanged
04-25 15:04:39.781 D/VideoFragment( 5859): =====PlayerService onServiceConnected

6、日誌緩沖區

C:\Users\000>adb shell
root@rk3288:/ # ll /dev/log
crw-rw-rw- root     log       10,  46 2016-04-25 10:50 events #事件相關的日誌信息
crw-rw-rw- root     log       10,  47 2016-04-25 10:50 main #默認的緩沖區
crw-rw-rw- root     log       10,  45 2016-04-25 10:50 radio #廣播電話相關的日誌信息
crw-rw-rw- root     log       10,  44 2016-04-25 10:50 system #與系統相關的日誌信息

查看日誌緩沖區信息:

C:\Users\000>adb logcat -g
/dev/log/main: ring buffer is 256Kb (40Kb consumed), max entry is 5120b, max pay
load is 4076b
/dev/log/system: ring buffer is 256Kb (11Kb consumed), max entry is 5120b, max p
ayload is 4076b

清空日誌緩沖區:

C:\Users\000>adb logcat -c

C:\Users\000>adb logcat -g
/dev/log/main: ring buffer is 256Kb (0Kb consumed), max entry is 5120b, max payl
oad is 4076b
/dev/log/system: ring buffer is 256Kb (0Kb consumed), max entry is 5120b, max pa
yload is 4076b

Tags:

文章來源:


ads
ads

相關文章
ads

相關文章

ad