2009年8月5日 星期三

Android Logcat

用 adb logcat -help 可以看到使用說明,另外http://jsharkey.org/downloads/coloredlogcat.pytxt是一個Modify the Android logcat stream for full-color debugging的工具使用方式

adb logcat | ~/coloredlogcat.py

以下是使用了adb logcat 與coloredlogcat.py 的logcat 輸出:

607 dalvikvm D GC freed 9300 objects / 524328 bytes in 145ms
567 ActivityManager I Starting activity: Intent { data=content://org
.jsharkey.sky/appwidgets/32 comp={org.jsharkey
.sky/org.jsharkey.sky.DetailsActivity} }
567 ActivityManager W startActivity called from non-Activity context
; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: I
ntent { data=content://org.jsharkey.sky/appwid
gets/32 flags=0x800000 comp={org.jsharkey.sky/
org.jsharkey.sky.DetailsActivity} }
567 ActivityManager I Starting activity: Intent { action=android.int
ent.action.VIEW data=http://forecast.weather.g
ov/MapClick.php?textField1=41.90&textField2=-8
7.65 comp={com.android.browser/com.android.bro
wser.BrowserActivity} }
567 InputManagerService W Starting input on non-focused client com.andro
id.internal.view.IInputMethodClient$Stub$Proxy
@436c75f0 (uid=10022 pid=961)
982 InetAddress D forecast.weather.gov: 140.90.113.200 (family 2
, proto 6)
982 InetAddress D www.google.com: 64.233.189.147 (family 2, prot
o 6)
982 InetAddress D www.google.com: 64.233.189.104 (family 2, prot
o 6)
982 InetAddress D www.google.com: 64.233.189.99 (family 2, proto
6)
610 dalvikvm D GC freed 726 objects / 32928 bytes in 325ms
982 InetAddress D radar.weather.gov: 204.227.127.200 (family 2,
proto 6)
982 InetAddress D www.weather.gov: 140.90.113.200 (family 2, pro
to 6)
982 dalvikvm D GC freed 3724 objects / 350448 bytes in 165ms
961 dalvikvm D GC freed 1101 objects / 46080 bytes in 236ms

每一行log 內容分成四個欄位(內定是使用brief log format):pid ,tag ,priority,message


而在Java程式中如何讓訊息輸出到logcat stream呢?答案是使用android.util.Log中所提供的一些methods(http://developer.android.com/reference/android/util/Log.html):


Log.v(String tag, String msg);
Log.d(String tag, String msg);
Log.i(String tag, String msg);
Log.w(String tag, String msg);
Log.e(String tag, String msg);

tag:指log內容的tag欄位
msg:指log內容的message欄位


使用adb logcat -s [tag]:[priority]僅顯示要看的訊息,例如:

adb logcat -s MyAndroid:I

这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.


referenc:

http://developer.android.com/guide/developing/tools/adb.html
http://jsharkey.org/blog/
http://developer.android.com/reference/android/util/Log.html
http://www.androidlab.cn/archiver/?tid-828.html

沒有留言: