From d17de8e83a999b4833d816d9b53eb299dff424bb Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Sat, 1 Sep 2012 09:56:09 -0400 Subject: [PATCH] back all ConsoleLoggers by a common ConsoleOut The common ConsoleOut merges (overwrites) consecutive Resolving xxxx ... lines when ansi codes are enabled. --- util/log/ConsoleLogger.scala | 26 ++++++++++++++++++++++++++ util/log/GlobalLogging.scala | 5 ++++- util/log/MainLogging.scala | 16 ++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/util/log/ConsoleLogger.scala b/util/log/ConsoleLogger.scala index bc48d7ad2..711489c5c 100644 --- a/util/log/ConsoleLogger.scala +++ b/util/log/ConsoleLogger.scala @@ -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 = diff --git a/util/log/GlobalLogging.scala b/util/log/GlobalLogging.scala index e54b00a00..f712dd88a 100644 --- a/util/log/GlobalLogging.scala +++ b/util/log/GlobalLogging.scala @@ -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)) } } \ No newline at end of file diff --git a/util/log/MainLogging.scala b/util/log/MainLogging.scala index d7b45e043..9e024ef28 100644 --- a/util/log/MainLogging.scala +++ b/util/log/MainLogging.scala @@ -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)