Permalink
Browse files

defer opening logging output files until an actual write

  • Loading branch information...
1 parent 3dba998 commit 62a3eb48f7d714ba3c2ab46ae8072979eb0320b4 @harrah committed Aug 21, 2012
Showing with 30 additions and 1 deletion.
  1. +1 −1 tasks/standard/Streams.scala
  2. +29 −0 util/io/DeferredWriter.scala
@@ -77,7 +77,7 @@ object Streams
make(a, sid)(f => new BufferedInputStream(new FileInputStream(f)))
def text(sid: String = default): PrintWriter =
- make(a, sid)(f => new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), IO.defaultCharset))) )
+ make(a, sid)(f => new PrintWriter(new DeferredWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), IO.defaultCharset)))) )
def binary(sid: String = default): BufferedOutputStream =
make(a, sid)(f => new BufferedOutputStream(new FileOutputStream(f)))
@@ -0,0 +1,29 @@
+package sbt
+
+ import java.io.Writer
+
+final class DeferredWriter(make: => Writer) extends Writer
+{
+ private[this] var opened = false
+ private[this] var delegate0: Writer = _
+ private[this] def delegate: Writer = synchronized {
+ if(delegate0 eq null) {
+ delegate0 = make
+ opened = true
+ }
+ delegate0
+ }
+ override def close() = synchronized {
+ if(opened) delegate0.close()
+ }
+
+ override def append(c: Char): Writer = delegate.append(c)
+ override def append(csq: CharSequence): Writer = delegate.append(csq)
+ override def append(csq: CharSequence, start: Int, end: Int): Writer = delegate.append(csq, start, end)
+ override def flush() = delegate.flush()
+ override def write(cbuf: Array[Char]) = delegate.write(cbuf)
+ override def write(cbuf: Array[Char], off: Int, len: Int): Unit = delegate.write(cbuf, off, len)
+ override def write(c: Int): Unit = delegate.write(c)
+ override def write(s: String): Unit = delegate.write(s)
+ override def write(s: String, off: Int, len: Int): Unit = delegate.write(s, off, len)
+}

0 comments on commit 62a3eb4

Please sign in to comment.