adb logcat 基本用法

入门android ,至少需要了解 adb 吧,那么打 log 也是必不可少的了。

下面简单介绍一下 adb logcat 基本用法:

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 -v time -s videocamera

意思是说,会打印出 log 信息中时间,以及包含关键字 "videocamera" 的所有 log

C:\Users\Administrator>adb logcat -v time -s videocamera fulin
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
07-01 08:43:57.037 V/videocamera( 2329): VideoCamera onCreate!
07-01 08:43:57.039 I/videocamera( 2329): getExternalSdState() : removed
07-01 08:43:57.040 I/videocamera( 2329): getExternalState() : mounted
07-01 08:43:57.040 I/videocamera( 2329): Sdcard is exists ? false
07-01 08:43:57.157 V/videocamera( 2329): fulin onCreate Storage.getAvailableSpace() 1335459840
07-01 08:43:57.159 V/videocamera( 2329): fulin initThumbnailButton
07-01 08:43:57.170 D/videocamera( 2329): Thumbnail.getLastThumbnail >>>
07-01 08:43:57.204 E/videocamera( 2329): --------------readVideoPreferences---------------minutes = 600
07-01 08:43:57.205 E/videocamera( 2329): --------------readVideoPreferences----------quality6=10
07-01 08:43:57.207 E/videocamera( 2329): -----------------readVideoPreferences---------mCameraId=0
07-01 08:43:57.208 E/videocamera( 2329): -----------------readVideoPreferences---------quality7=10
07-01 08:43:57.209 V/videocamera( 2329): mDesiredPreviewWidth=640. mDesiredPreviewHeight=480
07-01 08:43:57.234 V/videocamera( 2329): startPreview
07-01 08:43:57.240 V/videocamera( 2329): videocamera--mParameters.setFocusMode(mFocusManager.getFocusMode())
07-01 08:43:57.265 D/videocamera( 2329): Thumbnail.getLastThumbnail <<< is null:false
07-01 08:43:57.269 V/videocamera( 2329): Video snapshot size is 3264x2448
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 176x144
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 320x240
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 352x288
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 480x368
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 640x480
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 720x480
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 800x600
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 864x480
07-01 08:43:57.376 D/videocamera( 2329): SupportedPreviewSizes : 1280x720
07-01 08:43:57.422 V/videocamera( 2329): updateSceneOnScreenIndicator isVisible falsemSceneIndicator android.widget.ImageView@41acd0e0

……

<think>好的,我需要向用户介绍adb logcat用法。首先,我得回忆一下自己对这个工具的了解。adb logcat是Android调试桥(ADB)中的一个命令,用于查看设备的日志信息。开发人员常用它来调试应用程序,分析系统行为等。 用户可能想知道基本用法,所以我应该从最简单的命令开始。比如直接运行`adb logcat`会输出大量的日志,但信息太多可能难以处理。接下来需要介绍如何过滤日志,常用的过滤方法有按标签、优先级等。比如使用`adb logcat -s TAG`可以只显示特定标签的日志。 然后,用户可能需要了解日志的优先级,比如V(Verbose)、D(Debug)、I(Info)、W(Warn)、E(Error)、F(Fatal)、S(Silent)。这里要解释清楚每个优先级对应的含义,并举例说明如何按优先级过滤,例如`adb logcat *:E`只显示错误级别以上的日志。 另外,格式化输出也很重要。默认格式可能不够清晰,使用`adb logcat -v <格式>`可以改变输出格式,比如时间、进程ID等。常用的格式有brief、process、tag、raw、time、threadtime等,其中threadtime可能比较详细,适合需要更多上下文的情况。 还有清除日志缓冲区、输出到文件、查看特定进程日志等高级用法需要涵盖。比如`adb logcat -c`清除旧日志,`adb logcat -f /sdcard/log.txt`将日志保存到文件,或者结合重定向符`>`在PC上保存。对于查看特定进程,可能需要先获取进程ID,再用`adb logcat --pid=PID`来过滤。 需要注意的是,有些命令可能需要搭配其他参数,或者在不同环境下使用。例如,在Windows、macOS或Linux下,命令可能略有不同,但大部分是相同的。此外,adb logcat还可以结合grep(在Linux/macOS)或findstr(在Windows)进行更复杂的过滤,但这点可能需要用户有一定命令行基础。 最后,要提醒用户,如果遇到权限问题,可能需要设备已开启USB调试,并且授权了计算机的调试权限。此外,某些日志可能需要root权限才能查看全部内容,但一般情况下普通调试已经足够。 现在需要将这些内容组织成一个结构清晰的回答,分点说明,确保用户能一步步理解和应用。同时注意使用正确的LaTeX格式,不过在这个主题中可能数学公式较少,主要注意代码块和命令的格式是否正确。比如用反引号包裹命令,参数用尖括号表示变量部分,必要时用中文解释每个参数的作用。</think>以下是对`adb logcat`用法的详细介绍,按功能分类说明: --- ### 一、基本命令 1. **查看完整日志** 直接运行命令会输出设备的所有日志(信息量大): ```bash adb logcat ``` 2. **退出日志输出** 按`Ctrl + C`终止日志输出。 --- ### 二、按优先级过滤日志 Android日志分为7个优先级(从低到高): `V`(Verbose,全部)、`D`(Debug,调试)、`I`(Info,信息)、`W`(Warn,警告)、`E`(Error,错误)、`F`(Fatal,严重错误)、`S`(Silent,无输出)。 1. **显示指定优先级及以上日志** ```bash adb logcat *:优先级 # 示例:仅显示错误(E)及以上日志 adb logcat *:E ``` --- ### 三、按标签(TAG)过滤 1. **显示特定标签的日志** ```bash adb logcat -s TAG # 示例:过滤系统进程和错误日志 adb logcat -s System.err AndroidRuntime ``` 2. **组合过滤(标签 + 优先级)** ```bash adb logcat TAG:优先级 # 示例:显示标签为"ActivityManager"且优先级为Info及以上的日志 adb logcat ActivityManager:I *:S ``` --- ### 四、格式化输出 使用`-v <格式>`指定日志显示格式,常用格式: `brief`(默认)、`process`、`tag`、`raw`、`time`、`threadtime`(推荐)。 1. **显示时间戳和线程信息** ```bash adb logcat -v threadtime ``` --- ### 五、实用技巧 1. **清空日志缓冲区** ```bash adb logcat -c ``` 2. **将日志保存到文件** ```bash adb logcat -f /sdcard/log.txt # 保存到设备 adb logcat > pc_log.txt # 保存到电脑(实时写入) ``` 3. **过滤特定进程日志** ```bash adb logcat --pid=<进程ID> # 步骤: # 1. 查找进程ID:adb shell "ps | grep 包名" # 2. 过滤日志:adb logcat --pid=1234 ``` 4. **实时滚动查看日志** ```bash adb logcat -f - | grep "关键词" # Linux/macOS adb logcat -f - | findstr "关键词" # Windows ``` --- ### 六、高级用法 1. **查看内核日志** ```bash adb logcat -b kernel ``` 2. **指定日志缓冲区** Android有多个日志缓冲区(`main`、`radio`、`events`等): ```bash adb logcat -b main -b radio # 同时查看主缓冲区和射频日志 ``` --- ### 七、注意事项 1. 确保设备已通过`adb devices`正确连接。 2. 若日志过多,建议结合`grep`/`findstr`二次过滤。 3. 部分系统日志需要`root`权限才能查看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值