-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Forward standard output to logger
Causes VirtusLab/scala-cli#1023
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
shared/src/main/scala/bloop/logging/DuplicatingOutputStream.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package bloop.logging | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.io.IOException | ||
import java.io.OutputStream | ||
|
||
import scala.util.control.NonFatal | ||
|
||
final class DuplicatingOutputStream( | ||
val stdout: OutputStream, | ||
val logged: ByteArrayOutputStream, | ||
logger: Logger | ||
) extends OutputStream { | ||
|
||
private val cache = new StringBuilder | ||
if (stdout == null || logged == null) { | ||
throw new NullPointerException() | ||
} | ||
|
||
@throws[IOException] | ||
override def write(b: Int): Unit = { | ||
stdout.write(b) | ||
logged.write(b) | ||
logAndReset() | ||
} | ||
|
||
@throws[IOException] | ||
override def write(b: Array[Byte]): Unit = { | ||
stdout.write(b) | ||
logged.write(b) | ||
logAndReset() | ||
} | ||
|
||
@throws[IOException] | ||
override def write(b: Array[Byte], off: Int, len: Int): Unit = { | ||
stdout.write(b, off, len) | ||
logged.write(b, off, len) | ||
logAndReset() | ||
} | ||
|
||
private def logAndReset() = synchronized { | ||
try { | ||
cache.append(logged.toString()) | ||
logged.reset() | ||
val logMessage = cache.toString.reverse.dropWhile(_ != '\n').reverse.trim() | ||
|
||
if (logMessage != "") { | ||
cache.delete(0, logMessage.size + 1) | ||
logger.info(logMessage) | ||
} | ||
} catch { | ||
case NonFatal(_) => | ||
} | ||
} | ||
|
||
@throws[IOException] | ||
override def flush(): Unit = { | ||
stdout.flush() | ||
logged.flush() | ||
} | ||
|
||
@throws[IOException] | ||
override def close(): Unit = { | ||
try { | ||
stdout.close() | ||
} finally { | ||
logged.close() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters