Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __FILE__ and __LINE__ meta-constants #7458

Closed
scabug opened this issue May 6, 2013 · 3 comments
Closed

Add __FILE__ and __LINE__ meta-constants #7458

scabug opened this issue May 6, 2013 · 3 comments

Comments

@scabug
Copy link

@scabug scabug commented May 6, 2013

Add FILE and LINE meta-constants (or some variant) for use in logging and debugging.

@scabug
Copy link
Author

@scabug scabug commented May 6, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7458?orig=1
Reporter: G. Ralph Kuntz, MD (grkuntzmd)
Affected Versions: 2.10.1

@scabug
Copy link
Author

@scabug scabug commented May 6, 2013

@retronym said:
You can implement this without compiler support in 2.10.0 with a macro.

scala> import language.experimental.macros, reflect.macros.Context
import language.experimental.macros
import reflect.macros.Context

scala> def __FILE__impl(c: Context) = c.literal(c.macroApplication.pos.source.toString)
__FILE__impl: (c: scala.reflect.macros.Context)c.Expr[String]

scala> def __FILE__ = macro __FILE__impl
__FILE__: String

scala> __FILE__
res1: String = <console>

scala> def __LINE__impl(c: Context) = c.literal(c.macroApplication.pos.line)
__LINE__impl: (c: scala.reflect.macros.Context)c.Expr[Int]

scala> def __LINE__ = macro __LINE__impl
__LINE__: Int

scala> __LINE__
res2: Int = 17

There has been a proposal to standardize an extension of this, Source Locations, which would allow you to implicitly pass the source location from a call site to some method.

See: http://docs.scala-lang.org/sips/pending/source-locations.html

That effort was put on hold as it was shows that even this can now be done as a macro. But we haven't moved that out of a prototype.

@scabug scabug closed this May 6, 2013
@scabug
Copy link
Author

@scabug scabug commented May 6, 2013

@retronym said:
Also, in case you're not aware, LogBack can extract this information at runtime from JVM stack traces: http://logback.qos.ch/manual/layouts.html.

http://logback.qos.ch/manual/layouts.html#caller
http://logback.qos.ch/manual/layouts.html#line

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

Successfully merging a pull request may close this issue.

None yet
2 participants