Created
February 15, 2012 20:19
-
-
Save ThomasAlexandre/1838734 to your computer and use it in GitHub Desktop.
Timing and Logging from REPL
This file contains hidden or 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
:power | |
def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B = | |
try { f(param) } finally { param.close() } | |
def appendToFile(fileName:String, textData:String) = | |
using (new java.io.FileWriter(fileName, true)){ | |
fileWriter => using (new java.io.PrintWriter(fileWriter)) { | |
printWriter => printWriter.println(textData) | |
} | |
} | |
def timedAndLogged[T](body: => T): T = { | |
val start = System.nanoTime | |
try { | |
val result = body | |
appendToFile("/tmp/repl.log",result.toString) | |
result | |
} | |
finally println(" "+(System.nanoTime - start) + " nanos elapsed. ") | |
} | |
:wrap timedAndLogged | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment