Java / Android的 – 如何打印出一个完整的堆栈跟踪?

在Android(Java)中,我如何打印完整的堆栈跟踪? 如果我的应用程序崩溃nullPointerException或者什么,它打印出一个(几乎)完整的堆栈跟踪,如下所示:

java.io.IOException: Attempted read from closed stream. com.android.music.sync.common.SoftSyncException: java.io.IOException: Attempted read from closed stream. at com.android.music.sync.google.MusicSyncAdapter.getChangesFromServerAsDom(MusicSyncAdapter.java:545) at com.android.music.sync.google.MusicSyncAdapter.fetchDataFromServer(MusicSyncAdapter.java:488) at com.android.music.sync.common.AbstractSyncAdapter.download(AbstractSyncAdapter.java:417) at com.android.music.sync.common.AbstractSyncAdapter.innerPerformSync(AbstractSyncAdapter.java:313) at com.android.music.sync.common.AbstractSyncAdapter.onPerformLoggedSync(AbstractSyncAdapter.java:243) at com.google.android.common.LoggingThreadedSyncAdapter.onPerformSync(LoggingThreadedSyncAdapter.java:33) at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:164) Caused by: java.io.IOException: Attempted read from closed stream. at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:148) at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:212) at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81) at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:64) at android.net.http.AndroidHttpClient.getUngzippedContent(AndroidHttpClient.java:218) at com.android.music.sync.api.MusicApiClientImpl.createAndExecuteMethod(MusicApiClientImpl.java:312) at com.android.music.sync.api.MusicApiClientImpl.getItems(MusicApiClientImpl.java:588) at com.android.music.sync.api.MusicApiClientImpl.getTracks(MusicApiClientImpl.java:638) at com.android.music.sync.google.MusicSyncAdapter.getChangesFromServerAsDom(MusicSyncAdapter.java:512) ... 6 more 

不过有时候,为了debugging目的,我想从代码中的位置logging完整的堆栈跟踪。 我想我可以这样做:

 StackTraceElement trace = new Exception().getStackTrace(); Log.d("myapp", trace.toString()); 

但是这只是打印出指向对象的指针…我是否必须遍历所有的堆栈跟踪元素才能打印出来? 还是有一个简单的方法打印出来呢?

(String tag, String msg, Throwable tr)签名的所有日志方法都会覆盖。

传递一个exception作为第三个参数应该给你logcat中的完整堆栈跟踪。

以下应该做的伎俩:

 Log.d("myapp", Log.getStackTraceString(new Exception())); 

使用Log.getStackTraceString(Throwable t)。 通过深入挖掘可以获得更长的堆栈轨迹。 例如:

 try { ... } catch(Exception e) { Log.d("Some tag", Log.getStackTraceString(e.getCause().getCause())); } 

Retreived from http://developer.android.com/reference/android/util/Log.html#getStackTraceString%28java.lang.Throwable%29

你可以使用这个:

 public static String toString(StackTraceElement[] stackTraceElements) { if (stackTraceElements == null) return ""; StringBuilder stringBuilder = new StringBuilder(); for (StackTraceElement element : stackTraceElements) stringBuilder.append(element.toString()).append("\n"); return stringBuilder.toString(); } 
 private static String buildStackTraceString(final StackTraceElement[] elements) { StringBuilder sb = new StringBuilder(); if (elements != null && elements.length > 0) { for (StackTraceElement element : elements) { sb.append(element.toString()); } } return sb.toString(); } // call this at your check point Log.d(TAG, buildStackTraceString(Thread.currentThread().getStackTrace()));