Skip to content

Expose logging levels #459

Merged
KodrAus merged 4 commits into
rust-lang:masterfrom
WaDelma:master
Nov 15, 2021
Merged

Expose logging levels #459
KodrAus merged 4 commits into
rust-lang:masterfrom
WaDelma:master

Conversation

@WaDelma
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

WaDelma commented Oct 27, 2021

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

Comment thread src/lib.rs
}

/// Iterate through all supported logging levels
pub fn iter() -> impl Iterator<Item = Self> {
Copy link
Copy Markdown
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
Copy Markdown
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.

2 participants