eclipse 自帶的 LogCat 工具太垃圾了, 開始用 adb logcat 在終端查看日志;
1. 解析 adb logcat 的幫助信息
在命令行中輸入 adb logcat --help 命令, 就可以顯示該命令的幫助信息;
- octopus@octopus:~$ 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"
adb logcat 命令格式 : adb logcat [選項] [過濾項], 其中 選項 和 過濾項 在 中括號 [] 中, 說明這是可選的;
(1) 選項解析
選項解析 :
-- "-s"選項 : 設置輸出日志的標簽, 只顯示該標簽的日志;
--"-f"選項 : 將日志輸出到文件, 默認輸出到標准輸出流中, -f 參數執行不成功;
--"-r"選項 : 按照每千字節輸出日志, 需要 -f 參數, 不過這個命令沒有執行成功;
--"-n"選項 : 設置日志輸出的最大數目, 需要 -r 參數, 這個執行 感覺 跟 adb logcat 效果一樣;
--"-v"選項 : 設置日志的輸出格式, 注意只能設置一項;
--"-c"選項 : 清空所有的日志緩存信息;
--"-d"選項 : 將緩存的日志輸出到屏幕上, 並且不會阻塞;
--"-t"選項 : 輸出最近的幾行日志, 輸出完退出, 不阻塞;
--"-g"選項 : 查看日志緩沖區信息;
--"-b"選項 : 加載一個日志緩沖區, 默認是 main, 下面詳解;
--"-B"選項 : 以二進制形式輸出日志;
.
輸出指定標簽內容 :
-- "-s"選項 : 設置默認的過濾器, 如 我們想要輸出 "System.out" 標簽的信息, 就可以使用adb logcat -s System.out 命令;
- octopus@octopus:~$ adb logcat -s System.out
- --------- beginning of /dev/log/system
- --------- beginning of /dev/log/main
- I/System.out(22930): GSM -91
- I/System.out(22930): SignalStrength issssssssss : -91
- I/System.out(22930): GSM -91
- I/System.out(22930): SignalStrength issssssssss : -91
- I/System.out(22930): Supervisor Thread
- I/System.out(22930): Got run mode
輸出日志信息到文件 :
-- "-f"選項 : 該選向后面跟着輸入日志的文件, 使用adb logcat -f /sdcard/log.txt 命令, 注意這個log文件是輸出到手機上,需要指定合適的路徑。
- octopus@octopus:~$ adb logcat -f /sdcard/log.txt
這個參數對對不能一直用電腦連着手機收集日志的場景非常有用,其實android shell下也有一個相同參數的logcat命令。使用如下命令可以執行后斷開PC和手機持續收集LOG。
- shell@pc$ adb shell
- shell@android$ logcat -f /sdcard/log.txt & #這里的&符號表示后台執行,別少了。
- shell@android$ exit
注:
(1)以上shell@pc$ 指在pc的shell終端執行后邊的命令, shell@android$ 表示在手機shell中執行后邊的命令l
(2)一定注意合適的時候需要停止掉以上命令,否則再次使用相同命令的時候,就會有兩個logcat寫同一個文件了
停止方法: adb shell kill -9 <logcat_pid>
其中logcat_pid 通過 如下命令獲取
adb shell ps | grep logcat # linux 平台
adb shell ps | findstr "logcat" #Windows平台
-- ">"輸出 : ">" 后面跟着要輸出的日志文件, 可以將 logcat 日志輸出到文件中, 使用adb logcat > log 命令, 使用more log 命令查看日志信息;
- octopus@octopus:~$ adb logcat > log
- ^C
- octopus@octopus:~$ more log
- --------- beginning of /dev/log/system
- V/ActivityManager( 500): We have pending thumbnails: null
- V/ActivityManager( 500): getTasks: max=1, flags=0, receiver=null
- V/ActivityManager( 500): com.android.settings/.Settings: task=TaskRecord{42392278 #448 A com.android.settings U 0}
- V/ActivityManager( 500): We have pending thumbnails: null
-- " -d -f <log>" 組合命令:可以將日志保存到手機上的指定位置,對不能一直用電腦連着手機收集日志的場景非常有用。
- adb logcat -d -v /sdcard/mylog.txt
指定 logcat 的日志輸出格式 :
-- "-v"選項 : 使用adb logcat -v time 命令, 可以啥看日志的輸出時間;
使用adb logcat -v threadtime 命令, 可以啥看日志的輸出時間和線程信息;
-- "brief"格式 : 這是默認的日志格式" 優先級 / 標簽 (進程ID) : 日志信息 ", 使用adb logcat -v prief 命令;
- octopus@octopus:~$ adb logcat -v brief
- --------- beginning of /dev/log/system
- D/PowerManagerService( 500): handleSandman: canDream=true, mWakefulness=Awake
- D/PowerManagerService( 500): releaseWakeLockInternal: lock=1101267696, flags=0x0
-- "process"格式 : " 優先級 (進程ID) : 日志信息 ", 使用adb logcat -v process 命令;
- octopus@octopus:~$ adb logcat -v process
- --------- beginning of /dev/log/system
- D( 500) MobileDataStateReceiver received: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED_MOBILE [wap] (MobileDataStateTracker)
- V( 500) Broadcast: Intent { act=android.intent.action.ANY_DATA_STATE_MOBILE flg=0x10 (has extras) } ordered=true userid=0 (ActivityManager)
- D( 500) wap: Intent from SIM 0, current SIM 0, current DataState DISCONNECTED (MobileDataStateTracker)
- D( 500) wap: wap setting isAvailable to false (MobileDataStateTracker)
- D( 500) wap: Received state=DISCONNECTED, old=DISCONNECTED, reason=dataDetached (MobileDataStateTracker)
- D( 500) BDC-Calling finishReceiver: IIntentReceiver=41c46ba0 (ActivityThread)
-- "tag"格式 : " 優先級 / 標簽 : 日志信息", 使用adb logcat -v tag 命令;
- octopus@octopus:~$ adb logcat -v tag
- --------- beginning of /dev/log/system
- I/PowerManagerService: setBrightness mButtonLight 0.
- D/PowerManagerService: updateScreenStateLocked: mDisplayReady=true, newScreenState=2, mWakefulness=1, mWakeLockSummary=0x1, mUserActivitySummary=0x1, mBootCompleted=true
- D/PowerManagerService: handleSandman: canDream=true, mWakefulness=Awake
-- "thread"格式 : " 優先級 ( 進程ID : 線程ID) 標簽 : 日志內容 ", 使用adb logcat -v tag 命令;
- octopus@octopus:~$ adb logcat -v thread
- --------- beginning of /dev/log/system
- V( 500: 2141) getTasks: max=1, flags=0, receiver=null
- V( 500: 2141) com.lewa.launcher/.Launcher: task=TaskRecord{41dccc20 #425 A com.lewa.launcher U 0}
- V( 500: 2141) We have pending thumbnails: null
- V( 500: 2140) getTasks: max=1, flags=0, receiver=null
-- "raw"格式 : 只輸出日志信息, 不附加任何其他 信息, 如 優先級 標簽等, 使用adb logcat -v raw 命令;
- octopus@octopus:~$ adb logcat -v raw
- --------- beginning of /dev/log/system
- notifications are enabled for com.kindroid.security
- Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])
- Native set alarm :Alarm{41e1ca00 type 3 com.kindroid.security}
- reset poweroff alarm none
-- "time"格式 : "日期 時間 優先級 / 標簽 (進程ID) : 進程名稱 : 日志信息 ", 使用adb logcat -v time 命令;
- octopus@octopus:~$ adb logcat -v time
- --------- beginning of /dev/log/system
- 04-25 17:18:13.019 V/ActivityManager( 500): Broadcast sticky: Intent { act=android.intent.action.SIG_STR flg=0x10 (has extras) } ordered=false userid=-1
- 04-25 17:18:13.157 V/NotificationService( 500): enqueueNotificationInternal: pkg=com.kindroid.security id=1020 notification=Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])
- 04-25 17:18:13.158 V/NotificationService( 500): notifications are enabled for com.kindroid.security
- 04-25 17:18:13.158 V/NotificationService( 500): Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])
- 04-25 17:18:13.555 V/ActivityManager( 500): getTasks: max=1, flags=0, receiver=null
-- "long"格式:" [ 日期 時間 進程ID : 線程ID 優先級 / 標簽] 日志信息 ", 輸出以上提到的所有的頭信息, 使用adb logcat -v long 命令;
- octopus@octopus:~$ adb logcat -v long
- --------- beginning of /dev/log/system
- [ 04-25 17:21:18.118 500:0x2fe V/ActivityManager ]
- We have pending thumbnails: null
- [ 04-25 17:21:18.696 593:0x251 W/ActivityThread ]
- Content provider com.android.providers.telephony.TelephonyProvider already published as telephony
- [ 04-25 17:21:19.119 500:0x396 V/ActivityManager ]
- getTasks: max=1, flags=0, receiver=null
清空日志緩存信息 : 使用 adb logcat -c 命令, 可以將之前的日志信息清空, 重新開始輸出日志信息;
將緩存日志輸出 : 使用 adb logcat -d 命令, 輸出命令, 之后推出命令, 不會進行阻塞;
輸出最近的日志 : 使用adb logcat -t 5 命令, 可以輸出最近的5行日志, 並且不會阻塞;
- octopus@octopus:~$ adb logcat -t 5
- --------- beginning of /dev/log/system
- --------- beginning of /dev/log/main
- W/ADB_SERVICES(10028): adb: unable to open /proc/10028/oom_adj
- D/dalvikvm(23292): threadid=11: created from interp
- D/dalvikvm(23292): start new thread
- D/dalvikvm(23292): threadid=11: notify debugger
- D/dalvikvm(23292): threadid=11 (Thread-24538): calling run()
- octopus@octopus:~$
查看日志緩沖區信息 : 使用 adb logcat -g 命令;
- octopus@octopus:~$ adb logcat -g
- /dev/log/main: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b
- /dev/log/system: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b
- octopus@octopus:~$
加載日志緩沖區 : 使用 adb logcat -b 緩沖區類型 命令;
-- Android中的日志緩沖區 : system緩沖區 - 與系統相關的日志信息, radio緩沖區 - 廣播電話相關的日志信息, events緩沖區 - 事件相關的日志信息, main緩沖區 - 默認的緩沖區;
- octopus@octopus:~$ adb logcat -b radio -t 5
- D/PHONE (23599): [GeminiDataSubUtil] UAPP_C6-4
- D/GSM (23599): [GDCT][simId1]apnType = default
- D/GSM (23599): [GDCT][simId1]isDataAllowed: not allowed due to - gprs= 1 - SIM not loaded - desiredPowerState= false
- D/GSM (23599): [GDCT][simId1]isDataPossible(default): possible=false isDataAllowed=false apnTypePossible=true apnContextisEnabled=true apnContextState()=IDLE
- I/MUXD (23591): [gsm0710muxd] 3426:main(): Frames received/dropped: 18242/0
- octopus@octopus:~$
- octopus@octopus:~$ adb logcat -b main -t 5
- D/NotificationService( 500): notification.sound=null
- D/NotificationService( 500): mDmLock=false
- I/ATCIJ (16576): Couldn't find 'atci-serv-fw' socket; retrying after timeout
- W/ADB_SERVICES( 246): create_local_service_socket() name=shell:export ANDROID_LOG_TAGS="" ; exec logcat -b main -t 5
- W/ADB_SERVICES(16815): adb: unable to open /proc/16815/oom_adj
- octopus@octopus:~$
- octopus@octopus:~$ adb logcat -b system -t 5
- D/PowerManagerService( 500): updateScreenStateLocked: mDisplayReady=true, newScreenState=0, mWakefulness=0, mWakeLockSummary=0x1, mUserActivitySummary=0x0, mBootCompleted=true
- D/PowerManagerService( 500): handleSandman: canDream=false, mWakefulness=Asleep
- V/NotificationService( 500): enqueueNotificationInternal: pkg=com.kindroid.security id=1020 notification=Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])
- V/NotificationService( 500): notifications are enabled for com.kindroid.security
- V/NotificationService( 500): Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])
- octopus@octopus:~$
- octopus@octopus:~$ adb logcat -b event -t 5
- Unable to open log device '/dev/log/event': No such file or directory
- octopus@octopus:~$ adb logcat -b events -t 5
- I/notification_cancel( 500): [com.kindroid.security,1026,NULL,0,0,64]
- I/notification_enqueue( 500): [com.kindroid.security,1020,NULL,0,Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])]
- I/notification_cancel( 500): [com.kindroid.security,1026,NULL,0,0,64]
- I/notification_enqueue( 500): [com.kindroid.security,1020,NULL,0,Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])]
- I/notification_cancel( 500): [com.kindroid.security,1026,NULL,0,0,64]
- octopus@octopus:~$
以二進制形式輸出日志 : 使用 adb logcat -B 命令;
- octopus@octopus:~$ adb logcat -B -t 5
- O��_�3ZS�4gps_mt3326nmea_reader_parse: line = 1218GPS get accuracy failed, fix mode:1
- ^��_�3ZS�=gps_mt3326nmea_reader_addc: line = 1331the structure include nmea_cb address is 0x658cc8e8
- H��_�3ZSEGEgps_mt3326nmea_reader_addc: line = 1332nmea_cb address is 0x5d2fe279
- i���3ZS�)>ADB_SERVICEScreate_local_service_socket() name=shell:export ANDROID_LOG_TAGS="" ; exec logcat -B -t 5
- 7*E*E�3ZSo�YADB_SERVICESadb: unable to open /proc/17706/oom_adj
(2) 過濾項解析
過濾項格式 : <tag>[:priority] , 標簽:日志等級, 默認的日志過濾項是 " *:I " ;
-- V : Verbose (明細);
-- D : Debug (調試);
-- I : Info (信息);
-- W : Warn (警告);
-- E : Error (錯誤);
-- F: Fatal (嚴重錯誤);
-- S : Silent(Super all output) (最高的優先級, 可能不會記載東西);
過濾指定等級日志 : 使用 adb logcat 10 *:E 命令, 顯示 Error 以上級別的日志;
- octopus@octopus:~$ adb logcat *:E
- Note: log switch off, only log_main and log_events will have logs!
- --------- beginning of /dev/log/main
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/dalvikvm( 756): GC_CONCURRENT freed 1809K, 27% free 19489K/26695K, paused 16ms+5ms, total 109ms
- E/WifiHW ( 441): wifi_send_command : SCAN ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/dalvikvm( 756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 16ms+3ms, total 102ms
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
過濾指定標簽等級日志 : 使用 adb logcat WifiHW:D *:S 命令進行過濾;
-- 命令含義 : 輸出10條日志, 日志是 標簽為 WifiHW, 並且優先級 Debug(調試) 等級以上的級別的日志;
--注意 *:S : 如果沒有 *S 就會輸出錯誤;
- octopus@octopus:~$ adb logcat WifiHW:D *:S
- Note: log switch off, only log_main and log_events will have logs!
- --------- beginning of /dev/log/main
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
可以同時設置多個過濾器 : 使用adb logcat WifiHW:D dalvikvm:I *:S 命令, 輸出 WifiHW 標簽 的 Debug 以上級別 和 dalvikvm 標簽的 Info 以上級別的日志;
- octopus@octopus:~$ adb logcat WifiHW:D dalvikvm:I *:S
- Note: log switch off, only log_main and log_events will have logs!
- --------- beginning of /dev/log/main
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/dalvikvm( 756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 17ms+2ms, total 110ms
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/dalvikvm( 756): GC_CONCURRENT freed 1810K, 27% free 19489K/26695K, paused 17ms+5ms, total 108ms
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
2. 使用管道過濾日志
(1) 過濾固定字符串
過濾固定字符串 : 只要命令行出現的日志都可以過濾, 不管是不是標簽;
-- 命令 : adb logcat | grep Wifi ;
- octopus@octopus:~$ adb logcat | grep Wifi
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : AP_SCAN 1 ; interface index=0;
- E/WifiHW ( 441): wifi_send_command : SCAN_RESULTS ; interface index=0;
過濾字符串忽略大小寫 : adb logcat | grep -i wifi ;
(2) 使用正則表達式匹配
分析日志 : 該日志開頭兩個字符是 "V/", 后面開始就是標簽, 寫一個正則表達式 "^..ActivityManager", 就可以匹配日志中的 "V/ActivityManager" 字符串;
- V/ActivityManager( 574): getTasks: max=1, flags=0, receiver=null
正則表達式過濾日志: 使用上面的正則表達式組成命令 adb logcat | grep "^..Activity" ;