Merge pull request #106 from eed3si9n/wip/stacktrace

Fix ConsoleAppender to show full stack trace
This commit is contained in:
Dale Wijnand 2017-07-26 09:01:11 +01:00 committed by GitHub
commit 3104ae0d57
2 changed files with 33 additions and 7 deletions

View File

@ -279,6 +279,15 @@ class ConsoleAppender private[ConsoleAppender] (
private val SUCCESS_MESSAGE_COLOR = reset
private val NO_COLOR = reset
private var traceEnabledVar: Int = Int.MaxValue
def setTrace(level: Int): Unit = synchronized { traceEnabledVar = level }
/**
* Returns the number of lines for stacktrace.
*/
def getTrace: Int = synchronized { traceEnabledVar }
override def append(event: XLogEvent): Unit = {
val level = ConsoleAppender.toLevel(event.getLevel)
val message = event.getMessage
@ -383,11 +392,27 @@ class ConsoleAppender private[ConsoleAppender] (
case _ => appendLog(level, msg.getFormattedMessage)
}
private def appendTraceEvent(te: TraceEvent): Unit = {
val traceLevel = getTrace
val throwableShowLines: ShowLines[Throwable] =
ShowLines[Throwable]( (t: Throwable) => {
List(StackTrace.trimmed(t, traceLevel))
})
val codec: ShowLines[TraceEvent] =
ShowLines[TraceEvent]( (t: TraceEvent) => {
throwableShowLines.showLines(t.message)
})
codec.showLines(te).toVector foreach { appendLog(Level.Error, _) }
}
private def appendMessageContent(level: Level.Value, o: AnyRef): Unit = {
def appendEvent(oe: ObjectEvent[_]): Unit =
{
val contentType = oe.contentType
LogExchange.stringCodec[AnyRef](contentType) match {
if (contentType == "sbt.internal.util.TraceEvent") {
appendTraceEvent(oe.message.asInstanceOf[TraceEvent])
}
else LogExchange.stringCodec[AnyRef](contentType) match {
case Some(codec) if contentType == "sbt.internal.util.SuccessEvent" =>
codec.showLines(oe.message.asInstanceOf[AnyRef]).toVector foreach { success(_) }
case Some(codec) =>

View File

@ -9,12 +9,13 @@ object StackTrace {
* 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.
* 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 = {
require(d >= 0)