mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
trace: Add shorthand syntax for local fields (#1103)
## Motivation
A common pattern in `tokio-trace` is to use the value of a local
variable as a field on a span or event. Currently, this requires code
like:
```rust
info!(foo = foo);
```
which is not particularly ergonomic given how commonly this occurs.
Struct initializers support a shorthand syntax for fields where the name
of the field is the same as a local variable, and `tokio-trace` should
as well.
## Solution
This branch adds support for syntax like
```rust
let foo = ...;
info!(foo);
```
and
```rust
let foo = Foo {
bar: ...,
...
};
info!(foo.bar)
```
to the `tokio-trace` span and event macros. This syntax also works with
the `Debug` and `Display` field shorthand.
The span macros previously used a field name with no value to indicate
an uninitialized field. A new issue, #1138, has been opened for finding a
replacement syntax for uninitialized fields. Until then, the `tokio-trace`
macros will no longer provide a way to create fields without values,
although the `-core` API will continue to support this.
Closes #1062
Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
@@ -42,7 +42,7 @@ fn test_always_log() {
|
||||
info!(message = "hello world;", thingy = 42, other_thingy = 666);
|
||||
last(&a, "hello world; thingy=42 other_thingy=666");
|
||||
|
||||
let mut foo = span!(Level::TRACE, "foo");
|
||||
let foo = span!(Level::TRACE, "foo");
|
||||
last(&a, "foo;");
|
||||
foo.in_scope(|| {
|
||||
last(&a, "-> foo");
|
||||
@@ -55,11 +55,13 @@ fn test_always_log() {
|
||||
span!(Level::TRACE, "foo", bar = 3, baz = false);
|
||||
last(&a, "foo; bar=3 baz=false");
|
||||
|
||||
let mut span = span!(Level::TRACE, "foo", bar, baz);
|
||||
span.record("bar", &3);
|
||||
last(&a, "foo; bar=3");
|
||||
span.record("baz", &"a string");
|
||||
last(&a, "foo; baz=\"a string\"");
|
||||
// TODO(#1138): determine a new syntax for uninitialized span fields, and
|
||||
// re-enable these.
|
||||
// let span = span!(Level::TRACE, "foo", bar = _, baz = _);
|
||||
// span.record("bar", &3);
|
||||
// last(&a, "foo; bar=3");
|
||||
// span.record("baz", &"a string");
|
||||
// last(&a, "foo; baz=\"a string\"");
|
||||
}
|
||||
|
||||
fn last(state: &State, expected: &str) {
|
||||
|
||||
Reference in New Issue
Block a user