做者 : 萬境絕塵  轉載請著名出處android


eclipse 自帶的 LogCat 工具太垃圾了, 開始用 adb logcat 在終端查看日誌;正則表達式


1. 解析 adb logcat 的幫助信息


在命令行中輸入 adb logcat --help 命令, 就能夠顯示該命令的幫助信息;shell

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat --help  
  2. Usage: logcat [options] [filterspecs]  
  3. options include:  
  4.   -s              Set default filter to silent.  
  5.                   Like specifying filterspec '*:s'  
  6.   -f <filename>   Log to file. Default to stdout  
  7.   -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f  
  8.   -n <count>      Sets max number of rotated logs to <count>, default 4  
  9.   -v <format>     Sets the log print format, where <format> is one of:  
  10.   
  11.                   brief process tag thread raw time threadtime long  
  12.   
  13.   -c              clear (flush) the entire log and exit  
  14.   -d              dump the log and then exit (don't block)  
  15.   -t <count>      print only the most recent <count> lines (implies -d)  
  16.   -g              get the size of the log's ring buffer and exit  
  17.   -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'  
  18.                   or 'events'. Multiple -b parameters are allowed and the  
  19.                   results are interleaved. The default is -b main -b system.  
  20.   -B              output the log in binary  
  21. filterspecs are a series of   
  22.   <tag>[:priority]  
  23.   
  24. where <tag> is a log component tag (or * for all) and priority is:  
  25.   V    Verbose  
  26.   D    Debug  
  27.   I    Info  
  28.   W    Warn  
  29.   E    Error  
  30.   F    Fatal  
  31.   S    Silent (supress all output)  
  32.   
  33. '*' means '*:d' and <tag> by itself means <tag>:v  
  34.   
  35. If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.  
  36. If no filterspec is found, filter defaults to '*:I'  
  37.   
  38. If not specified with -v, format is set from ANDROID_PRINTF_LOG  
  39. or defaults to "brief"  


adb logcat 命令格式 : adb logcat [選項] [過濾項], 其中 選項 和 過濾項 在 中括號 [] 中, 說明這是可選的;緩存


(1) 選項解析


選項解析 : eclipse

-- "-s"選項 : 設置輸出日誌的標籤, 只顯示該標籤的日誌;socket

-- "-f"選項 : 將日誌輸出到文件, 默認輸出到標準輸出流中, -f 參數執行不成功;
ide

-- "-r"選項 : 按照每千字節輸出日誌, 須要 -f 參數, 不過這個命令沒有執行成功;工具

-- "-n"選項 : 設置日誌輸出的最大數目, 須要 -r 參數, 這個執行 感受 跟 adb logcat 效果同樣;post

-- "-v"選項 : 設置日誌的輸出格式, 注意只能設置一項;ui

-- "-c"選項 : 清空全部的日誌緩存信息;

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

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

-- "-g"選項 : 查看日誌緩衝區信息;

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

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


.

輸出指定標籤內容 : 

-- "-s"選項 : 設置默認的過濾器, 如 咱們想要輸出 "System.out" 標籤的信息, 就可使用 adb logcat -s System.out 命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -s System.out  
  2. --------- beginning of /dev/log/system  
  3. --------- beginning of /dev/log/main  
  4. I/System.out(22930): GSM -91  
  5. I/System.out(22930): SignalStrength issssssssss : -91  
  6. I/System.out(22930): GSM -91  
  7. I/System.out(22930): SignalStrength issssssssss : -91  
  8. I/System.out(22930): Supervisor Thread  
  9. I/System.out(22930): Got run mode  

輸出日誌信息到文件 : 

-- "-f"選項 : 該選向後面跟着輸入日誌的文件, 使用 adb logcat -f log 命令, 會出現錯誤, 這裏咱們不推薦使用該選項;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -f log  
  2. couldn't open output file: Read-only file system  
--  ">"輸出  : ">" 後面跟着要輸出的日誌文件, 能夠將 logcat 日誌輸出到文件中, 使用  adb logcat > log  命令, 使用  more log  命令查看日誌信息;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat > log  
  2. ^C  
  3. octopus@octopus:~$ more log  
  4. --------- beginning of /dev/log/system  
  5. V/ActivityManager(  500): We have pending thumbnails: null  
  6. V/ActivityManager(  500): getTasks: max=1, flags=0, receiver=null  
  7. V/ActivityManager(  500): com.android.settings/.Settings: task=TaskRecord{42392278 #448 A com.android.settings U 0}  
  8. V/ActivityManager(  500): We have pending thumbnails: null  


指定 logcat 的日誌輸出格式  : 

-- "-v"選項 : 使用 adb logcat -v time 命令, 能夠啥看日誌的輸出時間;

-- "brief"格式 : 這是默認的日誌格式 " 優先級 / 標籤 (進程ID) : 日誌信息 ", 使用 adb logcat -v prief 命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v brief  
  2. --------- beginning of /dev/log/system  
  3. D/PowerManagerService(  500): handleSandman: canDream=true, mWakefulness=Awake  
  4. D/PowerManagerService(  500): releaseWakeLockInternal: lock=1101267696, flags=0x0  
--  "process"格式  :  " 優先級 (進程ID) : 日誌信息 " , 使用  adb logcat -v process  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v process  
  2. --------- beginning of /dev/log/system  
  3. D(  500) MobileDataStateReceiver received: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED_MOBILE [wap]  (MobileDataStateTracker)  
  4. V(  500) Broadcast: Intent { act=android.intent.action.ANY_DATA_STATE_MOBILE flg=0x10 (has extras) } ordered=true userid=0  (ActivityManager)  
  5. D(  500) wap: Intent from SIM 0, current SIM 0, current DataState DISCONNECTED  (MobileDataStateTracker)  
  6. D(  500) wap: wap setting isAvailable to false  (MobileDataStateTracker)  
  7. D(  500) wap: Received state=DISCONNECTED, old=DISCONNECTED, reason=dataDetached  (MobileDataStateTracker)  
  8. D(  500) BDC-Calling finishReceiver: IIntentReceiver=41c46ba0  (ActivityThread)  
--  "tag"格式  :  " 優先級 / 標籤 : 日誌信息" , 使用  adb logcat -v tag  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v tag  
  2. --------- beginning of /dev/log/system  
  3. I/PowerManagerService: setBrightness mButtonLight 0.  
  4. D/PowerManagerService: updateScreenStateLocked: mDisplayReady=true, newScreenState=2, mWakefulness=1, mWakeLockSummary=0x1, mUserActivitySummary=0x1, mBootCompleted=true  
  5. D/PowerManagerService: handleSandman: canDream=true, mWakefulness=Awake  
--  "thread"格式  :  " 優先級 ( 進程ID : 線程ID) 標籤 : 日誌內容 " , 使用  adb logcat -v tag  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v thread  
  2. --------- beginning of /dev/log/system  
  3. V(  500: 2141) getTasks: max=1, flags=0, receiver=null  
  4. V(  500: 2141) com.lewa.launcher/.Launcher: task=TaskRecord{41dccc20 #425 A com.lewa.launcher U 0}  
  5. V(  500: 2141) We have pending thumbnails: null  
  6. V(  500: 2140) getTasks: max=1, flags=0, receiver=null  
--  "raw"格式  : 只輸出日誌信息, 不附加任何其餘 信息, 如 優先級 標籤等, 使用  adb logcat -v raw  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v raw  
  2. --------- beginning of /dev/log/system  
  3. notifications are enabled for com.kindroid.security  
  4. Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])  
  5. Native set alarm :Alarm{41e1ca00 type 3 com.kindroid.security}  
  6. reset poweroff alarm none  
--  "time"格式  "日期 時間 優先級 / 標籤 (進程ID) : 進程名稱 : 日誌信息 "  , 使用  adb logcat -v time  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v time  
  2. --------- beginning of /dev/log/system  
  3. 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  
  4. 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])  
  5. 04-25 17:18:13.158 V/NotificationService(  500): notifications are enabled for com.kindroid.security  
  6. 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])  
  7. 04-25 17:18:13.555 V/ActivityManager(  500): getTasks: max=1, flags=0, receiver=null  
