Skip to content

Commit

Permalink
Merge pull request #2361 from tgodzik/print-out
Browse files Browse the repository at this point in the history
bugfix: Forward standard output to logger
  • Loading branch information
tgodzik authored Aug 15, 2024
2 parents ba9a03e + c7f3484 commit 3fdeacf
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/src/main/scala/bloop/logging/BloopLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ final class BloopLogger(
val debugFilter: DebugFilter,
originId: Option[String]
) extends Logger {

redirectOutputToLogs(System.out)

override def ansiCodesSupported() = true
override def debug(msg: String)(implicit ctx: DebugFilter): Unit =
if (isVerbose && debugFilter.isEnabledFor(ctx)) print(msg, printDebug)
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 @@ -119,6 +129,11 @@ final class BloopHighLevelCompiler(
if (scalac.scalaInstance.libraryJars().isEmpty) {
throw new CompileFailed(new Array(0), s"Expected Scala library jar in Scala instance containing ${scalac.scalaInstance.allJars().mkString(", ")}", new Array(0))
}
val baos = new ByteArrayOutputStream()
withTee {
_.addListener(baos)
}

try {
scalac.compile(
sources.toArray,
Expand Down Expand Up @@ -150,6 +165,13 @@ final class BloopHighLevelCompiler(
throw new InterfaceCompileCancelled(Array(), "Caught NPE when compilation was cancelled!")
case t => throw t
}
} finally {
withTee { tee =>
val result = baos.toString()
if (result.nonEmpty)
logger.info(baos.toString)
tee.removeListener(baos)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions backend/src/test/scala/bloop/logging/RecordingLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class RecordingLogger(
) extends Logger {
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
32 changes: 32 additions & 0 deletions frontend/src/test/scala/bloop/BaseCompileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ abstract class BaseCompileSpec extends bloop.testing.BaseSuite {
}
}
}
test("compile-with-Vprint:typer") {
TestUtil.withinWorkspace { workspace =>
val sources = List(
"""/main/scala/Foo.scala
|class Foo
""".stripMargin
)

val logger = new RecordingLogger(ansiCodesSupported = false)
val `A` = TestProject(workspace, "a", sources, scalacOptions = List("-Vprint:typer"))
val projects = List(`A`)
val state = loadState(workspace, projects, logger)
val compiledState = state.compile(`A`)
assertExitStatus(compiledState, ExitStatus.Ok)
assertValidCompilationState(compiledState, projects)

assertNoDiff(
logger.infos.filterNot(_.contains("Compiled")).mkString("\n").trim(),
"""|Compiling a (1 Scala source)
|[[syntax trees at end of typer]] // Foo.scala
|package <empty> {
| class Foo extends scala.AnyRef {
| def <init>(): Foo = {
| Foo.super.<init>();
| ()
| }
| }
|}
|""".stripMargin
)
}
}

test("compile a project, delete an analysis and then write it back during a no-op compilation") {
TestUtil.withinWorkspace { workspace =>
Expand Down
6 changes: 6 additions & 0 deletions shared/src/main/scala/bloop/logging/Logger.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package bloop.logging

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

import bloop.io.Environment

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) = {
System.setOut(new TeeOutputStream(out))
}

/** The name of the logger */
def name: String

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 3fdeacf

Please sign in to comment.