Skip to content

Instantly share code, notes, and snippets.

@virasak
Created July 9, 2010 05:32
Show Gist options
  • Save virasak/469087 to your computer and use it in GitHub Desktop.
Save virasak/469087 to your computer and use it in GitHub Desktop.
error message filter and redirect to error file
trait CompileMessageLogger extends Project {
override def logImpl: Logger = {
val logFile = (info.projectPath / "target" / "error").asFile
new MultiLogger(super.logImpl::new WarnAndErrorLogger(logFile):: Nil)
}
}
import java.io.{File, PrintWriter, FileInputStream, FileOutputStream}
import java.nio.channels.FileChannel
class WarnAndErrorLogger(val logFile: File) extends BasicLogger {
val tempLogFile = {
val temp = File.createTempFile("warn-and-error", null)
temp.deleteOnExit()
temp
}
private def openLogWriter(): PrintWriter = new PrintWriter(tempLogFile)
private def transferLog: Unit = {
logFile.createNewFile
var src: FileChannel = null
var des: FileChannel = null
try {
src = new FileInputStream(tempLogFile).getChannel
des = new FileOutputStream(logFile).getChannel
des.transferFrom(src, 0, src.size)
} finally {
if (src != null) {
src.close
}
if (des != null) {
des.close
}
}
}
private def reset(): Unit = {
if (ef != null) {
ef.close()
}
transferLog
ef = openLogWriter()
}
private var ef = openLogWriter()
private var compiling = false
def log(level: Level.Value, message: => String): Unit = level match {
case Level.Warn if compiling =>
ef.print("[warn] ")
ef.println(message)
case Level.Error if compiling =>
ef.print("[error] ")
ef.println(message)
case Level.Info if message.trim.startsWith("Total time:") =>
reset()
case _ =>
}
def control(event: ControlEvent.Value, message: => String): Unit = event match {
case ControlEvent.Header if message.trim == "== compile ==" => compiling = true
case ControlEvent.Header if message.trim == "== test-compile ==" => compiling = true
case ControlEvent.Finish => compiling = false
case ControlEvent.Start =>
}
def success(message: => String): Unit = ()
def logAll(events: Seq[sbt.LogEvent]): Unit = ()
def trace(t: => Throwable): Unit = ()
}
@virasak
Copy link
Author

virasak commented Jul 9, 2010

Usage

Mix this CompileMessageLogger into your sbt project:

class TestProject(info: ProjectInfo) extends DefaultProject(info) with CompileMessageLogger {
    ....
}

Now, when ever you compile the project, the logger will create an error file (target/error) that can be
used in Vim.

You can get the scala compiler plugin for Vim at http://gist.github.com/374023#file_sbt.vim

If you need to use this fragment of code in many projects, you can create a plugin project and the source of the project contained only this code. Learn more about sbt plugin at http://code.google.com/p/simple-build-tool/wiki/SbtPlugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment