back all ConsoleLoggers by a common ConsoleOut

The common ConsoleOut merges (overwrites) consecutive Resolving xxxx ... lines
when ansi codes are enabled.
This commit is contained in:
Mark Harrah 2012-09-01 09:56:09 -04:00
parent 2e8fdbdf05
commit d17de8e83a
3 changed files with 44 additions and 3 deletions

View File

@ -8,6 +8,29 @@ package sbt
object ConsoleLogger
{
def systemOut: ConsoleOut = printStreamOut(System.out)
def overwriteContaining(s: String): (String,String) => Boolean = (cur, prev) =>
cur.contains(s) && prev.contains(s)
/** ConsoleOut instance that is backed by System.out. It overwrites the previously printed line
* if the function `f(lineToWrite, previousLine)` returns true.
*
* The ConsoleOut returned by this method assumes that the only newlines are from println calls
* and not in the String arguments. */
def systemOutOverwrite(f: (String,String) => Boolean): ConsoleOut = new ConsoleOut {
val lockObject = System.out
private[this] var last: Option[String] = None
private[this] var current = new java.lang.StringBuffer
def print(s: String): Unit = synchronized { current.append(s) }
def println(s: String): Unit = synchronized { current.append(s); println() }
def println(): Unit = synchronized {
val s = current.toString
if(formatEnabled && last.exists(lmsg => f(s, lmsg)))
System.out.print(OverwriteLine)
System.out.println(s)
last = Some(s)
current = new java.lang.StringBuffer
}
}
def printStreamOut(out: PrintStream): ConsoleOut = new ConsoleOut {
val lockObject = out
def print(s: String) = out.print(s)
@ -30,6 +53,9 @@ object ConsoleLogger
/** Escape character, used to introduce an escape sequence. */
final val ESC = '\u001B'
/** Move to beginning of previous line and clear the line. */
private[sbt] final val OverwriteLine = "\r\u001BM\u001B[2K"
/** An escape terminator is a character in the range `@` (decimal value 64) to `~` (decimal value 126).
* It is the final character in an escape sequence. */
def isEscapeTerminator(c: Char): Boolean =

View File

@ -19,9 +19,12 @@ object GlobalLogBacking
}
object GlobalLogging
{
@deprecated("Explicitly specify standard out.", "0.13.0")
def initial(newLogger: (PrintWriter, GlobalLogBacking) => GlobalLogging, newBackingFile: => File): GlobalLogging =
initial(newLogger, newBackingFile, ConsoleLogger.systemOut)
def initial(newLogger: (PrintWriter, GlobalLogBacking) => GlobalLogging, newBackingFile: => File, console: ConsoleOut): GlobalLogging =
{
val log = ConsoleLogger()
val log = ConsoleLogger(console)
GlobalLogging(log, log, GlobalLogBacking(newLogger, newBackingFile))
}
}

View File

@ -18,17 +18,29 @@ object MainLogging
multi: Logger
}
def globalDefault(writer: PrintWriter, backing: GlobalLogBacking): GlobalLogging =
globalDefault(writer, backing, ConsoleLogger.systemOut)
def globalDefault(writer: PrintWriter, backing: GlobalLogBacking, console: ConsoleOut): GlobalLogging =
{
val backed = defaultBacked()(writer)
val full = multiLogger(defaultMultiConfig( backed ) )
val full = multiLogger(defaultMultiConfig(console, backed ) )
GlobalLogging(full, backed, backing)
}
@deprecated("Explicitly specify the console output.", "0.13.0")
def defaultMultiConfig(backing: AbstractLogger): MultiLoggerConfig =
new MultiLoggerConfig(defaultScreen(ConsoleLogger.noSuppressedMessage), backing, Nil, Level.Info, Level.Debug, -1, Int.MaxValue)
defaultMultiConfig(ConsoleLogger.systemOut, backing)
def defaultMultiConfig(console: ConsoleOut, backing: AbstractLogger): MultiLoggerConfig =
new MultiLoggerConfig(defaultScreen(console, ConsoleLogger.noSuppressedMessage), backing, Nil, Level.Info, Level.Debug, -1, Int.MaxValue)
@deprecated("Explicitly specify the console output.", "0.13.0")
def defaultScreen(): AbstractLogger = ConsoleLogger()
@deprecated("Explicitly specify the console output.", "0.13.0")
def defaultScreen(suppressedMessage: SuppressedTraceContext => Option[String]): AbstractLogger = ConsoleLogger(suppressedMessage = suppressedMessage)
def defaultScreen(console: ConsoleOut): AbstractLogger = ConsoleLogger(console)
def defaultScreen(console: ConsoleOut, suppressedMessage: SuppressedTraceContext => Option[String]): AbstractLogger =
ConsoleLogger(console, suppressedMessage = suppressedMessage)
def defaultBacked(useColor: Boolean = ConsoleLogger.formatEnabled): PrintWriter => ConsoleLogger =
to => ConsoleLogger(ConsoleLogger.printWriterOut(to), useColor = useColor)