Handy function to get a loggable stack trace from a Throwable @param tr An exception to log
(Throwable tr)
| 315 | * @param tr An exception to log |
| 316 | */ |
| 317 | public static String getStackTraceString(Throwable tr) { |
| 318 | if (tr == null) { |
| 319 | return ""; |
| 320 | } |
| 321 | |
| 322 | // This is to reduce the amount of log spew that apps do in the non-error |
| 323 | // condition of the network being unavailable. |
| 324 | Throwable t = tr; |
| 325 | while (t != null) { |
| 326 | if (t instanceof UnknownHostException) { |
| 327 | return ""; |
| 328 | } |
| 329 | t = t.getCause(); |
| 330 | } |
| 331 | |
| 332 | StringWriter sw = new StringWriter(); |
| 333 | PrintWriter pw = new FastPrintWriter(sw, false, 256); |
| 334 | tr.printStackTrace(pw); |
| 335 | pw.flush(); |
| 336 | return sw.toString(); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Low-level logging call. |