App測試-logcat

1核芽、解釋adb logcat的幫助信息

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

C:\Program Files (x86)\Nox\bin>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 [選項(xiàng)] [過濾器]谜喊,其中 選項(xiàng) 和 過濾項(xiàng) 在 中括號 [] 中, 說明這是可選的;

1) 選項(xiàng)解析

  • 選項(xiàng)解析 :

-- "-s"選項(xiàng) : 設(shè)置輸出日志的標(biāo)簽, 只顯示該標(biāo)簽的日志;

--"-f"選項(xiàng) : 將日志輸出到文件, 默認(rèn)輸出到標(biāo)準(zhǔn)輸出流中, -f 參數(shù)執(zhí)行不成功;

--"-r"選項(xiàng) : 按照每千字節(jié)輸出日志, 需要 -f 參數(shù), 不過這個命令沒有執(zhí)行成功;

--"-n"選項(xiàng) : 設(shè)置日志輸出的最大數(shù)目, 需要 -r 參數(shù), 這個執(zhí)行 感覺 跟 adb logcat 效果一樣;

--"-v"選項(xiàng) : 設(shè)置日志的輸出格式, 注意只能設(shè)置一項(xiàng);

--"-c"選項(xiàng) : 清空所有的日志緩存信息;

--"-d"選項(xiàng) : 將緩存的日志輸出到屏幕上, 并且不會阻塞;

--"-t"選項(xiàng) : 輸出最近的幾行日志, 輸出完退出, 不阻塞;

--"-g"選項(xiàng) : 查看日志緩沖區(qū)信息;

--"-b"選項(xiàng) : 加載一個日志緩沖區(qū), 默認(rèn)是 main, 下面詳解;

--"-B"選項(xiàng) : 以二進(jìn)制形式輸出日志;

  • 輸出指定標(biāo)簽內(nèi)容 :
    -- "-s"選項(xiàng) :
    設(shè)置默認(rèn)的過濾器, 如 我們想要輸出 "System.out" 標(biāo)簽的信息, 就可以使用
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"選項(xiàng) :
    該選向后面跟著輸入日志的文件, 使用adb logcat -f /sdcard/log.txt 命令, 注意這個log文件是輸出到手機(jī)上枢赔,需要指定合適的路徑。
adb logcat -f /sdcard/log.txt   

這個參數(shù)對對不能一直用電腦連著手機(jī)收集日志的場景非常有用煌往,其實(shí)android shell下也有一個相同參數(shù)的logcat命令倾哺。使用如下命令可以執(zhí)行后斷開PC和手機(jī)持續(xù)收集LOG。

shell@pc$ adb shell  
shell@android$ logcat -f /sdcard/log.txt &   
#這里的&符號表示后臺執(zhí)行刽脖,別少了羞海。  
shell@android$ exit  

注:

(1)以上shell@pc$ 指在pc的shell終端執(zhí)行后邊的命令, shell@android$ 表示在手機(jī)shell中執(zhí)行后邊的命令l

(2)一定注意合適的時(shí)候需要停止掉以上命令曲管,否則再次使用相同命令的時(shí)候却邓,就會有兩個logcat寫同一個文件了

      停止方法:  adb shell kill -9 <logcat_pid>         

       其中l(wèi)ogcat_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>" 組合命令:
可以將日志保存到手機(jī)上的指定位置,對不能一直用電腦連著手機(jī)收集日志的場景非常有用院水。

adb logcat -d -v /sdcard/mylog.txt  
  • 指定 logcat 的日志輸出格式 :
    -- "-v"選項(xiàng) :
    使用adb logcat -v time 命令, 可以啥看日志的輸出時(shí)間; 使用adb logcat -v threadtime 命令, 可以啥看日志的輸出時(shí)間和線程信息;

