Skip to content

Commit

Permalink
Make mill show skip -j prefixes to ensure machine readability (#2884
Browse files Browse the repository at this point in the history
)

This requires us add another method `Logger#rawOutputStream`, which the
various implementations forward without decorating with prefixes or
other things.

We cannot just use `SystemStreams.original` because when running Mill in
client/server mode we still need to use the `OutputStream` that gets
forwarded from server to client, as the `SystemStreams.original.out`
just ends up forwarded to `out/mill-worker-hash-1/stdout` files that
aren't surfaced to users

Tested manually with `./mill -i dev.run tmp -i -j 2 show foo` on an
example build in the `tmp/` folder. Previously it would show the output
of `show` with a prefix e.g. `[#0] "bar"`, after this PR it shows the
output without any prefixes `"bar"`

`Logger#rawOutputStream` has to be a non-abstract method for binary
compatibility. This makes it a bit error prone since you can easily
forget to override it where necessary. So I first made it abstract, made
sure everyone overrides it, and then added the default case.

Pull request: #2884
  • Loading branch information
lihaoyi authored Nov 19, 2023
1 parent 69b2199 commit d6f56f8
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bsp/src/mill/bsp/BspContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ private[mill] class BspContext(
override def error(s: String): Unit = streams.err.println(s)
override def ticker(s: String): Unit = streams.err.println(s)
override def debug(s: String): Unit = streams.err.println(s)

override def debugEnabled: Boolean = true

override def rawOutputStream: PrintStream = systemStreams.out
}

BspWorker(os.pwd, home, log).flatMap { worker =>
Expand Down
8 changes: 8 additions & 0 deletions main/api/src/mill/api/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ trait Logger {

def errorStream: PrintStream = systemStreams.err
def outputStream: PrintStream = systemStreams.out

/**
* [[rawOutputStream]] is intended to be a version of [[outputStream]]
* without decoration: colors, prefixes, timestamps, etc. It is intended
* for the use of tasks like `show` which output data in a way that is
* easily readable by downstream programs.
*/
def rawOutputStream: PrintStream = systemStreams.out
def inStream: InputStream = systemStreams.in

def info(s: String): Unit
Expand Down
3 changes: 3 additions & 0 deletions main/eval/src/mill/eval/GroupEvaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import mill.define._
import mill.eval.Evaluator.TaskResult
import mill.util._

import java.io.PrintStream
import scala.collection.mutable
import scala.reflect.NameTransformer.{encode, decode}
import scala.util.DynamicVariable
Expand Down Expand Up @@ -274,6 +275,8 @@ private[mill] trait GroupEvaluator {
if (enableTicker) super.ticker(tickerPrefix.getOrElse("") + s)
else () // do nothing
}

override def rawOutputStream: PrintStream = logger.rawOutputStream
}
// This is used to track the usage of `T.dest` in more than one Task
// But it's not really clear what issue we try to prevent here
Expand Down
2 changes: 1 addition & 1 deletion main/src/mill/main/MainModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object MainModule {
case Right((watched, Right(res))) =>
val output = f(res)
watched.foreach(watch0)
log.outputStream.println(output.render(indent = 2))
log.rawOutputStream.println(output.render(indent = 2))
Result.Success(output)
}
}
Expand Down
1 change: 1 addition & 0 deletions main/util/src/mill/util/DummyLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object DummyLogger extends Logger {
new PrintStream(_ => ()),
new ByteArrayInputStream(Array())
)
override def rawOutputStream = systemStreams.out

def info(s: String) = ()
def error(s: String) = ()
Expand Down
2 changes: 2 additions & 0 deletions main/util/src/mill/util/FileLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ class FileLogger(
if (outputStreamUsed)
outputStream.close()
}

override def rawOutputStream: PrintStream = outputStream
}
2 changes: 2 additions & 0 deletions main/util/src/mill/util/MultiLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class MultiLogger(
logger1.close()
logger2.close()
}

override def rawOutputStream: PrintStream = systemStreams.out
}

class MultiStream(stream1: OutputStream, stream2: OutputStream)
Expand Down
2 changes: 2 additions & 0 deletions main/util/src/mill/util/PrefixLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class PrefixLogger(
logger0.systemStreams.in
)

override def rawOutputStream = logger0.rawOutputStream

override def info(s: String): Unit = logger0.info(context + s)
override def error(s: String): Unit = logger0.error(context + s)
override def ticker(s: String): Unit = logger0.ticker(context + tickerContext + s)
Expand Down
2 changes: 2 additions & 0 deletions main/util/src/mill/util/PrintLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class PrintLogger(
systemStreams.err.println(context + s)
}
}

override def rawOutputStream: PrintStream = systemStreams.out
}

object PrintLogger {
Expand Down
4 changes: 4 additions & 0 deletions main/util/src/mill/util/ProxyLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mill.util

import mill.api.Logger

import java.io.PrintStream

/**
* A Logger that forwards all logging to another Logger. Intended to be
* used as a base class for wrappers that modify logging behavior.
Expand All @@ -19,4 +21,6 @@ class ProxyLogger(logger: Logger) extends Logger {
override def debugEnabled: Boolean = logger.debugEnabled

override def close() = logger.close()

override def rawOutputStream: PrintStream = logger.rawOutputStream
}

0 comments on commit d6f56f8

Please sign in to comment.