Android中logcat和日志打印


 一、logcat對日志過濾

1.# logcat --help

# logcat --help
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)
  -t '<time>'     print most recent lines since specified time (implies -d)
  -T <count>      print only the most recent <count> lines (does not imply -d)
  -T '<time>'     print most recent lines since specified time (not imply -d)
                  count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'
  -g              get the size of the log's ring buffer and exit
  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio',
                  'events', 'crash' or 'all'. Multiple -b parameters are
                  allowed and results are interleaved. The default is
                  -b main -b system -b crash.
  -B              output the log in binary.
  -S              output statistics.
  -G <size>       set size of log ring buffer, may suffix with K or M.
  -p              print prune white and ~black list. Service is specified as
                  UID, UID/PID or /PID. Weighed for quicker pruning if prefix
                  with ~, otherwise weighed for longevity if unadorned. All
                  other pruning activity is oldest first. Special case ~!
                  represents an automatic quicker pruning for the noisiest
                  UID as determined by the current statistics.
  -P '<list> ...' set prune white and ~black list, using same format as
                  printed above. Must be quoted.

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"
View Code

 

1. logcat命令位置:/system/bin/logcat

2.logcat的常用用法

# logcat *:W 過濾日志則會將該級別及以上的日志輸出

# logcat ActivityManager:I MyApp:D *:S 表示輸出tag=“ActivityManager”的Info以上級別日志,輸出tag=“MyApp”的Debug以上級別日志,及其它tag的Silent級別日志(即屏蔽其它 tag 日志)。

# logcat -d logcat應該是動態打印的,加-d后打印完log后退出,不阻塞
# logcat -t 20 只打印最近的20行log,-T也是類似的功能,但是打印完后不退出。
# logcat -f a.txt -f保存到文件中,不使用重定向符號的
# logcat | grep -i myapp 忽略大小寫。
# logcat | grep --color=auto -i priv-app 匹配到的“priv-app”會顯示出顏色
# logcat | grep --color=auto 1979 只打印出進程PID為1979的log
# logcat -c && logcat
# logcat -v time 打印時加上時間
# logcat -g 打印各個環形緩沖區的大小后退出
# logcat -S 打印出統計信息
# logcat | grep -E "Theft|u-blox" grep -E 正則表達式,可以一次性檢索多個關鍵字
# logcat <tag1>:<priority1> <tag2>:<priority2> 過濾低優先級的日志

# logcat *:E   只查看錯誤打印

3.有 3 種方式使用 logcat 打印日志:
可以通過 IDE(比如 Android Studio)提供的 logcat 窗口查看打印的日志
可以通過 adb 命令使用 logcat 在本地計算機進行日志查看
通過 adb 命令進入模擬器或者所連設備的 shell 命令窗口,進行 logcat 的操作和使用

4.設置輸出日志等級
通過設置優先級,過濾掉低優先級的日志,使用方式如: adb logcat <tag1>:<priority1> <tag2>:<priority2>
示例: logcat PowerManagerService:I PackageManager:I 表示logcat時不打印PowerManagerService和PackageManager的優先級低於Info的log

5.logcat選擇打印的緩沖區:
# logcat -b <buffer> 可以指定要打印的環緩沖區,“main”,“system”,“radio”或“events”。 允許多個-b連用,這樣打印的結果是交錯的,默認為: -b main -b system -b crash。
radio - 查看緩沖區的相關的信息.
events - 查看和事件相關的的緩沖區.
main - 查看主要的日志緩沖區.
system - 與系統相關的日志信息.

6. logcat默認的幾種輸出格式為

格式:logcat -v threadtime

(1)brief格式為:<priority>/<tag>(<pid>): <message>  eg: D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0

(2)process格式為:<priority>(<pid>) <message>  eg: D( 1785) Disconnected process message: 10, size: 0 (HeadsetStateMachine)

(3)tag格式為:<priority>/<tag>: <message>  eg: D/HeadsetStateMachine: Disconnected process message: 10, size: 0

(4)raw格式為:<message>   eg: Disconnected process message: 10, size: 0

(5)time格式為:<datetime> <priority>/<tag>(<pid>): <message>  eg: 08-28 22:39:39.974 D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0

(6)threadtime格式為:<datetime> <pid> <tid> <priority> <tag>: <message>  eg: 08-28 22:39:39.974 1785 1832 D HeadsetStateMachine: Disconnected process message: 10, size: 0

