summaryrefslogtreecommitdiff
path: root/src/library/scala/sys/process/ProcessLogger.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-02-12 20:08:21 +0000
committerPaul Phillips <paulp@improving.org>2011-02-12 20:08:21 +0000
commit1c5d8d2e689769cbd5aa106cacb8b3c3af0c0451 (patch)
treed199a39086ff0a5dbe7b7da240bbca772a97ec88 /src/library/scala/sys/process/ProcessLogger.scala
parent02fd6b6139dce48fffe46785fb6b588690885b26 (diff)
downloadscala-1c5d8d2e689769cbd5aa106cacb8b3c3af0c0451.tar.gz
scala-1c5d8d2e689769cbd5aa106cacb8b3c3af0c0451.tar.bz2
scala-1c5d8d2e689769cbd5aa106cacb8b3c3af0c0451.zip
Fixed all the forms of process input/output red...
Fixed all the forms of process input/output redirection so the exit code which makes it out is the exit code of the process. Also changing names to be internally consistent and trying to prune pieces which don't make so much sense without sbt around. Started on documentation. No review.
Diffstat (limited to 'src/library/scala/sys/process/ProcessLogger.scala')
-rw-r--r--src/library/scala/sys/process/ProcessLogger.scala52
1 files changed, 47 insertions, 5 deletions
diff --git a/src/library/scala/sys/process/ProcessLogger.scala b/src/library/scala/sys/process/ProcessLogger.scala
index 309c9ef2b5..1ce77c9196 100644
--- a/src/library/scala/sys/process/ProcessLogger.scala
+++ b/src/library/scala/sys/process/ProcessLogger.scala
@@ -9,16 +9,58 @@
package scala.sys
package process
+import java.io._
+
+/** Encapsulates the output and error streams of a running process.
+ * Many of the methods of ProcessBuilder accept a ProcessLogger as
+ * an argument.
+ *
+ * @see ProcessBuilder
+ */
trait ProcessLogger {
- def info(s: => String): Unit
- def error(s: => String): Unit
+ /** Will be called with each line read from the process output stream.
+ */
+ def out(s: => String): Unit
+
+ /** Will be called with each line read from the process error stream.
+ */
+ def err(s: => String): Unit
+
+ /** If a process is begun with one of these ProcessBuilder methods:
+ *
+ * def !(log: ProcessLogger): Int
+ * def !<(log: ProcessLogger): Int
+ *
+ * The run will be wrapped in a call to buffer. This gives the logger
+ * an opportunity to set up and tear down buffering. At present the
+ * library implementations of ProcessLogger simply execute the body unbuffered.
+ */
def buffer[T](f: => T): T
}
+class FileProcessLogger(file: File) extends ProcessLogger with Closeable with Flushable {
+ private val writer = (
+ new PrintWriter(
+ new BufferedWriter(
+ new OutputStreamWriter(
+ new FileOutputStream(file, true)
+ )
+ )
+ )
+ )
+ def out(s: => String): Unit = writer println s
+ def err(s: => String): Unit = writer println s
+ def buffer[T](f: => T): T = f
+ def close(): Unit = writer.close()
+ def flush(): Unit = writer.flush()
+}
+
object ProcessLogger {
- def apply(fn: String => Unit): ProcessLogger = new ProcessLogger {
- def info(s: => String): Unit = fn(s)
- def error(s: => String): Unit = fn(s)
+ def apply(file: File): FileProcessLogger = new FileProcessLogger(file)
+ def apply(fn: String => Unit): ProcessLogger = apply(fn, fn)
+ def apply(fout: String => Unit, ferr: String => Unit): ProcessLogger = new ProcessLogger {
+ def out(s: => String): Unit = fout(s)
+ def err(s: => String): Unit = ferr(s)
def buffer[T](f: => T): T = f
}
}