title: Scala get the line and file of a functions invocation at compile-time
body: <p>What I want to have is a function that is able to print the lines + file from where it was called.</p>
  
  <p>The behavior would be something like this:</p>
  
  <pre><code>log("x") //Called at line 15 of file Blax.scala
  </code></pre>
  
  <p>Should print</p>
  
  <pre><code>Blax.scala, line 15, message: x
  </code></pre>
  
  <p>Something analogous to the following code for c++:</p>
  
  <pre><code>    void _log(const char* message, const char* file, int line) { std::cout &lt;&lt; file &lt;&lt; ", " &lt;&lt; line &lt;&lt; ", message: " &lt;&lt;
  message
  &lt;&lt; "\n";}
      #define log(message) _log(message, __FILE__, __LINE__)
  </code></pre>
  
  <p>Is this possible to do in Scala? Is it possible to do it in a way that is half-readable and somewhat compatible with future and older version of the compiler?</p>
  
  <p>I've been trying to implement something similar based on the macro section here: <a href="https://docs.scala-lang.org/overviews/macros/overview.html" rel="nofollow noreferrer">https://docs.scala-lang.org/overviews/macros/overview.html</a></p>
  
  <p>But the code is extremely hard to read and the articles don't seem to cover anything that closely related to what I need.</p>
  
  <p>I guess my question could be split into two or three:</p>
  
  <p>a) Is it possible to do any type of "traditional" code inlining in Scala by calling a macro just like a function? </p>
  
  <p>So basically calling <code>blax(a)</code> which expands into <code>impl_blax(a, "b", __C__)</code> before compilation.</p>
  
  <p>b) What is the "recommended" way of getting the current line number and file name at compile time in Scala?</p>
  
  <p>c) If either a or b is impossible, is there still away to accomplish what I want?</p>
  
  <p><strong>Clarification:</strong></p>
  
  <p>Yes, this is easily doable using a Stack trace, but throwing an exception and getting the Stack trace is unbearably slow and not suitable for anything but certain niche debug cases. I'm looking for a no-overhead or low overhead solution, hence why I asked for one using macros</p>
  
  <p><strong>Edits (In order to answer flags):</strong></p>
  
  <p>@Seth Tisue on the marking of the question as duplicate</p>
  
  <p>If the question you marked this as a duplicate of solves my problem I'd love to hear how. Some of the links in the original questions lead to 404, the code of the highest voted answer (which is not marked correct) doesn't seem to do anything related to what I want and parts of it are marked as deprecated in scala 2.12... I did stumble upon the question but I hoped that being more explicit about the problem may yield an answer which that previously linked 4 years old question doesn't have.</p>