--  "long"格式   " [ 日期 時間 進程ID : 線程ID 優先級 / 標籤] 日誌信息 " , 輸出以上提到的全部的頭信息, 使用  adb logcat -v long  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -v long  
  2. --------- beginning of /dev/log/system  
  3. [ 04-25 17:21:18.118   500:0x2fe V/ActivityManager ]  
  4. We have pending thumbnails: null  
  5.   
  6. [ 04-25 17:21:18.696   593:0x251 W/ActivityThread ]  
  7. Content provider com.android.providers.telephony.TelephonyProvider already published as telephony  
  8.   
  9. [ 04-25 17:21:19.119   500:0x396 V/ActivityManager ]  
  10. getTasks: max=1, flags=0, receiver=null  


清空日誌緩存信息  : 使用  adb logcat -c  命令, 能夠將以前的日誌信息清空, 從新開始輸出日誌信息;


將緩存日誌輸出  : 使用  adb logcat -d  命令, 輸出命令, 以後推出命令, 不會進行阻塞;


輸出最近的日誌 : 使用 adb logcat -t 5 命令, 能夠輸出最近的5行日誌, 而且不會阻塞;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -t 5  
  2. --------- beginning of /dev/log/system  
  3. --------- beginning of /dev/log/main  
  4. W/ADB_SERVICES(10028): adb: unable to open /proc/10028/oom_adj  
  5. D/dalvikvm(23292): threadid=11: created from interp  
  6. D/dalvikvm(23292): start new thread  
  7. D/dalvikvm(23292): threadid=11: notify debugger  
  8. D/dalvikvm(23292): threadid=11 (Thread-24538): calling run()  
  9. octopus@octopus:~$   

