From 930489eba3d9b8b660ef8e1da50c92591f504825 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Wed, 26 Jul 2017 00:03:07 -0400 Subject: [PATCH] Fix ConsoleAppender to show full stack trace This is modification of crash log event logging that was added in sbt/util#85. Instead of using the hardcoded 0 as the default value, this introduces `setTrace(..)` to `ConsoleAppender` like `BasicLogger`. Also the default value is set to `Int.MaxValue` that will display the full stack trace. Fixes sbt/sbt#3343 --- .../sbt/internal/util/ConsoleAppender.scala | 27 ++++++++++++++++++- .../scala/sbt/internal/util/StackTrace.scala | 13 ++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/ConsoleAppender.scala b/internal/util-logging/src/main/scala/sbt/internal/util/ConsoleAppender.scala index 14cfe6ab2..ef392e785 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/ConsoleAppender.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/ConsoleAppender.scala @@ -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) => diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/StackTrace.scala b/internal/util-logging/src/main/scala/sbt/internal/util/StackTrace.scala index e636d914c..20821eefb 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/StackTrace.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/StackTrace.scala @@ -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)