core: give SetGlobalDefaultError a useful Debug impl#2250
Merged
Conversation
## Motivation
When a global default dispatcher has already been set, the
`dispatch::set_global_default` function fails with a
`SetGlobalDefaultError`. The `fmt::Display` impl for this type includes
a message explaining that the error indicates that a global default has
already been set, but the `fmt::Debug` impl is derived, and just looks
like this:
```
SetGlobalDefaultError { _no_construct: () }
```
which isn't particularly helpful.
Unfortunately, when `unwrap()`ping or `expect()`ing a `Result`, the
`fmt::Debug` implementation for the error type is used, rather than
`fmt::Display`. This means that the error message will not explain to
the user why setting the global default dispatcher failed, which is a
bummer.
## Solution
This branch replaces the derived `Debug` impl with a manually
implemented one that prints the same message as the `Display` impl, but
formatted like a tuple struct with a single string field. This avoids
emitting `Debug` output that's *just* a textual human-readable message,
rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
```rust
tracing::dispatch::set_global_default(foo).unwrap();
```
The mesasge now looks like:
```
SetGlobalDefaultError("a global default trace dispatcher has already been set")
```
which should be a bit easier to debug.
hawkw
added a commit
that referenced
this pull request
Jul 29, 2022
## Motivation
When a global default dispatcher has already been set, the
`dispatch::set_global_default` function fails with a
`SetGlobalDefaultError`. The `fmt::Display` impl for this type includes
a message explaining that the error indicates that a global default has
already been set, but the `fmt::Debug` impl is derived, and just looks
like this:
```
SetGlobalDefaultError { _no_construct: () }
```
which isn't particularly helpful.
Unfortunately, when `unwrap()`ping or `expect()`ing a `Result`, the
`fmt::Debug` implementation for the error type is used, rather than
`fmt::Display`. This means that the error message will not explain to
the user why setting the global default dispatcher failed, which is a
bummer.
## Solution
This branch replaces the derived `Debug` impl with a manually
implemented one that prints the same message as the `Display` impl, but
formatted like a tuple struct with a single string field. This avoids
emitting `Debug` output that's *just* a textual human-readable message,
rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
```rust
tracing::dispatch::set_global_default(foo).unwrap();
```
The mesasge now looks like:
```
SetGlobalDefaultError("a global default trace dispatcher has already been set")
```
which should be a bit easier to debug.
hawkw
added a commit
that referenced
this pull request
Jul 29, 2022
## Motivation
When a global default dispatcher has already been set, the
`dispatch::set_global_default` function fails with a
`SetGlobalDefaultError`. The `fmt::Display` impl for this type includes
a message explaining that the error indicates that a global default has
already been set, but the `fmt::Debug` impl is derived, and just looks
like this:
```
SetGlobalDefaultError { _no_construct: () }
```
which isn't particularly helpful.
Unfortunately, when `unwrap()`ping or `expect()`ing a `Result`, the
`fmt::Debug` implementation for the error type is used, rather than
`fmt::Display`. This means that the error message will not explain to
the user why setting the global default dispatcher failed, which is a
bummer.
## Solution
This branch replaces the derived `Debug` impl with a manually
implemented one that prints the same message as the `Display` impl, but
formatted like a tuple struct with a single string field. This avoids
emitting `Debug` output that's *just* a textual human-readable message,
rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
```rust
tracing::dispatch::set_global_default(foo).unwrap();
```
The mesasge now looks like:
```
SetGlobalDefaultError("a global default trace dispatcher has already been set")
```
which should be a bit easier to debug.
hawkw
added a commit
that referenced
this pull request
Jul 29, 2022
## Motivation
When a global default dispatcher has already been set, the
`dispatch::set_global_default` function fails with a
`SetGlobalDefaultError`. The `fmt::Display` impl for this type includes
a message explaining that the error indicates that a global default has
already been set, but the `fmt::Debug` impl is derived, and just looks
like this:
```
SetGlobalDefaultError { _no_construct: () }
```
which isn't particularly helpful.
Unfortunately, when `unwrap()`ping or `expect()`ing a `Result`, the
`fmt::Debug` implementation for the error type is used, rather than
`fmt::Display`. This means that the error message will not explain to
the user why setting the global default dispatcher failed, which is a
bummer.
## Solution
This branch replaces the derived `Debug` impl with a manually
implemented one that prints the same message as the `Display` impl, but
formatted like a tuple struct with a single string field. This avoids
emitting `Debug` output that's *just* a textual human-readable message,
rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
```rust
tracing::dispatch::set_global_default(foo).unwrap();
```
The mesasge now looks like:
```
SetGlobalDefaultError("a global default trace dispatcher has already been set")
```
which should be a bit easier to debug.
hawkw
added a commit
that referenced
this pull request
Jul 29, 2022
# 0.1.29 (July 29, 2022) This release of `tracing-core` adds `PartialEq` and `Eq` implementations for metadata types, and improves error messages when setting the global default subscriber fails. ### Added - `PartialEq` and `Eq` implementations for `Metadata` ([#2229]) - `PartialEq` and `Eq` implementations for `FieldSet` ([#2229]) ### Fixed - Fixed unhelpful `fmt::Debug` output for `dispatcher::SetGlobalDefaultError` ([#2250]) - Fixed compilation with `-Z minimal-versions` ([#2246]) Thanks to @jswrenn and @CAD97 for contributing to this release! [#2229]: #2229 [#2246]: #2246 [#2250]: #2250
hawkw
added a commit
that referenced
this pull request
Jul 29, 2022
# 0.1.29 (July 29, 2022) This release of `tracing-core` adds `PartialEq` and `Eq` implementations for metadata types, and improves error messages when setting the global default subscriber fails. ### Added - `PartialEq` and `Eq` implementations for `Metadata` ([#2229]) - `PartialEq` and `Eq` implementations for `FieldSet` ([#2229]) ### Fixed - Fixed unhelpful `fmt::Debug` output for `dispatcher::SetGlobalDefaultError` ([#2250]) - Fixed compilation with `-Z minimal-versions` ([#2246]) Thanks to @jswrenn and @CAD97 for contributing to this release! [#2229]: #2229 [#2246]: #2246 [#2250]: #2250
kaffarell
pushed a commit
to kaffarell/tracing
that referenced
this pull request
May 22, 2024
## Motivation
When a global default dispatcher has already been set, the
`dispatch::set_global_default` function fails with a
`SetGlobalDefaultError`. The `fmt::Display` impl for this type includes
a message explaining that the error indicates that a global default has
already been set, but the `fmt::Debug` impl is derived, and just looks
like this:
```
SetGlobalDefaultError { _no_construct: () }
```
which isn't particularly helpful.
Unfortunately, when `unwrap()`ping or `expect()`ing a `Result`, the
`fmt::Debug` implementation for the error type is used, rather than
`fmt::Display`. This means that the error message will not explain to
the user why setting the global default dispatcher failed, which is a
bummer.
## Solution
This branch replaces the derived `Debug` impl with a manually
implemented one that prints the same message as the `Display` impl, but
formatted like a tuple struct with a single string field. This avoids
emitting `Debug` output that's *just* a textual human-readable message,
rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
```rust
tracing::dispatch::set_global_default(foo).unwrap();
```
The mesasge now looks like:
```
SetGlobalDefaultError("a global default trace dispatcher has already been set")
```
which should be a bit easier to debug.
kaffarell
pushed a commit
to kaffarell/tracing
that referenced
this pull request
May 22, 2024
# 0.1.29 (July 29, 2022) This release of `tracing-core` adds `PartialEq` and `Eq` implementations for metadata types, and improves error messages when setting the global default subscriber fails. ### Added - `PartialEq` and `Eq` implementations for `Metadata` ([tokio-rs#2229]) - `PartialEq` and `Eq` implementations for `FieldSet` ([tokio-rs#2229]) ### Fixed - Fixed unhelpful `fmt::Debug` output for `dispatcher::SetGlobalDefaultError` ([tokio-rs#2250]) - Fixed compilation with `-Z minimal-versions` ([tokio-rs#2246]) Thanks to @jswrenn and @CAD97 for contributing to this release! [tokio-rs#2229]: tokio-rs#2229 [tokio-rs#2246]: tokio-rs#2246 [tokio-rs#2250]: tokio-rs#2250
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a global default dispatcher has already been set, the
dispatch::set_global_defaultfunction fails with aSetGlobalDefaultError. Thefmt::Displayimpl for this type includesa message explaining that the error indicates that a global default has
already been set, but the
fmt::Debugimpl is derived, and just lookslike this:
which isn't particularly helpful.
Unfortunately, when
unwrap()ping orexpect()ing aResult, thefmt::Debugimplementation for the error type is used, rather thanfmt::Display. This means that the error message will not explain tothe user why setting the global default dispatcher failed, which is a
bummer.
Solution
This branch replaces the derived
Debugimpl with a manuallyimplemented one that prints the same message as the
Displayimpl, butformatted like a tuple struct with a single string field. This avoids
emitting
Debugoutput that's just a textual human-readable message,rather than looking like Rust code, but ensures the message is visible
to the user when writing code like
The mesasge now looks like:
which should be a bit easier to debug.