查看日誌緩衝區信息  : 使用  adb logcat -g  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -g  
  2. /dev/log/main: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b  
  3. /dev/log/system: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b  
  4. octopus@octopus:~$   

加載日誌緩衝區  : 使用  adb logcat -b 緩衝區類型  命令;

-- Android中的日誌緩衝區 : system緩衝區 - 與系統相關的日誌信息, radio緩衝區 - 廣播電話相關的日誌信息, events緩衝區 - 事件相關的日誌信息, main緩衝區 - 默認的緩衝區;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -b radio -t 5  
  2. D/PHONE   (23599): [GeminiDataSubUtil] UAPP_C6-4  
  3. D/GSM     (23599): [GDCT][simId1]apnType = default  
  4. D/GSM     (23599): [GDCT][simId1]isDataAllowed: not allowed due to - gprs= 1 - SIM not loaded - desiredPowerState= false  
  5. D/GSM     (23599): [GDCT][simId1]isDataPossible(default): possible=false isDataAllowed=false apnTypePossible=true apnContextisEnabled=true apnContextState()=IDLE  
  6. I/MUXD    (23591): [gsm0710muxd] 3426:main(): Frames received/dropped: 18242/0  
  7. octopus@octopus:~$   
  8. octopus@octopus:~$ adb logcat -b main -t 5  
  9. D/NotificationService(  500): notification.sound=null  
  10. D/NotificationService(  500): mDmLock=false  
  11. I/ATCIJ   (16576): Couldn't find 'atci-serv-fw' socket; retrying after timeout  
  12. W/ADB_SERVICES(  246): create_local_service_socket() name=shell:export ANDROID_LOG_TAGS="" ; exec logcat -b main -t 5  
  13. W/ADB_SERVICES(16815): adb: unable to open /proc/16815/oom_adj  
  14. octopus@octopus:~$   
  15. octopus@octopus:~$ adb logcat -b system -t 5  
  16. D/PowerManagerService(  500): updateScreenStateLocked: mDisplayReady=true, newScreenState=0, mWakefulness=0, mWakeLockSummary=0x1, mUserActivitySummary=0x0, mBootCompleted=true  
  17. D/PowerManagerService(  500): handleSandman: canDream=false, mWakefulness=Asleep  
  18. 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])  
  19. V/NotificationService(  500): notifications are enabled for com.kindroid.security  
  20. 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])  
  21. octopus@octopus:~$   
  22. octopus@octopus:~$ adb logcat -b event -t 5  
  23. Unable to open log device '/dev/log/event': No such file or directory  
  24. octopus@octopus:~$ adb logcat -b events -t 5  
  25. I/notification_cancel(  500): [com.kindroid.security,1026,NULL,0,0,64]  
  26. 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])]  
  27. I/notification_cancel(  500): [com.kindroid.security,1026,NULL,0,0,64]  
  28. 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])]  
  29. I/notification_cancel(  500): [com.kindroid.security,1026,NULL,0,0,64]  
  30. octopus@octopus:~$   

以二進制形式輸出日誌  : 使用  adb logcat -B  命令;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat -B  -t 5  
  2. O��_�3ZS�4gps_mt3326nmea_reader_parse: line = 1218GPS get accuracy failed, fix mode:1  
  3. ^��_�3ZS�=gps_mt3326nmea_reader_addc: line = 1331the structure include nmea_cb address is 0x658cc8e8  
  4. H��_�3ZSEGEgps_mt3326nmea_reader_addc: line = 1332nmea_cb address is 0x5d2fe279  
  5. i���3ZS�)>ADB_SERVICEScreate_local_service_socket() name=shell:export ANDROID_LOG_TAGS="" ; exec logcat -B -t 5  
  6. 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 以上級別的日誌;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat *:E  
  2.   
  3. Note: log switch off, only log_main and log_events will have logs!  
  4. --------- beginning of /dev/log/main  
  5. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  6. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  7. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  8. E/dalvikvm(  756): GC_CONCURRENT freed 1809K, 27% free 19489K/26695K, paused 16ms+5ms, total 109ms  
  9. E/WifiHW  (  441): wifi_send_command : SCAN ; interface index=0;  
  10. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  11. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  12. E/dalvikvm(  756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 16ms+3ms, total 102ms  
  13. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  14. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  


過濾指定標籤等級日誌 : 使用 adb logcat WifiHW:D *:S 命令進行過濾;

-- 命令含義 : 輸出10條日誌, 日誌是 標籤爲 WifiHW, 而且優先級 Debug(調試) 等級以上的級別的日誌;

-- 注意 *:S : 若是沒有 *S 就會輸出錯誤;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat WifiHW:D *:S  
  2.   
  3. Note: log switch off, only log_main and log_events will have logs!  
  4. --------- beginning of /dev/log/main  
  5. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  6. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  7. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  8. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  9. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  10. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  11. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  



能夠同時設置多個過濾器 : 使用 adb logcat WifiHW:D dalvikvm:I *:S 命令, 輸出 WifiHW 標籤 的 Debug 以上級別 和 dalvikvm 標籤的 Info 以上級別的日誌;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat WifiHW:D dalvikvm:I *:S   
  2.   
  3. Note: log switch off, only log_main and log_events will have logs!  
  4. --------- beginning of /dev/log/main  
  5. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  6. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  7. E/dalvikvm(  756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 17ms+2ms, total 110ms  
  8. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  9. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  10. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  11. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  12. E/dalvikvm(  756): GC_CONCURRENT freed 1810K, 27% free 19489K/26695K, paused 17ms+5ms, total 108ms  
  13. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  14. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  


2. 使用管道過濾日誌


(1) 過濾固定字符串


過濾固定字符串 : 只要命令行出現的日誌均可以過濾, 無論是否是標籤;

-- 命令 : adb logcat | grep Wifi ;

[plain]  view plain copy
  1. octopus@octopus:~$ adb logcat | grep Wifi  
  2. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  3. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  4. E/WifiHW  (  441): wifi_send_command : SCAN ; interface index=0;  
  5. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  6. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  7. E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
  8. E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  


過濾字符串忽略大小寫 : adb logcat | grep -i wifi ;




(2) 使用正則表達式匹配


分析日誌 : 該日誌開頭兩個字符是 "V/", 後面開始就是標籤, 寫一個正則表達式 "^..ActivityManager", 就能夠匹配日誌中的 "V/ActivityManager" 字符串;

[plain]  view plain copy
  1. V/ActivityManager(  574): getTasks: max=1, flags=0, receiver=null  


正則表達式過濾日誌 : 使用上面的正則表達式組成命令 adb logcat | grep "^..Activity" ;



做者 : 萬境絕塵  轉載請著名出處

版權聲明:若無特殊註明,本文皆爲( hanshuliang )原創,轉載請保留文章出處。