logcat的調試 比較有用的幾個命令


網上很多的logcat調試命令,但是太多的命令只會令人鹽雜。

(主要是adt工具帶的調試功能容易死掉 每次都要重啟太煩)

個人認為有一下幾個常用命令:

adb logcat -c 清除所有以前的日志

adb logcat -d 這個命令是在時間上倒過來用的 就是你先操作 然后敲這個命令 打出來的log就是你剛操作那段時間的log 而且自動退出log模式

adb logcat -s XXX這個是設置過濾用的 比如你只想查看FirstDemo的日志 就替換最后的XXX

 

補充:

android的logcat詳細用法
 

Android日志系統提供了記錄和查看系統調試信息的功能。日志都是從各種軟件和一些系統的緩沖區中記錄下來的,緩沖區可以通過 logcat 命 令來查看和使用.

使用logcat命令

你可以用 logcat 命令來查看系統日志緩沖區的內容:

[adb] logcat [<option>] ... [<filter-spec>] ...

請查看Listing of logcat Command Options ,它對logcat命 令有詳細的描述 .

你也可以在你的電腦或運行在模擬器/設備上的遠程adb shell端來使用logcat命 令,也可以在你的電腦上查看日志輸出。

$ adb logcat

你也這樣使用:

# logcat

過濾 日志輸出

每一個輸出的Android日志信息都有一個標簽和它的優先級.

  • 日志的標簽是系統部件原始信息的一個簡要的標志。(比如:“View”就 是查看系統的標簽).
  • 優先級有下列集中,是按照從低到高順利排列的:
    • V — Verbose (lowest priority)
    • D — Debug
    • I — Info
    • W — Warning
    • E — Error
    • F — Fatal
    • S — Silent (highest priority, on which nothing is ever printed)

在運行logcat的時候在前兩列的信息中你就可以看到 logcat 的標簽列表和優先級別,它是這樣標出的:<priority>/<tag> .

下面是一個logcat輸出的例子,它的優先級就似乎I,標簽 就是ActivityManage:

I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

為了讓日志輸出能體現管理的級別,你還可以用過濾器來控制日志輸出,過濾器可以幫助你描述 系統的標簽等級.

過濾器語句按照下面的格式描tag:priority ... , tag 表 示是標簽,priority 是表示標簽的報告的最低等級. 從上面的tag的中可以得到日志的優先級. 你可以在過濾器中多次寫tag:priority .

這些說明都只到空白結束。下面有一個列子,例子表示支持所有的日志信息,除了那些標簽 為”ActivityManager”和優先級為”Info”以上的和標簽為” MyApp”和優先級為” Debug”以上的。 小等級,優先權報告為tag.

adb logcat ActivityManager:I MyApp:D *:S

上面表達式的最后的元素 *:S ,,是設置所有的標 簽為”silent”,所有日志只顯示有”View” and “MyApp”的,用 *:S 的另一個用處是 能夠確保日志輸出的時候是按照過濾器的說明限制的,也讓過濾器也作為一項輸出到日志中.

下面的過濾語句指顯示優先級為warning或更高的日志信息:

adb logcat *:W

如果你電腦上運行logcat ,相比在遠程adbshell端,你還可以 為環境變量ANDROID_LOG_TAGS :輸入一個參數來設置默認的過濾

export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

需要注意的是ANDROID_LOG_TAGS 過濾器如果 通過遠程shell運行logcat 或 用adb shell logcat 來 運行模擬器/設備不能輸出日志.

控制 日志輸出格式

日志信息包括了許多元數據域包括標簽和優先級。可以修改日志的輸出格式,所以可以顯示出特 定的元數據域。可以通過 -v 選項得到格式化輸出日志的相關信息.

  • brief — Display priority/tag and PID of originating process (the default format).
  • process — Display PID only.
  • tag — Display the priority/tag only.
  • thread — Display process:thread and priority/tag only.
  • raw — Display the raw log message, with no other metadata fields.
  • time — Display the date, invocation time, priority/tag, and PID of the originating process.
  • long — Display all metadata fields and separate messages with a blank lines.

當啟動了logcat ,你可以通過-v 選 項來指定輸出格式:

[adb] logcat [-v <format>]

下面是用 thread 來產生的日志格式:

adb logcat -v thread

需要注意的是你只能-v 選項來規定輸出格式 option.

查看 可用日志緩沖區

Android日志系統有循環緩沖區,並不是所有的日志系統都有默認循環緩沖區。為了得到 日志信息,你需要通過-b 選項來啟動logcat 。如果要使用循環緩沖區,你需要查看剩余的 循環緩沖期:

  • radio — 查看緩沖區的相關的信息.
  • events — 查看和事件相關的的緩沖區.
  • main — 查看主要的日志緩沖區

-b 選項使用方法:

[adb] logcat [-b <buffer>]

下面的例子表示怎么查看日志緩沖區包含radio 和 telephony信息:

