move StackTrace to util/log

This commit is contained in:
Mark Harrah 2010-06-10 21:08:01 -04:00
parent 1144fb5a27
commit 3033bfec44
3 changed files with 65 additions and 2 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2008, 2009, 2010 Mark Harrah, Jason Zaugg
Copyright (c) 2008, 2009, 2010 Mark Harrah, Tony Sloane, Jason Zaugg
All rights reserved.
Redistribution and use in source and binary forms, with or without

View File

@ -1,3 +1,3 @@
Simple Build Tool: Logging Component
Copyright 2008, 2009 Mark Harrah
Copyright 2008, 2009, 2010 Mark Harrah, Tony Sloane
Licensed under BSD-style license (see LICENSE)

63
util/log/StackTrace.scala Normal file
View File

@ -0,0 +1,63 @@
/* sbt -- Simple Build Tool
* Copyright 2010 Tony Sloane
*/
package sbt
object StackTrace
{
def isSbtClass(name: String) = name.startsWith("sbt") || name.startsWith("xsbt")
/**
* 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 zero. If d is zero,
* then all elements are included up to (but not including) the first
* element that comes from sbt. If d is greater than zero, 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 = {
require(d >= 0)
val b = new StringBuilder ()
def appendStackTrace (t : Throwable, first : Boolean) {
val include : StackTraceElement => Boolean =
if (d == 0)
element => !isSbtClass(element.getClassName)
else {
var count = d - 1
(_ => { count -= 1; count >= 0 })
}
def appendElement (e : StackTraceElement) {
b.append ("\tat ")
b.append (e)
b.append ('\n')
}
if (!first)
b.append ("Caused by: ")
b.append (t)
b.append ('\n')
val els = t.getStackTrace ()
var i = 0
while ((i < els.size) && include (els (i))) {
appendElement (els (i))
i += 1
}
}
appendStackTrace (t, true)
var c = t
while (c.getCause () != null) {
c = c.getCause ()
appendStackTrace (c, false)
}
b.toString ()
}
}