Use IO.Newline for stack trace

This commit is contained in:
Eugene Yokota 2019-03-07 16:39:47 -05:00
parent 15ea0109b8
commit 8c85744d67
1 changed files with 23 additions and 10 deletions

View File

@ -3,6 +3,9 @@
*/ */
package sbt.internal.util package sbt.internal.util
import sbt.io.IO
import scala.collection.mutable.ListBuffer
object StackTrace { object StackTrace {
def isSbtClass(name: String) = name.startsWith("sbt.") || name.startsWith("xsbt.") def isSbtClass(name: String) = name.startsWith("sbt.") || name.startsWith("xsbt.")
@ -18,9 +21,9 @@ object StackTrace {
* where the line for the Throwable is counted plus one line for each stack element. * where the line for the Throwable is counted plus one line for each stack element.
* Less lines will be included if there are not enough stack elements. * Less lines will be included if there are not enough stack elements.
*/ */
def trimmed(t: Throwable, d: Int): String = { def trimmedLines(t: Throwable, d: Int): List[String] = {
require(d >= 0) require(d >= 0)
val b = new StringBuilder() val b = new ListBuffer[String]()
def appendStackTrace(t: Throwable, first: Boolean): Unit = { def appendStackTrace(t: Throwable, first: Boolean): Unit = {
@ -33,16 +36,12 @@ object StackTrace {
} }
def appendElement(e: StackTraceElement): Unit = { def appendElement(e: StackTraceElement): Unit = {
b.append("\tat ") b.append("\tat " + e)
b.append(e)
b.append('\n')
() ()
} }
if (!first) if (!first) b.append("Caused by: " + t.toString)
b.append("Caused by: ") else b.append(t.toString)
b.append(t)
b.append('\n')
val els = t.getStackTrace() val els = t.getStackTrace()
var i = 0 var i = 0
@ -59,7 +58,21 @@ object StackTrace {
c = c.getCause() c = c.getCause()
appendStackTrace(c, false) appendStackTrace(c, false)
} }
b.toString() b.toList
} }
/**
* Return a printable representation of the stack trace associated
* with t. Information about t and its Throwable causes is included.
* The number of lines to be included for each Throwable is configured
* via d which should be greater than or equal to 0.
*
* - If d is 0, then all elements are included up to (but not including)
* the first element that comes from sbt.
* - If d is greater than 0, then up to that many lines are included,
* where the line for the Throwable is counted plus one line for each stack element.
* Less lines will be included if there are not enough stack elements.
*/
def trimmed(t: Throwable, d: Int): String =
trimmedLines(t, d).mkString(IO.Newline)
} }