adb logcat -b radio

查看 stdout 和stderr

在默認狀態下,Android系統有stdout 和 stderr (System.outSystem.err ) 輸出到/dev/null , 在運行Dalvik VM的進程中,有一個系統可以備份日志文件。在這種情況下,系統會用stdout 和stderr 和 優先級 I.來記錄日志信息

通過這種方法指定輸出的路徑,停止運行的模擬器/設備,然后通過用setprop 命 令遠程輸入日志

$ adb shell stop $ adb shell setprop log.redirect-stdio true $ adb shell start

系統直到你關閉模擬器/設備前設置會一直保留,可以通過添加/data/local.prop 可 以使用模擬器/設備上的默認設置

Logcat命令列表

Option Description
-b <buffer> 加載一個可使用的日志緩沖區供查看,比如event 和radio . 默認值是main 。具體查看Viewing Alternative Log Buffers.
-c 清楚屏幕上的日志.
-d 輸出日志到屏幕上.
-f <filename> 指定輸出日志信息的<filename> , 默認是stdout .
-g 輸出指定的日志緩沖區,輸出后退出.
-n <count> 設置日志的最大數目<count> ., 默認值是4,需要和 -r 選 項一起使用。
-r <kbytes> <kbytes> 時 輸出日志,默認值為16,需要和-f 選 項一起使用.
-s 設置默認的過濾級別為silent.
-v <format> 設置日志輸入格式,默認的是brief 格 式,要知道更多的支持的格式,參看Controlling Log Output Format .

 

 

 

發現一個新的命令:

利用下面命令可直接在cmd中打開界面
adb shell am start -n com.android.phone/.XXXXX
 
android am命令


am命令作用:管理Activity
usage: am [start|broadcast|instrument|profile]
am start -D INTENT
am broadcast INTENT
am instrument [-r] [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
[-w] <COMPONENT> 
am profile <PROCESS> [start <PROF_FILE>|stop]
INTENT is described with:
[-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>] [-f <FLAGS>] [<URI>]The start command starts activity. Use -D option to make 'DebugOption' true. 
The broadcast command sends broadcast. 
The instrument command starts instrumentation. 
Use -r option to make 'rawMode' true. 
Use -e option to add the pair of ARG_NAME and ARG_VALUE into Bundle. 
Use -p option to specify profileFile. 
Use -w option to make 'wait' true in order to new an instance of InstrumentationWatcher. 
Use COMPONENT to specify the name of the instrumentation component. 
The profile command turn on/off profiling in a particular process specified by PROCESS.
Use start option to turn on and stop to turn off.Use PROF_FILE to specify the file path of profile. 
Use -a to set action specified by ACTION to be performed. 
Use -d to create a Uri(data) which parses the given encoded URI string DATA_URI. 
Use -t to specify the type specified by MIME_TYPE. Use -c to add a new category specified by 
CATEGORY to the intent. 
Use -e or --es to add extended data to the intent.EXTRA_KEY specifies the name of the extra data and 
EXTRA_STRING_VALUE specifies the string data value. 
Use --ez to add extended data to the intent. EXTRA_KEY specifies the name of the extra data and 
EXTRA_BOOLEAN_VALUE specifies the serializable data value. 
Use -e or --ei to add extended data to the intent. EXTRA_KEY specifies the name of the extra data and
EXTRA_INT_VALUE specifies the serializable data value. 
Use -n to explicitly set the component specified by COMPONENT to handle the intent. 
Use -f to set special flags controlling how this intent is handled.FLAGS specifies the desired flags. 
Use URI to create an intent from a URI.
在Android中,除了從界面上啟動程序之外,還可以從命令行啟動程序,使用的是命令行工具am.
usage: am [subcommand] [options]
    start an Activity: am start [-D] <INTENT>
        -D: enable debugging
    send a broadcast Intent: am broadcast <INTENT>
    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning
    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop
    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>] [<URI>]
啟動的方法為
# am start -n 包(package)名/包名.活動(activity)名稱
啟動的方法可以從每個應用的AndroidManifest.xml的文件中得到
Music 和 Video(音樂和視頻)的啟動方法為:
# am start -n com.android.music/com.android.music.MusicBrowserActivity
# am start -n com.android.music/com.android.music.VideoBrowserActivity
# am start -n com.android.music/com.android.music.MediaPlaybackActivity
Camera(照相機)的啟動方法為:
# am start -n com.android.camera/com.android.camera.Camera
Browser(瀏覽器)的啟動方法為:
# am start -n com.android.browser/com.android.browser.BrowserActivity
啟動瀏覽器 :
am start -a android.intent.action.VIEW -d  http://www.google.cn/
撥打電話 :
am start -a android.intent.action.CALL -d tel:10086
啟動 google map 直接定位到北京 :
am start -a android.intent.action.VIEW geo:0,0?q=beijing


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM