Skip to content

Instantly share code, notes, and snippets.

@ThomasAlexandre
Created February 15, 2012 20:19
Show Gist options
  • Save ThomasAlexandre/1838734 to your computer and use it in GitHub Desktop.
Save ThomasAlexandre/1838734 to your computer and use it in GitHub Desktop.
Timing and Logging from REPL
: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