adb命令使用之抓取log并过滤

  • 开发过程中,解决各种问题bug,不管是性能问题还是ANR问题,还是各种严重崩溃问题,经常需要抓取log,从log中分析找到问题源头,并进行修改。

    但是,统一时间点下,可能会有很多log打印出来,分属于各个不同的进程。因此,我们需要的部分可能已经被淹没了。因此,使用工具或者命令抓取需要的log部分,并尽可能少的减少遗漏,是非常有必要的。

    通常情况下,可以使用工具。

    因此,使用命令抓取变得很重要,这里就自己总结下adb相关的命令。

    比如eclipse 的logcat可以直接查看log输出,但是有个问题就是在手机设备没有连接的情况下,是很恼火的。比如我需要开机log,可以直接使用adb抓取到txt文件中就OK了。google的同时自己整理了一下。不喜勿喷。

    adb logcat 命令使用帮助说明;

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

    抓取log之前先清除缓存的log信息。

    1. appledeMacBook-Pro:~ apple$ adb logcat -c
    2. appledeMacBook-Pro:~ apple$

    或者你可以这样写

    1. appledeMacBook-Pro:~ apple$ adb logcat -c && adb logcat
    2. --------- beginning of /dev/log/main
    3. E/cynicok (16917): receive a heartbeat msg: H!
    4. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    5. I/Wisdom_ConnectSdk(16917): receive the heart message H

    这样查看晕死。。。。
    01. appledeMacBook-Pro:~ apple$ adb logcat
    02. --------- beginning of /dev/log/main
    03. D/AppOps  ( 1989): startOperation: allowing code 40 uid 1000 package android
    04. D/NtpTrustedTime( 1989): currentTimeMillis() cache hit
    05. --------- beginning of /dev/log/system
    06. D/PowerManagerService( 1989): newScreenState = 0
    07. D/PowerManagerService( 1989): updateDisplayPowerStateLocked:  mBootCompleted = true, mScreenBrightnessSettingDefault = 165
    08. D/PowerManagerService( 1989): updateDisplayPowerStateLocked:  xxxx = 187
    09. D/PowerManagerService( 1989): Package Lib: shouldUseProximitySensorLocked mLidMode = false
    10. D/PowerManagerService( 1989): updateDisplayPowerStateLocked:  yyyyy = 187
    11. D/PowerManagerDisplayController( 1989):  changed is false, mPendingRequestChangedLocked =false
    12. D/NtpTrustedTime( 1989): currentTimeMillis() cache hit
    13. D/NtpTrustedTime( 1989): currentTimeMillis() cache hit

    使用I,V,D,E,F,W等过滤

    和Log.i ,Log.e ,Log.d,Log.w等对应。

    01. appledeMacBook-Pro:~ apple$ adb logcat *:I
    02. --------- beginning of /dev/log/main
    03. --------- beginning of /dev/log/system
    04. E/cynicok (16917): receive a heartbeat msg: H!
    05. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    06. I/Wisdom_ConnectSdk(16917): receive the heart message H
    07. E/cynicok (16917): receive a heartbeat msg: H!
    08. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    09. I/Wisdom_ConnectSdk(16917): receive the heart message H
    10. E/cynicok (16917): receive a heartbeat msg: H!
    11. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    12. I/Wisdom_ConnectSdk(16917): receive the heart message H
    上面输出全部是Log.i打印出来的Log.

    输出指定标签的日志.后面必须是*:S

    1. appledeMacBook-Pro:~ apple$ adb logcat Wisdom_ConnectSdk:* *:S
    2. --------- beginning of /dev/log/main
    3. --------- beginning of /dev/log/system
    4. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200

    打印有时间的Log

    01. appledeMacBook-Pro:~ apple$ adb logcat -v time
    02. --------- beginning of /dev/log/main
    03. 02-08 12:18:48.700 D/eup     (17972): rqdp{  current unseen pn:}com.tencent.android<a href="http://www.it165.net/qq/" target="_blank" class="keylink">qq</a>mail:Push
    04. 02-08 12:18:49.810 D/MSF.C.NetConnTag( 2700): [E]netRecv ssoSeq:2023997818 uin:*9889cmd:-316037937 2023998098
    05. 02-08 12:18:49.840 D/Q.msg.TroopMsgProxy(18335): [E]insertToList MessageRecord=friendUin:2824senderuin:9517,istroop:1,msgType:-1000,time:1423369131,shmsgseq:33949
    06. 02-08 12:18:50.630 D/MSF.C.NetConnTag( 2700): [E]netRecv ssoSeq:2024000051 uin:*9889cmd:961141751 2024000635
    07. 02-08 12:18:50.665 D/MSF.C.NetConnTag( 2700): [E]pa ok: 93239
    08. 02-08 12:18:50.670 D/MSF.C.NetConnTag( 2700): [E]netSend ssoSeq:93239 uin:*9889cmd:-183665717 93419
    09. --------- beginning of /dev/log/system

    -v 设置log的打印格式。上面的time显示时间。

    process 只显示进程id

    1. appledeMacBook-Pro:~ apple$ adb logcat -c
    2. appledeMacBook-Pro:~ apple$ adb logcat -v process
    3. --------- beginning of /dev/log/main
    4. E(16917) receive a heartbeat msg: H!  (cynicok)
    5. I(16917) request long connection success and the state = 200  (Wisdom_ConnectSdk)
    6. I(16917) receive the heart message H  (Wisdom_ConnectSdk)

    -v tag按照标签来打印。
    01. appledeMacBook-Pro:~ apple$ adb logcat -v tag
    02. --------- beginning of /dev/log/main
    03. E/cynicok : receive a heartbeat msg: H!
    04. I/Wisdom_ConnectSdk: request long connection success and the state = 200
    05. I/Wisdom_ConnectSdk: receive the heart message H
    06. I/System.out: AsyncExecImpl : add task, s = 1
    07. D/SyncMainManager: requestInstantSync check local data : quickReply
    08. --------- beginning of /dev/log/system
    09. D/ActivityManager: Not moving, persistent: ProcessRe

    -v thread 显示 I ,D E等日志类型,16917:16936 进程ID:线程ID
    01. appledeMacBook-Pro:~ apple$ adb logcat -v thread
    02. --------- beginning of /dev/log/main
    03. E(16917:16936) receive a heartbeat msg: H!
    04. I(16917:16917) request long connection success and the state = 200
    05. I(16917:16917) receive the heart message H
    06. I( 22352235) AsyncExecImpl : add task, s = 1
    07. D( 2235:20109) requestInstantSync check local data : quickReply
    08. --------- beginning of /dev/log/system
    09. D( 19893972) Not moving, persistent: ProcessRecord{41fd2488 2173:com.android.phone/1001}
    10. D( 19892186) noteOperation: allowing code 14 uid 10042 package com.meizu.mzsyncservice

    根据进程id来过滤。

    adb logcat | grep PID

    adb logcat | grep --color= auto PID

    1. appledeMacBook-Pro:~ apple$ adb logcat | grep 16917
    2. E/cynicok (16917): receive a heartbeat msg: H!
    3. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    4. I/Wisdom_ConnectSdk(16917): receive the heart message H
    5. E/cynicok (16917): receive a heartbeat msg: H!
    6. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    7. I/Wisdom_ConnectSdk(16917): receive the heart message H

    带颜色的Log

    1. appledeMacBook-Pro:~ apple$ adb logcat | grep --color=auto  16917
    2. E/cynicok (16917): receive a heartbeat msg: H!
    3. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    4. I/Wisdom_ConnectSdk(16917): receive the heart message H
    5. E/cynicok (16917): receive a heartbeat msg: H!

    先查看程序进程pid。下面这个命令也可以看到该进程使用内存相关情况。

    01. appledeMacBook-Pro:~ apple$ adb shell dumpsys meminfo com.wisdom.wisdomapp
    02. Applications Memory Usage (kB):
    03. Uptime: 14917420 Realtime: 24328990
    04.  
    05. ** MEMINFO in pid 16835 [com.wisdom.wisdomapp] **
    06. Pss  Private  Private  Swapped     Heap     Heap     Heap
    07. Total    Dirty    Clean    Dirty     Size    Alloc     Free
    08. ------   ------   ------   ------   ------   ------   ------
    09. Native Heap        0        0        0        0    22396    20994      653
    10. Dalvik Heap    26814    26660        0        0    29228    28344      884
    11. Dalvik Other     1603     1512        0        0                          
    12. Stack       24       24        0        0                          
    13. Other dev        4        0        4        0                          
    14. .so mmap     1125      732        0        0                          
    15. .apk mmap        5        0        0        0                          
    16. .ttf mmap        0        0        0        0                          
    17. .dex mmap      307       28        0        0                          
    18. Other mmap       10        8        0        0                          
    19. Unknown    20246    20240        0        0                          
    20. TOTAL    50138    49204        4        0    51624    49338     1537

    查看当前系统中正在跑的service

    1. appledeMacBook-Pro:~ apple$ adb shell service
    2. Usage: service [-h|-?]
    3. service list
    4. service check SERVICE
    5. service call SERVICE CODE [i32 INT | s16 STR] ...
    6. Options:
    7. i32: Write the integer INT into the send parcel.
    8. s16: Write the UTF-16 string STR into the send parcel.

    列出service

    01. appledeMacBook-Pro:~ apple$ adb shell service list
    02. Found 92 services:
    03. 0   secsystemserver: [com.lbe.security.service.core.loader2.internal.ISystemServer]
    04. 1   secloader2: [com.lbe.security.service.core.loader2.internal.ILoaderServiceEx]
    05. 2   sip: [android.net.sip.ISipService]
    06. 3   phone_ext: [com.meizu.telephony.ITelephonyExt]
    07. 4   phone: [com.android.internal.telephony.ITelephony]
    08. 5   iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
    09. 6   simphonebook: [com.android.internal.telephony.IIccPhoneBook]
    10. 7   isms: [com.android.internal.telephony.ISms]
    11. 8   gesture_manager: [android.view.IGestureManager]
    12. 9   deivce_states: [android.os.IDeviceStateService]
    13. 10  access_control: [android.content.IAccessControlManager]
    14. 11  media_router: [android.media.IMediaRouterService]
    15. 12  print: [android.print.IPrintManager]
    16. 13  dreams: [android.service.dreams.IDreamManager]

    有人写了个脚本查看log,具体地址忘了,怪我只保存了脚本内容,却把来源忘了。

    python值得学习。有空是要学习下。

    01. #!/usr/bin/env python
    02. #coding:utf-8
    03. #This script is aimed to grep logs by application(User should input a packageName and then we look up for the process ids then separate logs by process ids).
    04.  
    05. import os
    06. import sys
    07.  
    08. packageName=str(sys.argv[1])
    09.  
    10. command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName)
    11. p = os.popen(command)
    12. ##for some applications,there are multiple processes,so we should get all the process id
    13. pid = p.readline().strip()
    14. filters = pid
    15. while(pid != ""):
    16. pid = p.readline().strip()
    17. if (pid != ''):
    18. filters = filters +  "|" + pid
    19. #print 'command = %s;filters=%s'%(command, filters)
    20. if (filters != '') :
    21. cmd = 'adb logcat | grep --color=always -E "%s" '%(filters)
    22. os.system(cmd)
    脚本使用:
    01. appledeMacBook-Pro:Desktop apple$ python logcat.py com.wisdom.wisdomapp
    02. E/cynicok (16917): receive a heartbeat msg: H!
    03. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    04. I/Wisdom_ConnectSdk(16917): receive the heart message H
    05. E/cynicok (16917): receive a heartbeat msg: H!
    06. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    07. I/Wisdom_ConnectSdk(16917): receive the heart message H
    08. E/cynicok (16917): receive a heartbeat msg: H!
    09. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    10. I/Wisdom_ConnectSdk(16917): receive the heart message H
    11. E/cynicok (16917): receive a heartbeat msg: H!
    12. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
    13. I/Wisdom_ConnectSdk(16917): receive the heart message H
    14. E/cynicok (16917): receive a heartbeat msg: H!
    15. I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200

    ps命令过滤查看。

    1. appledeMacBook-Pro:Desktop apple$ adb shell
    2. shell@mx2:/ $ ps | grep com.meizu.flyme.weather
    3. system    9175  1567  880208 32432 ffffffff 00000000 S com.meizu.flyme.weather
    4. shell@mx2:/ $


    adb 这么多命令,我该快速的用呢。其实早就有大神解决了这个问题。

    git 地址 :https://github.com/JakeWharton/pidcat

    这个开源代码pidcat在mac上面使用需要安装HomeView

    01. ^CappledeMacBook-Pro:Desktop apple$ ruby -e "$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/install)"
    02. ==> This script will install:
    03. /usr/local/bin/brew
    04. /usr/local/Library/...
    05. /usr/local/share/man/man1/brew.1
    06.  
    07. Press RETURN to continue or any other key to abort
    08. ==> /usr/bin/sudo /bin/mkdir /usr/local
    09. Pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>:
    10. ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local
    11. ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local
    12. ==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
    13. ==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
    14. ==> Downloading and installing Homebrew...
    15. remote: Counting objects: 229812, done.
    16. remote: Compressing objects: 100% (60244/60244), done.
    17. remote: Total 229812 (delta 168312), reused 229812 (delta 168312)
    18. Receiving objects: 100% (229812/229812), 52.82 MiB | 1022 KiB/s, done.
    19. Resolving deltas: 100% (168312/168312), done.
    20. From https://github.com/Homebrew/homebrew
    21. * [new branch]      master     -> origin/master
    22. HEAD is now at 9a0fbf6 moreutils: update 0.55 bottle.
    23. ==> Installation successful!
    24. ==> Next steps
    25. Run `brew doctor` before you install anything
    26. Run `brew help` to get started
    27. appledeMacBook-Pro:Desktop apple$

    配置adb环境变量,并且将变量添加到.bashrc or .zshrc 两个文件中

    1. export PATH=$PATH:/Users/apple/Documents/Tools/IDE/adt-bundle-mac-x86_64-20131030/sdk/tools:/Users/apple/Documents/Tools/IDE/adt-bundle-mac-x86_64-20131030/sdk/platform-tools

    并且要保证.bash_profile也配置好adb。

    \
    安装完成HomeView以及配置好环境后,安装pidcat.py

    1. appledeMacBook-Pro:Desktop apple$ brew install pidcat
    2. ==> Downloading https://github.com/JakeWharton/pidcat/archive/1.4.1.tar.gz
    3. ######################################################################## 100.0%
//----------------------------------------------------分割----------------------------------------------------------------

//----------------------------------------------------分割----------------------------------------------------------------

//----------------------------------------------------分割----------------------------------------------------------------

一般快速查看logcat信息,查看错误信息

$adb logcat -c

$adb logcat *:E


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值