title: How can i return a formatted string from a rust macro
body: <p>I am trying to achieve something like this:</p>
  <ol>
  <li>Construct an <code>error</code> message with some captured variables;</li>
  <li>Log this <code>error</code> message;</li>
  <li>Save and return this <code>error</code> message to user eventually;</li>
  </ol>
  <p>Apparently this can be done in a few lines of code, but it is a bit tedious since i need this pattern heavily everywhere in my project, i mean like below:</p>
  <pre class="lang-rust prettyprint-override"><code>let var_a = 233;
  let err = format!(&quot;some custom error message, var_a: {}&quot;, var_a);
  
  // Here i do the logging with the `tokio-rs/tracing` macros.
  error!(err = %err, &quot;some custom error message&quot;);
  
  // The s_errs will be eventually showed to end users.
  s_errs.append(err);
  </code></pre>
  <p>I am trying to achieve this with a macro which wraps the <code>tokio-rs/tracing</code> macros but return the constructed <code>err</code> message:</p>
  <pre class="lang-rust prettyprint-override"><code>#[macro_export]
  macro_rules! log_usr_err {
      ($($arg:tt)+) =&gt; (
        error!($($arg)+)
        format!($($arg)+)
    );
  }
  </code></pre>
  <p>so i can just use this macro in my project to simplify the coding a bit:</p>
  <pre class="lang-rust prettyprint-override"><code>let err = log_usr_err!(&quot;some custom error message, {}&quot;, var_a = var_a);
  
  // The s_errs will be eventually showed to end users.
  s_errs.append(err);
  </code></pre>
  <p>but stuck at the compile error:</p>
  <pre><code>error: macro expansion ignores token `format` and any following
  </code></pre>
  <p>How should i return the formatted string from my <code>log_urs_err!</code> macro?</p>
  <p>Coz in the future in want to include the accurate <code>file!</code> info and the <code>line!</code> info also in my logs, thus i think via macro is the right direction to go, or there is some better approach?</p>