开发过程中,解决各种问题bug,不管是性能问题还是ANR问题,还是各种严重崩溃问题,经常需要抓取log,从log中分析找到问题源头,并进行修改。
但是,统一时间点下,可能会有很多log打印出来,分属于各个不同的进程。因此,我们需要的部分可能已经被淹没了。因此,使用工具或者命令抓取需要的log部分,并尽可能少的减少遗漏,是非常有必要的。
通常情况下,可以使用工具。
因此,使用命令抓取变得很重要,这里就自己总结下adb相关的命令。
比如eclipse 的logcat可以直接查看log输出,但是有个问题就是在手机设备没有连接的情况下,是很恼火的。比如我需要开机log,可以直接使用adb抓取到txt文件中就OK了。google的同时自己整理了一下。不喜勿喷。
adb logcat 命令使用帮助说明;
logcat: option requires an argument -- v Unrecognized Option 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"
抓取log之前先清除缓存的log信息。
appledeMacBook-Pro:~ apple$ adb logcat -c appledeMacBook-Pro:~ apple$
或者你可以这样写
appledeMacBook-Pro:~ apple$ adb logcat -c && adb logcat --------- beginning of /dev/log/main E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H
appledeMacBook-Pro:~ apple$ adb logcat --------- beginning of /dev/log/main D/AppOps ( 1989): startOperation: allowing code 40 uid 1000 package android D/NtpTrustedTime( 1989): currentTimeMillis() cache hit --------- beginning of /dev/log/system D/PowerManagerService( 1989): newScreenState = 0 D/PowerManagerService( 1989): updateDisplayPowerStateLocked: mBootCompleted = true, mScreenBrightnessSettingDefault = 165 D/PowerManagerService( 1989): updateDisplayPowerStateLocked: xxxx = 187 D/PowerManagerService( 1989): Package Lib: shouldUseProximitySensorLocked mLidMode = false D/PowerManagerService( 1989): updateDisplayPowerStateLocked: yyyyy = 187 D/PowerManagerDisplayController( 1989): changed is false, mPendingRequestChangedLocked = false D/NtpTrustedTime( 1989): currentTimeMillis() cache hit D/NtpTrustedTime( 1989): currentTimeMillis() cache hit
使用I,V,D,E,F,W等过滤
和Log.i ,Log.e ,Log.d,Log.w等对应。
appledeMacBook-Pro:~ apple$ adb logcat *:I --------- beginning of /dev/log/main --------- beginning of /dev/log/system E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H上面输出全部是Log.i打印出来的Log.
输出指定标签的日志.后面必须是*:S
appledeMacBook-Pro:~ apple$ adb logcat Wisdom_ConnectSdk:* *:S --------- beginning of /dev/log/main --------- beginning of /dev/log/system I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
appledeMacBook-Pro:~ apple$ adb logcat -v time --------- beginning of /dev/log/main 02-08 12:18:48.700 D/eup (17972): rqdp{ current unseen pn:}com.tencent.androidqqmail:Push 02-08 12:18:49.810 D/MSF.C.NetConnTag( 2700): [E]netRecv ssoSeq:2023997818 uin:*9889 cmd:-316037937 2023998098 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 02-08 12:18:50.630 D/MSF.C.NetConnTag( 2700): [E]netRecv ssoSeq:2024000051 uin:*9889 cmd:961141751 2024000635 02-08 12:18:50.665 D/MSF.C.NetConnTag( 2700): [E]pa ok: 93239 02-08 12:18:50.670 D/MSF.C.NetConnTag( 2700): [E]netSend ssoSeq:93239 uin:*9889 cmd:-183665717 93419 --------- beginning of /dev/log/system
-v 设置log的打印格式。上面的time显示时间。
process 只显示进程id
appledeMacBook-Pro:~ apple$ adb logcat -c appledeMacBook-Pro:~ apple$ adb logcat -v process --------- beginning of /dev/log/main E(16917) receive a heartbeat msg: H! (cynicok) I(16917) request long connection success and the state = 200 (Wisdom_ConnectSdk) I(16917) receive the heart message H (Wisdom_ConnectSdk)
appledeMacBook-Pro:~ apple$ adb logcat -v tag --------- beginning of /dev/log/main E/cynicok : receive a heartbeat msg: H! I/Wisdom_ConnectSdk: request long connection success and the state = 200 I/Wisdom_ConnectSdk: receive the heart message H I/System.out: AsyncExecImpl : add task, s = 1 D/SyncMainManager: requestInstantSync check local data : quickReply --------- beginning of /dev/log/system D/ActivityManager: Not moving, persistent: ProcessRe
appledeMacBook-Pro:~ apple$ adb logcat -v thread --------- beginning of /dev/log/main E(16917:16936) receive a heartbeat msg: H! I(16917:16917) request long connection success and the state = 200 I(16917:16917) receive the heart message H I( 2235: 2235) AsyncExecImpl : add task, s = 1 D( 2235:20109) requestInstantSync check local data : quickReply --------- beginning of /dev/log/system D( 1989: 3972) Not moving, persistent: ProcessRecord{41fd2488 2173:com.android.phone/1001} D( 1989: 2186) noteOperation: allowing code 14 uid 10042 package com.meizu.mzsyncservice
adb logcat | grep PID
adb logcat | grep --color= auto PID
appledeMacBook-Pro:~ apple$ adb logcat | grep 16917 E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H
appledeMacBook-Pro:~ apple$ adb logcat | grep --color=auto 16917 E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H!
先查看程序进程pid。下面这个命令也可以看到该进程使用内存相关情况。
appledeMacBook-Pro:~ apple$ adb shell dumpsys meminfo com.wisdom.wisdomapp Applications Memory Usage (kB): Uptime: 14917420 Realtime: 24328990 ** MEMINFO in pid 16835 [com.wisdom.wisdomapp] ** Pss Private Private Swapped Heap Heap Heap Total Dirty Clean Dirty Size Alloc Free ------ ------ ------ ------ ------ ------ ------ Native Heap 0 0 0 0 22396 20994 653 Dalvik Heap 26814 26660 0 0 29228 28344 884 Dalvik Other 1603 1512 0 0 Stack 24 24 0 0 Other dev 4 0 4 0 .so mmap 1125 732 0 0 .apk mmap 5 0 0 0 .ttf mmap 0 0 0 0 .dex mmap 307 28 0 0 Other mmap 10 8 0 0 Unknown 20246 20240 0 0 TOTAL 50138 49204 4 0 51624 49338 1537
appledeMacBook-Pro:~ apple$ adb shell service Usage: service [-h|-?] service list service check SERVICE service call SERVICE CODE [i32 INT | s16 STR] ... Options: i32: Write the integer INT into the send parcel. s16: Write the UTF-16 string STR into the send parcel.
appledeMacBook-Pro:~ apple$ adb shell service list Found 92 services: 0 secsystemserver: [com.lbe.security.service.core.loader2.internal.ISystemServer] 1 secloader2: [com.lbe.security.service.core.loader2.internal.ILoaderServiceEx] 2 sip: [android.net.sip.ISipService] 3 phone_ext: [com.meizu.telephony.ITelephonyExt] 4 phone: [com.android.internal.telephony.ITelephony] 5 iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo] 6 simphonebook: [com.android.internal.telephony.IIccPhoneBook] 7 isms: [com.android.internal.telephony.ISms] 8 gesture_manager: [android.view.IGestureManager] 9 deivce_states: [android.os.IDeviceStateService] 10 access_control: [android.content.IAccessControlManager] 11 media_router: [android.media.IMediaRouterService] 12 print: [android.print.IPrintManager] 13 dreams: [android.service.dreams.IDreamManager]
python值得学习。有空是要学习下。
#!/usr/bin/env python #coding:utf-8 #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). import os import sys packageName=str(sys.argv[1]) command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName) p = os.popen(command) ##for some applications,there are multiple processes,so we should get all the process id pid = p.readline().strip() filters = pid while(pid != ""): pid = p.readline().strip() if (pid != ''): filters = filters + "|" + pid #print 'command = %s;filters=%s'%(command, filters) if (filters != '') : cmd = 'adb logcat | grep --color=always -E "%s" '%(filters) os.system(cmd)脚本使用:
appledeMacBook-Pro:Desktop apple$ python logcat.py com.wisdom.wisdomapp E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200 I/Wisdom_ConnectSdk(16917): receive the heart message H E/cynicok (16917): receive a heartbeat msg: H! I/Wisdom_ConnectSdk(16917): request long connection success and the state = 200
ps命令过滤查看。
appledeMacBook-Pro:Desktop apple$ adb shell shell@mx2:/ $ ps | grep com.meizu.flyme.weather system 9175 1567 880208 32432 ffffffff 00000000 S com.meizu.flyme.weather shell@mx2:/ $
adb 这么多命令,我该快速的用呢。其实早就有大神解决了这个问题。
git 地址 :https://github.com/JakeWharton/pidcat
这个开源代码pidcat在mac上面使用需要安装HomeView
^CappledeMacBook-Pro:Desktop apple$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/local/share/man/man1/brew.1 Press RETURN to continue or any other key to abort ==> /usr/bin/sudo /bin/mkdir /usr/local Password: ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local ==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew ==> Downloading and installing Homebrew... remote: Counting objects: 229812, done. remote: Compressing objects: 100% (60244/60244), done. remote: Total 229812 (delta 168312), reused 229812 (delta 168312) Receiving objects: 100% (229812/229812), 52.82 MiB | 1022 KiB/s, done. Resolving deltas: 100% (168312/168312), done. From https://github.com/Homebrew/homebrew * [new branch] master -> origin/master HEAD is now at 9a0fbf6 moreutils: update 0.55 bottle. ==> Installation successful! ==> Next steps Run `brew doctor` before you install anything Run `brew help` to get started appledeMacBook-Pro:Desktop apple$
.bashrc
or .zshrc
两个文件中
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
安装完成HomeView以及配置好环境后,安装pidcat.py
appledeMacBook-Pro:Desktop apple$ brew install pidcat ==> Downloading https://github.com/JakeWharton/pidcat/archive/1.4.1.tar.gz ######################################################################## 100.0%
原文:http://blog.csdn.net/xxm282828/article/details/43635829