(7)long格式為:[ <datetime> <pid>:<tid> <priority>/<tag> ] <message>  eg: [ 08-28 22:39:39.974 1785: 1832 D/HeadsetStateMachine ] Disconnected process  message: 10, size: 0

 

二、日志打印函數

1.程序中使用ALOGx(),SLOGx(),RLOGx()來分別打印app,system,radio的log,

x表示下面6種打印級別:
V Verbose
D Debug
I Info
W Warn
E Error
F Fatal

比如:
#define LOG_TAG "LedHal"
ALOGI("hal_led_ioctl called: which=%d, status=%d", which, status);
打印如下:

I/LedHal ( 2284): hal_led_ioctl called: which=0, status=0 //打印等級/Tag (PID): message

 

三、日志統計

1.# logcat -b all -S 可以統計出各個log buffer中最健談的log的進程、UID、TAG

# logcat -b all -S
size/num main               radio              events             system             crash  stats    performance   security           kernel             Total
Total    4294940/52209      1224624/6903       386275/3669        1197331/12884      165/3   0/0     9132/180      0/0                0/0                7112467/75848
Now      261517/3561        257987/1383        252696/2824        246225/2728        165/3           9132/180                                            1027722/10679
Logspan  1:09:38.987(3.9%)  2:15:44.731(24.2%) 2:15:41.166(96.9%) 2:15:44.791(37%)   3:03.21         2:10:35.716                                         2:15:48.058
Overhead 460933             335435             410840             398993             333             19212                                               1775074
update time=0, report time=0, buffer rate = 0 Byte/sec, log rate = 0 sec-1
buffer chatty rate = 15000 Byte/sec, log chatty rate = 100 sec-1

最健談的main log:
Chattiest UIDs in main log buffer:                           Size   +/-  Pruned
UID   PACKAGE                                               BYTES           NUM
10061 com.android.systemui                                  51486  -14%    1256
1000  system                                                40438  -67%    3478
  PID/UID   COMMAND LINE                                       "             "
 1612/1000  system_server                                    5384          2409
 2972/1000  com.coloros.persist.system                       2999           623


最健談的main log:
Chattiest UIDs in radio log buffer:                          Size   +/-  Pruned
UID   PACKAGE                                               BYTES           NUM
1001  radio                                                246546  -3.6%   5766
1000  system                                                11037  5.2X


最健談的event log:
Chattiest UIDs in events log buffer:                         Size   +/-  Pruned
UID   PACKAGE                                               BYTES           NUM
1000  system                                               231850  +4.3%    260
  PID/UID   COMMAND LINE                                       "             "
 1612/1000  system_server                                  153760            14


最健談的system log:
Chattiest UIDs in system log buffer:                         Size   +/-  Pruned
UID   PACKAGE                                               BYTES           NUM
1000  system                                               180236  -20%    8572
  PID/UID   COMMAND LINE                                       "             "
 1612/1000  system_server                                   69620          8238
 2972/1000  com.coloros.persist.system                      68819           324


最健談的crash log:
Chattiest UIDs in crash log buffer:                          Size
UID   PACKAGE                                               BYTES
0     root                                                    165


最健談的performance log:
Chattiest UIDs in performance log buffer:                    Size
UID   PACKAGE                                               BYTES
1000  system                                                 2909
  PID/UID   COMMAND LINE                                       "
 1612/1000  system_server                                     556


最健談的PID:
Chattiest PIDs:                                              Size        Pruned
  PID/UID   COMMAND LINE                                    BYTES           NUM
 2904/1001  com.android.phone                              270946          5604
 1612/1000  system_server                                  240357         10661


最健談的TID:
Chattiest TIDs:                                              Size        Pruned
  TID/UID   COMM                                            BYTES           NUM
 2904/1001  com.android.phone                              217685          4117
 1765/1000  android.bg                                      78212           147


最健談的events log中的TAG:
Chattiest events log buffer TAGs:                            Size         Prune
    TAG/UID   TAGNAME                                       BYTES           NUM
   1003       auditd                                        89160           390
  30047/1000  am_pss                                        73004


最健談的TAG:
Chattiest TAGs:                                              Size
  TID/PID/UID   LOG_TAG NAME                                BYTES
     2904/1001  SST                                        121294
                auditd                                      89160
View Code


免責聲明!

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



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