Does Scala have “macro” definitions ready to use - for example, LINE, FILE?

When you get a stack trace from an exception, you get the files and line numbers. I need something similar for my reports, so that I could quickly solve this problem.

I look, in particular, the LINE and FILE macros. Is there anything similar in Scala?

+5
source share
3 answers

There is no such macro in either Scala or Java. Of course, information about the number number is stored in bytecode (also for debugging purposes), but there is no API to get it.

Stack traces with class names and line numbers are generated through native Throwable.fillInStackTrace(). Logging libraries can also use Thread.getStackTrace().

. , , .

+6

sourcecode .

( ):

object Main extends App {
  def log(message: String)(implicit line: sourcecode.Line, file: sourcecode.File) = 
    println(s"${file.value}:${line.value} $message")

  log("foo")
}

:

//jhoffmann/Development/sourcecode/src/main/scala/Main.scala:5 foo

implicits .

+2

This is a project for providing macros in Scala .

Perhaps you could go to the project team to discuss what you need.

+1
source

All Articles