logcat命令使用方法和查看android系統日誌緩衝區內容的方法_Android

來源:互聯網
上載者:User

*註:可以用 adb logcat > 路徑/檔案名稱 來儲存,
此命令執行之時起的全部日誌資訊到一個檔案裡,ctrl + C 結束日誌輸出;
後面不加 > 路徑/檔案名稱 的話,則在 stdout (終端視窗)中輸出!
例如:$ adb logcat -v long Checkin *:S > ~/案頭/log.txt

一、在 Java 與 C 語言中輸出日誌:
1) Java 代碼在程式中輸出日誌, 使用 android.util.Log 類的以下 5 個方法:
   Log.v()、Log.d()、Log.i()、Log.w()、Log.e()。
   分對應 Verbose、Debug、INFO、Warn、Error 的首字母。
   例如:Log.i( "類::函數名", "日期_時間_源碼檔案名稱_行號_日誌資訊內容" );

2) C 代碼在程式中輸出日誌,使用 log 的 API 函數:
   __android_log_write( 日誌類型宏,日誌標籤字串,日誌令牌內容字串 );
   需要:1. Android.mk 中添加 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
      2. *.c 中添加 #include <android/log.h>
      3. 日誌類型宏有:

複製代碼 代碼如下:

        // Android log priority values, in ascending priority order.
        typedef enum android_LogPriority {
            ANDROID_LOG_UNKNOWN = 0,
            // only for SetMinPriority()
            ANDROID_LOG_DEFAULT,
            ANDROID_LOG_VERBOSE,
            ANDROID_LOG_DEBUG,
            ANDROID_LOG_INFO,
            ANDROID_LOG_WARN,
            ANDROID_LOG_ERROR,
            ANDROID_LOG_FATAL,
            // only for SetMinPriority(); must be last
            ANDROID_LOG_SILENT,
        } android_LogPriority;


二、logcat 使用方法:     
Usage: logcat [options] [filterspecs]
用法:  logcat [選項] [過濾說明]

options include:
選項包含:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:S'
                  設定預設過濾為無聲的。
                  像指定過濾說明為 *:S ,見下面 過濾說明 部份詳述

  -f <filename>   Log to file.
                  Default to stdout
                  輸出日誌到檔案。
                  預設為 stdout

  -r [<kbytes>]   Rotate log every kbytes.
                  (16 if unspecified).
                  Requires -f
                  設定環形日誌緩衝區的kbytes。
                  預設值為16。
                  需要和 -f 選項一起使用

  -n <count>      Sets max number of rotated logs to <count>, default 4
                  設定環形日誌緩衝區的最大數目,預設值是4,需要和 -r 選項一起使用

  -v <format>     Sets the log print format, where <format> is one of:
                  設定 log 的列印格式,  格式有如下主要7種:(不能組合使用)

                  brief
                  process
                  tag
                  thread
                  raw
                  time
                  threadtime
                  long

  -c              clear (flush) the entire log and exit
                  清除所有 log 並退出

  -d              dump the log and then exit (don't block)
                  得到所有log並退出且不阻塞

  -t <count>      print only the most recent <count> lines (implies -d)
                  僅列印最近的由參數 count 指出的行數(必然包含 -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.
                  請求供替換的環形緩衝區,如:main,system,radio,events。
                  多個 -b 參數是被允許,並且結果是交錯輸出的。
                  -b main -b system 是預設的。

  -B              output the log in binary
                  輸出 log 到二進位檔案中。

filterspecs are a series of <tag>[:priority]
過濾說明是一系列 <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
tag 是 eclipse 中 logcat 圖形介面中 Tag 的內容(或者有 * 表示全部),它之後的冒號(:)後面跟優先順序:
    日誌類型標識符(優先順序由低到高排列):
    1. V — Verbose 詳細的 <- 最低優先權
    2. D — Debug   調試
    3. I — Info    訊息
    4. W — Warn    警告
    5. E — Error   錯誤
    6. F — Fatal   致命的
    7. S — Silent  無聲的 <- 最高優先權

'*' means '*:d' and <tag> by itself means <tag>:v
* 意味著 *:d 且 單孤地 tag 意味著 tag:V

If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
如果在命令列上沒有詳細說明,過濾規格即是 ANDROID_LOG_TAGS 結果集。

If no filterspec is found, filter defaults to '*:I'
如果沒有過濾說明,過濾規格預設為 *:I

If not specified with -v, format is set from ANDROID_PRINTF_LOG or defaults to "brief"
如果沒有 -v 指定格式,將是 ANDROID_PRINTF_LOG 或 brief 格式集。

1) 只輸出指定 標籤 和 類型 的日誌
   格式:
   adb logcat <日誌標籤>:<日誌類型標識符> <日誌標籤>:<日誌類型標識符> ... *:S
   註:1. 可以寫多個 <日誌標籤>:<日誌類型標識符> 之間用空格分隔;
     2. 最後必須是 *:S ,表示其它的都不要顯示出來
   例如:
   $ adb logcat dalvikvm:D Checkin:W *:S

   註:adb logcat Checkin *:S =等同於=> adb logcat Checkin:V *:S
   註:以上命令均沒加 -v 來指出日誌格式,即預設為: ANDROID_PRINTF_LOG 或 brief 格式集。

2) 輸出指定 標籤 和 類型 的帶有格式的日誌
註:以下測試日誌內容為:test log format,
  即 eclipse 中的 logcat 圖形介面裡的 Text 中的內容!

1. brief      - 日誌類型/日誌標籤(進程ID): 日誌內容
   例如:$ adb logcat -v brief Checkin *:S
      I/Checkin(24713): test log format
      
2. process    - 日誌類型(進程ID) 日誌內容 (日誌標籤)
   例如:$ adb logcat -v process Checkin *:S
      I(24713) test log format  (Checkin)
      
3. tag        - 日誌類型/日誌標籤: 日誌內容
   例如:$ adb logcat -v tag Checkin *:S
        I/Checkin: test log format

4. thread     - 日誌類型(進程ID:線程ID)
   例如:$ adb logcat -v thread Checkin *:S
        I(24713:0x6089) test log format

5. raw        - 日誌內容
   例如:$ adb logcat -v raw Checkin *:S
        test log format

6. time       - 日期 調用時間 日誌類型/日誌標籤(進程ID): 日誌內容
   例如:$ adb logcat -v time Checkin *:S
   05-27 11:25:33.854 I/Checkin(24713): test log format

7. threadtime - 日期 調用時間 進程ID 線程ID 日誌類型 日誌標籤: 日誌內容
   例如:$ adb logcat -v time Checkin *:S
   05-27 11:25:33.854 24713 24713 I Checkin: test log format
   註:只有此種格式時 線程ID 為十進位數。

8. long       - [ 日期 調用時間 進程ID:線程ID 日誌類型/日誌標籤 ] 轉行顯示 日誌內容
   例如:$ adb logcat -v long Checkin *:S
   [ 05-27 11:25:33.854 24713:0x6089 I/Checkin ]
   test log format

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.