I am trying to achieve something like this:
- Construct an
errormessage with some captured variables; - Log this
errormessage; - Save and return this
errormessage to user eventually;
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:
let var_a = 233; let err = format!("some custom error message, var_a: {}", var_a); // Here i do the logging with the `tokio-rs/tracing` macros. error!(err = %err, "some custom error message"); // The s_errs will be eventually showed to end users. s_errs.append(err); I am trying to achieve this with a macro which wraps the tokio-rs/tracing macros but return the constructed err message:
#[macro_export] macro_rules! log_usr_err { ($($arg:tt)+) => ( error!($($arg)+) format!($($arg)+) ); } so i can just use this macro in my project to simplify the coding a bit:
let err = log_usr_err!("some custom error message, {}", var_a = var_a); // The s_errs will be eventually showed to end users. s_errs.append(err); but stuck at the compile error:
error: macro expansion ignores token `format` and any following How should i return the formatted string from my log_urs_err! macro?
Coz in the future in want to include the accurate file! info and the line! info also in my logs, thus i think via macro is the right direction to go, or there is some better approach?
error!($($arg)+)macro invocation?;coz in future it will be an error: github.com/rust-lang/rust/issues/79813 (actually it was the reason i removed the;)