Skip to content

Commit

Permalink
bugfix: Forward standard output to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Jul 16, 2024
1 parent 3d5d6c9 commit b4fe001
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 75 deletions.
2 changes: 1 addition & 1 deletion backend/src/main/scala/bloop/logging/BloopLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class BloopLogger(
originId: Option[String]
) extends Logger {

redirectOutputToLogs(out)
redirectOutputToLogs(System.out)

override def ansiCodesSupported() = true
override def debug(msg: String)(implicit ctx: DebugFilter): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// scalafmt: { maxColumn = 250 }
package sbt.internal.inc.bloop.internal

import java.io.ByteArrayOutputStream
import java.nio.file.Files
import java.util.Optional

import scala.concurrent.Promise
import scala.util.control.NonFatal

import bloop.logging.ObservedLogger
import bloop.logging.TeeOutputStream
import bloop.reporter.ZincReporter
import bloop.task.Task
import bloop.tracing.BraveTracer
Expand Down Expand Up @@ -107,6 +109,14 @@ final class BloopHighLevelCompiler(
}
}

def withTee(block: TeeOutputStream => Unit) = {
System.out match {
case tee: TeeOutputStream =>
block(tee)
case _ =>
}

}
def compileSources(
sources: Seq[VirtualFile],
scalacOptions: Array[String],
Expand All @@ -120,6 +130,11 @@ final class BloopHighLevelCompiler(
throw new CompileFailed(new Array(0), s"Expected Scala library jar in Scala instance containing ${scalac.scalaInstance.allJars().mkString(", ")}", new Array(0))
}
try {
val baos = new ByteArrayOutputStream()
withTee {
_.addListener(baos)
}

scalac.compile(
sources.toArray,
classpath.toArray,
Expand All @@ -132,6 +147,13 @@ final class BloopHighLevelCompiler(
config.progress.toOptional,
logger
)

withTee { tee =>
val result = baos.toString()
if (result.nonEmpty)
logger.info(baos.toString)
tee.removeListener(baos)
}
} catch {
case t: StackOverflowError =>
val msg = "Encountered a StackOverflowError coming from the compiler. You might need to restart your Bloop build server"
Expand Down
1 change: 1 addition & 0 deletions backend/src/test/scala/bloop/logging/RecordingLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class RecordingLogger(
private[this] val messages = new ConcurrentLinkedQueue[(String, String)]

redirectOutputToLogs(System.out)

def clear(): Unit = messages.clear()

def debugs: List[String] = getMessagesAt(Some("debug"))
Expand Down
70 changes: 0 additions & 70 deletions shared/src/main/scala/bloop/logging/DuplicatingOutputStream.scala

This file was deleted.

5 changes: 1 addition & 4 deletions shared/src/main/scala/bloop/logging/Logger.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bloop.logging

import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.function.Supplier

Expand All @@ -10,9 +9,7 @@ abstract class Logger extends xsbti.Logger with BaseSbtLogger {

// Duplicate the standard output so that we get printlns from the compiler
protected def redirectOutputToLogs(out: PrintStream) = {
val baos = new ByteArrayOutputStream()
val duplicating = new DuplicatingOutputStream(out, baos, this)
System.setOut(new PrintStream(duplicating));
System.setOut(new TeeOutputStream(out))
}

/** The name of the logger */
Expand Down
36 changes: 36 additions & 0 deletions shared/src/main/scala/bloop/logging/TeeOutputStream.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package bloop.logging

import java.io.PrintStream
import java.io.ByteArrayOutputStream
import scala.collection.concurrent.TrieMap

class TeeOutputStream(targetPS: PrintStream) extends PrintStream(targetPS) {

private val allStreams = TrieMap.empty[Int, ByteArrayOutputStream];

override def write(b: Int): Unit = {
allStreams.values.foreach(
_.write(b)
)
targetPS.write(b)
}

override def write(buf: Array[Byte], off: Int, len: Int): Unit = {
allStreams.values.foreach(
_.write(buf, off, len)
)
targetPS.write(buf, off, len)
}

override def flush(): Unit = {
targetPS.flush()
}

def addListener(baos: ByteArrayOutputStream): Unit = {
allStreams += baos.hashCode -> baos
}

def removeListener(baos: ByteArrayOutputStream): Unit = {
allStreams -= baos.hashCode
}
}

0 comments on commit b4fe001

Please sign in to comment.