-- "brief"格式 :
這是默認(rèn)的日志格式" 優(yōu)先級 / 標(biāo)簽 (進(jìn)程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"格式 :
" 優(yōu)先級 (進(jìn)程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"格式 :
" 優(yōu)先級 / 標(biāo)簽 : 日志信息", 使用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"格式 :
" 優(yōu)先級 ( 進(jìn)程ID : 線程ID) 標(biāo)簽 : 日志內(nèi)容 ", 使用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"格式 :
只輸出日志信息, 不附加任何其他 信息, 如 優(yōu)先級 標(biāo)簽等, 使用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"格式 :
"日期 時(shí)間 優(yōu)先級 / 標(biāo)簽 (進(jìn)程ID) : 進(jìn)程名稱 : 日志信息 ", 使用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"格式:
" [ 日期 時(shí)間 進(jìn)程ID : 線程ID 優(yōu)先級 / 標(biāo)簽] 日志信息 ", 輸出以上提到的所有的頭信息, 使用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 命令, 輸出命令, 之后推出命令, 不會進(jìn)行阻塞;

輸出最近的日志 :

使用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:~$   

查看日志緩沖區(qū)信息 :

使用 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:~$   

(2) 過濾項(xiàng)解析

過濾項(xiàng)格式 : <tag>[:priority] , 標(biāo)簽:日志等級, 默認(rèn)的日志過濾項(xiàng)是 " *:I " ;

-- V : Verbose (明細(xì));

-- D : Debug (調(diào)試);

-- I : Info (信息);

-- W : Warn (警告);

-- E : Error (錯誤);

-- F: Fatal (嚴(yán)重錯誤);

-- S : Silent(Super all output) (最高的優(yōu)先級, 可能不會記載東西);

  • 過濾指定等級日志 :
    使用 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;
  • 過濾指定標(biāo)簽等級日志 :
    使用 adb logcat WifiHW:D *:S 命令進(jìn)行過濾;
    -- 命令含義 :
    輸出10條日志, 日志是 標(biāo)簽為 WifiHW, 并且優(yōu)先級 Debug(調(diào)試) 等級以上的級別的日志;

--注意 *: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;  
  • 可以同時(shí)設(shè)置多個過濾器 :
    使用adb logcat WifiHW:D dalvikvm:I *:S 命令, 輸出 WifiHW 標(biāo)簽 的 Debug 以上級別 和 dalvikvm 標(biāo)簽的 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;  
    1. 使用管道過濾日志
      過濾固定字符串 : 只要命令行出現(xiàn)的日志都可以過濾, 不管是不是標(biāo)簽;

-- 命令 : 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;  
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末腊徙,一起剝皮案震驚了整個濱河市简十,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌撬腾,老刑警劉巖螟蝙,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異民傻,居然都是意外死亡胰默,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門漓踢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牵署,“玉大人,你說我怎么就攤上這事喧半∨福” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵挺据,是天一觀的道長取具。 經(jīng)常有香客問我,道長扁耐,這世上最難降的妖魔是什么者填? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮做葵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘心墅。我一直安慰自己酿矢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布怎燥。 她就那樣靜靜地躺著瘫筐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪铐姚。 梳的紋絲不亂的頭發(fā)上策肝,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音隐绵,去河邊找鬼之众。 笑死,一個胖子當(dāng)著我的面吹牛依许,可吹牛的內(nèi)容都是我干的棺禾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼峭跳,長吁一口氣:“原來是場噩夢啊……” “哼膘婶!你這毒婦竟也來了缺前?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悬襟,失蹤者是張志新(化名)和其女友劉穎衅码,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脊岳,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逝段,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了逸绎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惹恃。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖棺牧,靈堂內(nèi)的尸體忽然破棺而出巫糙,到底是詐尸還是另有隱情,我是刑警寧澤颊乘,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布参淹,位于F島的核電站,受9級特大地震影響乏悄,放射性物質(zhì)發(fā)生泄漏浙值。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一檩小、第九天 我趴在偏房一處隱蔽的房頂上張望开呐。 院中可真熱鬧,春花似錦规求、人聲如沸筐付。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瓦戚。三九已至,卻和暖如春丛塌,著一層夾襖步出監(jiān)牢的瞬間较解,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工赴邻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留印衔,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓姥敛,卻偏偏與公主長得像当编,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容