|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package dotty.tools.dotc.util |
| 14 | + |
| 15 | +import collection.mutable, mutable.ListBuffer |
| 16 | +import scala.util.chaining.given |
| 17 | +import java.lang.System.lineSeparator |
| 18 | + |
| 19 | +object StackTraceOps: |
| 20 | + |
| 21 | + extension (t: Throwable): |
| 22 | + |
| 23 | + /** Format a stack trace, taking the prefix span satisfying a predicate. |
| 24 | + * |
| 25 | + * The format is similar to the typical case described in the Javadoc |
| 26 | + * for [[java.lang.Throwable#printStackTrace()*]]. |
| 27 | + * If a stack trace is truncated, it will be followed by a line of the form |
| 28 | + * `... 3 elided`, by analogy to the lines `... 3 more` which indicate |
| 29 | + * shared stack trace segments. |
| 30 | + * @param e the exception |
| 31 | + * @param p the predicate to select the prefix |
| 32 | + */ |
| 33 | + def formatStackTracePrefix(p: StackTraceElement => Boolean): String = |
| 34 | + |
| 35 | + type TraceRelation = String |
| 36 | + val Self = new TraceRelation("") |
| 37 | + val CausedBy = new TraceRelation("Caused by: ") |
| 38 | + val Suppressed = new TraceRelation("Suppressed: ") |
| 39 | + |
| 40 | + def header(e: Throwable): String = |
| 41 | + def because = e.getCause match { case null => null ; case c => header(c) } |
| 42 | + def msg = e.getMessage match { case null => because ; case s => s } |
| 43 | + def txt = msg match { case null => "" ; case s => s": $s" } |
| 44 | + s"${e.getClass.getName}$txt" |
| 45 | + |
| 46 | + val seen = mutable.Set.empty[Throwable] |
| 47 | + def unseen(e: Throwable): Boolean = (e != null && !seen(e)).tap(if _ then seen += e) |
| 48 | + |
| 49 | + val lines = ListBuffer.empty[String] |
| 50 | + |
| 51 | + // format the stack trace, skipping the shared trace |
| 52 | + def print(e: Throwable, r: TraceRelation, share: Array[StackTraceElement], indents: Int): Unit = if unseen(e) then |
| 53 | + val trace = e.getStackTrace |
| 54 | + val frames = if share.isEmpty then trace else |
| 55 | + val spare = share.reverseIterator |
| 56 | + val trimmed = trace.reverse.dropWhile(spare.hasNext && spare.next() == _) |
| 57 | + trimmed.reverse |
| 58 | + val prefix = frames.takeWhile(p) |
| 59 | + val margin = " " * indents |
| 60 | + lines += s"${margin}${r}${header(e)}" |
| 61 | + prefix.foreach(frame => lines += s"$margin at $frame") |
| 62 | + |
| 63 | + val traceFramesLenDiff = trace.length - frames.length |
| 64 | + val framesPrefixLenDiff = frames.length - prefix.length |
| 65 | + if traceFramesLenDiff > 0 then |
| 66 | + if framesPrefixLenDiff > 0 then lines += s"$margin ... $framesPrefixLenDiff elided and $traceFramesLenDiff more" |
| 67 | + else lines += s"$margin ... $traceFramesLenDiff more" |
| 68 | + else if framesPrefixLenDiff > 0 then lines += s"$margin ... $framesPrefixLenDiff elided" |
| 69 | + |
| 70 | + print(e.getCause, CausedBy, trace, indents) |
| 71 | + e.getSuppressed.foreach(print(_, Suppressed, frames, indents + 1)) |
| 72 | + end print |
| 73 | + |
| 74 | + print(t, Self, share = Array.empty, indents = 0) |
| 75 | + lines.mkString(lineSeparator) |
| 76 | + end formatStackTracePrefix |
0 commit comments