-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathSavedLogs.scala
52 lines (43 loc) · 1.54 KB
/
SavedLogs.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package scala.build
import java.io.{ByteArrayOutputStream, PrintStream}
import sbt._
import Keys._
import sbt.internal.util.ConsoleAppender
import scala.collection.mutable
/** Save MiMa logs so they don't get lost in lots of debug output */
object SavedLogs {
val savedLogs = new mutable.HashMap[String, ByteArrayOutputStream]
val showSavedLogs = TaskKey[Unit]("showSavedLogs", "Print all saved logs to the console")
val clearSavedLogs = TaskKey[Unit]("clearSavedLogs", "Clear all saved logs")
def showSavedLogsImpl(println: String => Unit): Unit = synchronized {
savedLogs.foreach {
case (k, os) =>
val log = new String(os.toByteArray)
if (log.nonEmpty) {
println(s"Saved log of $k:")
println(log)
}
}
}
def clearSavedLogsImpl(): Unit = synchronized { savedLogs.clear() }
lazy val settings = Seq[Setting[_]](
(Global / extraAppenders) := {
val previous = (Global / extraAppenders).value
(key: ScopedKey[_]) => {
key.scope match {
case Scope(Select(ProjectRef(_, p)), _, Select(t), _) if t.label == "mimaReportBinaryIssues" =>
val os = new ByteArrayOutputStream
val a = ConsoleAppender(new PrintStream(os, true))
SavedLogs.synchronized { savedLogs.put(s"$p/${t.label}", os) }
a +: previous(key)
case _ => previous(key)
}
}
},
showSavedLogs := {
val log = streams.value.log
showSavedLogsImpl(s => log.info(s))
},
clearSavedLogs := { clearSavedLogsImpl() }
)
}