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

Expose logging levels #459

Merged
merged 4 commits into from
Nov 15, 2021
Merged

Expose logging levels #459

merged 4 commits into from
Nov 15, 2021

Conversation

WaDelma
Copy link
Contributor

@WaDelma WaDelma commented Aug 27, 2021

I am using FromStr implementation of LevelFilter to parse logging level given by user via command line flag. In case user gives invalid logging level I want to print list of allowed values. Currently I have to replicate LOG_LEVEL_NAMES array in my code and remember to keep it in sync (It is probably unlikely it changes that much though).

One question I have is that is there a reason it is static instead of const?

@KodrAus
Copy link
Contributor

KodrAus commented Oct 26, 2021

Hey @WaDelma 👋

There probably isn't a great reason for this to be static rather than const. Rather than make the array public though I think a static method on Level that returns some impl Iterator<Item = Level> might be better:

pub fn iter_all() -> impl Iterator<Item = Level> {
    ..
}

That way in your code you could call something like:

let names = Level::iter_all().map(|l| l.as_str()).collect();

The reason I think we should avoid making the array public is that it would be a breaking change to add any more levels (as unlikely as that is) because the type would change from [&'static str; 6] to [&'static str; 7].

@WaDelma
Copy link
Contributor Author

WaDelma commented Oct 27, 2021

Makes sense. I added such static methods for both Level and LevelFilter,

@@ -560,6 +560,11 @@ impl Level {
pub fn as_str(&self) -> &'static str {
LOG_LEVEL_NAMES[*self as usize]
}

/// Iterate through all supported logging levels
pub fn iter() -> impl Iterator<Item = Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth documenting something about the order of these fields. Using from_usize we’re effectively returning them in priority order.

What do you think?

@WaDelma WaDelma changed the title Make log level names array public Expose logging levels Nov 5, 2021
Copy link
Contributor

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @WaDelma!

@KodrAus KodrAus merged commit 2d3ecdf into rust-lang:master Nov 15, 2021
@KodrAus KodrAus mentioned this pull request Nov 15, 2021
EFanZh pushed a commit to EFanZh/log that referenced this pull request Jul 23, 2023
* Refactor: Avoid parsing `package.version()` twice in `ops::resolve` and `ops::install`
* Optimize Resolution: Replace `Package<Meta>` with two `CompactStrings`: `name` and `version`
* Use `CompactString` for `BinstallError::CratesIoApi::crate_name`
* Use `CompactString` for `BinstallError::VersionParse::v`
* Use `CompactString` for `BinstallError::VersionReq::req`
* Use `CompactString` for `BinstallError::VersionUnavailable::crate_name`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants