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

Support shorthand for #[emit::as_debug] #168

Closed
KodrAus opened this issue Oct 29, 2024 · 3 comments · Fixed by #185
Closed

Support shorthand for #[emit::as_debug] #168

KodrAus opened this issue Oct 29, 2024 · 3 comments · Fixed by #185

Comments

@KodrAus
Copy link
Contributor

KodrAus commented Oct 29, 2024

The case of logging values with their debug representation while building or testing code is common and important enough that we may want a shorthand for it. Instead of having to write:

emit::debug!("received {#[emit::as_debug] message} from queue");

you could instead write:

emit::debug!("received {message:?} from queue");

The ident:? pattern would expand to #[emit::as_debug] ident. You wouldn't be able to combine it with other attributes or format flags, and other attributes wouldn't have a shorthand. We may also restrict it to the debug! macro. The goal is just to make it easier to pepper temporary debug-style logging in code you're actively working on. Long-term logging should prefer serialized or display-formatted values over the debug format.

@KodrAus
Copy link
Contributor Author

KodrAus commented Dec 15, 2024

An alternative design here is another macro: emit::dbg!(). It could accept either an expr, like the standard dbg!(), or a regular template. The difference to emit::debug!() would be:

  1. A plain expr captures using fmt::Debug in a property called value.
  2. A regular template also captures any properties using fmt::Debug. The template form would be preferred over the expr form (string literals would be ambiguous).
  3. The panic::location is also captured.

This would make emit::dbg!() largely compatible with the standard dbg!() as a quick-and-dirty way to pepper temporary debugging aids through a program. These can then be easily found later with a search for 'dbg' to remove them or replace them with longer-lived diagnostics.

@nblumhardt
Copy link
Contributor

Just to add another perspective, for me the main advantage of :? is that it's consistent with how format! is used; in the back of my mind I keep expecting it to work, and needing to flip over to #[emit::as_debug] pulls me out of the familiar/predictable Rust-y API and into emit's specifics. I'd probably not gain any benefit from the alternative dbg! solution.

@KodrAus
Copy link
Contributor Author

KodrAus commented Dec 17, 2024

Yeh, the balance I'm trying to strike is that debug is convenient for quick debugging, especially when working with a lot of internal types that don't implement display, but I think is non-ideal for long-term logging. In that case, you're better off with a deliberate and stable format from display, or a structured format from serde. I was hoping a different macro for quick-and-dirty debugging would put you in a different mindset, so your long-lived events would be higher quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants