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

开发过程中,解决各种问题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

打印有时间的Log

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)

-v tag按照标签来打印。

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

-v  thread  显示 I ,D E等日志类型,16917:16936 进程ID:线程ID

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

根据进程id来过滤。

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

带颜色的Log

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

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

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.

列出service

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]

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

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
[email protected]:/ $ ps | grep com.meizu.flyme.weather
system    9175  1567  880208 32432 ffffffff 00000000 S com.meizu.flyme.weather
[email protected]:/ $ 

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$ 

配置adb环境变量,并且将变量添加到.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

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

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

appledeMacBook-Pro:Desktop apple$ brew install pidcat
==> Downloading https://github.com/JakeWharton/pidcat/archive/1.4.1.tar.gz
######################################################################## 100.0%
时间: 2024-10-21 15:28:50

adb 命令使用之抓取log并过滤。的相关文章

adb命令 logcat日志抓取

一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sdcard中建立一个1.log的文件或在抓日志时直接导出到电脑位置 3.程序运行cmd,进入到含有adb.exe目录 4.输入adb devices 查看设备是否连上 5.输入抓取命令: 存放到手机 adb logcat -s *:E > /mmt/sdcard/1.log 存放到PC adb logcat -s '*:E' > d

如何用adb抓取log?

在Androidclient的測试过程中,有时候我们会遇到闪退等异常情况. 这时我们能够通过adb抓取log.从而给开发提供很多其它信息. 一.下载ADB.exe 在网上搜索"adb工具包"就能够找到非常多adb了,我们须要adb.exe. AdbWinApi.dll. AdbWinUsbApi.dll这3个文件,其它无所谓.将包括这3个文件的文件夹放入随意文件夹(这里放入d:\adb文件夹) 二.配置环境变量 电脑桌面上右击 计算机-->属性-->高级系统配置-->

怎样用adb抓取log?

在Android客户端的测试过程中,有时候我们会遇到闪退等异常情况.这时我们可以通过adb抓取log,从而给开发提供更多信息. 一.下载 在网上搜索"adb工具包"就可以找到很多adb了,我们需要adb.exe. AdbWinApi.dll. AdbWinUsbApi.dll这3个文件,其他无所谓.将包含这3个文件的文件夹放入任意目录(这里放入d:\adb目录) 二.配置环境变量 电脑桌面上右击 计算机-->属性-->高级系统配置-->环境变量,在 系统变量 中找到p

Android 抓取LOG的几种命令【转】

通常调试时候需要抓取log信息,下面几种通过ADB命令来抓取log的方法: USB连接上手机,手机需要其他操作:然后运行ADB工具:输入不同的命令即可抓取对应的LOG信息. 抓取radio LOG信息命令: adb logcat -b radio -v time >log_radio.txt 抓取main LOG信息命令: adb logcat -b main -v time >log_main.txt 抓取event LOG信息命令: adb logcat -b events-v time

Android 抓取LOG的几种命令

通常调试时候需要抓取log信息,下面几种通过ADB命令来抓取log的方法: USB连接上手机,手机需要其他操作:然后运行ADB工具:输入不同的命令即可抓取对应的LOG信息. 抓取radio LOG信息命令: adb logcat -b radio -v time >log_radio.txt 抓取main LOG信息命令: adb logcat -b main -v time >log_main.txt 抓取event LOG信息命令: adb logcat -b events-v time

小米手机抓取Log教程

当小米手机遇到任何系统下的较为严重的故障时(即此时系统还是基本正常运行的,只是某些功能实现出了问题),例如软件自动退出,SD卡自动卸载,电话无法拨出等等.可以请用户协助抓取log即系统运行日志,来发送到[email protected],或者在论坛售后区发帖.我们再进一步转发给相应的工程师去根据log分析处理,这样就能够更加精准地帮助用户解决问题.那么如何抓取Log呢? 1.方法一     在某个程序运行过程中出现程序中断(FC),会出现以下图片,点击“立即报修”,就会把相应的LOG文件通过邮件

nutch2 crawl 命令分解,抓取网页的详细过程

首先,何以见得crawl是inject,generate,fetch,parse,update的集成呢(命令的具体含义及功能会在后续文章中说明),我们打开NUTCH_HOME/runtime/local/bin/crawl 我将主要代码黏贴下来 # initial injection echo "Injecting seed URLs" __bin_nutch inject "$SEEDDIR" -crawlId "$CRAWL_ID" # ma

软件测试之adb命令-实际公司使用场景--今日log

Dotest-董浩整理1)可以看内存泄漏: 2)可以安装.卸载app--截图并提交bug: 3)可以通过抓app日志定位问题: 4)可以结合monkey进行性能.压力(健壮性的测试).** 当然?adb命令有很多,也有很多其他的使用场景.不过我这里总结的是实际公司(小米.58等)在软件测试过程中的使用!如果有其他用到的可以告知,共同学习,谢谢! ?怎么才能把学到的东西运用到实际工作中? 实际工作中都用到哪些?知识? 当然不是说你学的多不好?.?学的再多你不会应用,怎么办? 个人建议:在学习的时候

adb 命令抓取log日志方法

1.确保手机已连接电脑,并开启调试 2.输入指令:  adb logcat  *:E  >c:\log.txt 如图所示: "E"  表示log日志级别 adb log日志级别 : 每一个输出的Android日志信息都有一个标签和它的优先级. · 日志的标签是系统部件原始信息的一个简要的标志.(比如:"View"就是查看系统的标签). · 优先级有下列集中,是按照从低到高顺利排列的: V - Verbose (lowest priority) D - Debu