Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Forward standard output to logger #2361

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)
}
tgodzik marked this conversation as resolved.
Show resolved Hide resolved
} catch {
case t: StackOverflowError =>
val msg = "Encountered a StackOverflowError coming from the compiler. You might need to restart your Bloop build server"
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
}
}
Loading