Skip to content

Commit 103b052

Browse files
authored
Merge pull request #937 from epage/log
feat: Add logger support
2 parents abbdb6c + d7a993c commit 103b052

File tree

6 files changed

+120
-15
lines changed

6 files changed

+120
-15
lines changed

Cargo.lock

Lines changed: 79 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ toml_edit = "0.22.22"
7878
indexmap = "2"
7979
url = "2.5.4"
8080
pathdiff = "0.2"
81+
env_logger = "0.11.8"
82+
clap-verbosity-flag = "3.0.2"
83+
log = "0.4.27"
8184

8285
[dependencies.semver]
8386
features = ["serde"]

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ Upgrade dependency version requirements in Cargo.toml manifest files
7474
Usage: cargo upgrade [OPTIONS]
7575

7676
Options:
77-
--dry-run Print changes to be made without making them
77+
-n, --dry-run Print changes to be made without making them
7878
--manifest-path <PATH> Path to the manifest to upgrade
7979
--rust-version <VER> Override `rust-version`
8080
--ignore-rust-version Ignore `rust-version` specification in packages
8181
--locked Require `Cargo.toml` to be up to date
82-
-v, --verbose... Use verbose output
82+
-v, --verbose... Increase logging verbosity
83+
-q, --quiet... Decrease logging verbosity
8384
-Z <FLAG> Unstable (nightly-only) flags
8485
-h, --help Print help
8586
-V, --version Print version
@@ -132,10 +133,12 @@ Options:
132133
-p, --package <PKGID> Package id of the crate to change the version of
133134
--all [deprecated in favor of `--workspace`]
134135
--workspace Modify all packages in the workspace
135-
--dry-run Print changes to be made without making them
136+
-n, --dry-run Print changes to be made without making them
136137
--exclude <EXCLUDE> Crates to exclude and not modify
137138
--offline Run without accessing the network
138139
--locked Require `Cargo.toml` to be up to date
140+
-v, --verbose... Increase logging verbosity
141+
-q, --quiet... Decrease logging verbosity
139142
-Z <FLAG> Unstable (nightly-only) flags
140143
-h, --help Print help
141144
-V, --version Print version

src/bin/set-version/set_version.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct VersionArgs {
5353
workspace: bool,
5454

5555
/// Print changes to be made without making them.
56-
#[arg(long)]
56+
#[arg(long, short = 'n')]
5757
dry_run: bool,
5858

5959
/// Crates to exclude and not modify.
@@ -68,6 +68,9 @@ pub struct VersionArgs {
6868
#[arg(long)]
6969
locked: bool,
7070

71+
#[command(flatten)]
72+
verbose: clap_verbosity_flag::Verbosity,
73+
7174
/// Unstable (nightly-only) flags
7275
#[arg(short = 'Z', value_name = "FLAG", global = true, value_enum)]
7376
unstable_features: Vec<UnstableOptions>,
@@ -85,6 +88,10 @@ enum UnstableOptions {}
8588
/// Main processing function. Allows us to return a `Result` so that `main` can print pretty error
8689
/// messages.
8790
fn exec(args: VersionArgs) -> CargoResult<()> {
91+
env_logger::Builder::from_env("CARGO_LOG")
92+
.filter_level(args.verbose.log_level_filter())
93+
.init();
94+
8895
let VersionArgs {
8996
target,
9097
bump,
@@ -97,6 +104,7 @@ fn exec(args: VersionArgs) -> CargoResult<()> {
97104
exclude,
98105
locked,
99106
offline,
107+
verbose: _,
100108
unstable_features: _,
101109
} = args;
102110

src/bin/upgrade/upgrade.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use termcolor::{Color, ColorSpec};
1919
#[command(version)]
2020
pub struct UpgradeArgs {
2121
/// Print changes to be made without making them.
22-
#[arg(long)]
22+
#[arg(long, short = 'n')]
2323
dry_run: bool,
2424

2525
/// Path to the manifest to upgrade
@@ -38,9 +38,8 @@ pub struct UpgradeArgs {
3838
#[arg(long)]
3939
locked: bool,
4040

41-
/// Use verbose output
42-
#[arg(short, long, action = clap::ArgAction::Count)]
43-
verbose: u8,
41+
#[command(flatten)]
42+
verbose: clap_verbosity_flag::Verbosity,
4443

4544
/// Unstable (nightly-only) flags
4645
#[arg(short = 'Z', value_name = "FLAG", global = true, value_enum)]
@@ -117,8 +116,19 @@ impl UpgradeArgs {
117116
exec(self)
118117
}
119118

119+
fn verbose_num(&self) -> i8 {
120+
match self.verbose.filter() {
121+
clap_verbosity_flag::VerbosityFilter::Off => -1,
122+
clap_verbosity_flag::VerbosityFilter::Error => 0,
123+
clap_verbosity_flag::VerbosityFilter::Warn => 1,
124+
clap_verbosity_flag::VerbosityFilter::Info => 2,
125+
clap_verbosity_flag::VerbosityFilter::Debug => 3,
126+
clap_verbosity_flag::VerbosityFilter::Trace => 4,
127+
}
128+
}
129+
120130
fn is_verbose(&self) -> bool {
121-
0 < self.verbose
131+
0 < self.verbose_num()
122132
}
123133

124134
fn verbose<F>(&self, mut callback: F) -> CargoResult<()>
@@ -156,6 +166,10 @@ enum UnstableOptions {}
156166
/// Main processing function. Allows us to return a `Result` so that `main` can print pretty error
157167
/// messages.
158168
fn exec(args: UpgradeArgs) -> CargoResult<()> {
169+
env_logger::Builder::from_env("CARGO_LOG")
170+
.filter_level(args.verbose.log_level_filter())
171+
.init();
172+
159173
let offline = false;
160174
let mut index = IndexCache::new(CertsSource::Native);
161175

@@ -437,7 +451,7 @@ fn exec(args: UpgradeArgs) -> CargoResult<()> {
437451
if !table.is_empty() {
438452
let (interesting, uninteresting) = table
439453
.into_iter()
440-
.partition::<Vec<_>, _>(|d| d.show_for(args.verbose));
454+
.partition::<Vec<_>, _>(|d| d.show_for(args.verbose_num()));
441455
print_upgrade(interesting)?;
442456
uninteresting_crates.extend(uninteresting);
443457
}
@@ -796,7 +810,7 @@ impl Dep {
796810
spec
797811
}
798812

799-
fn show_for(&self, verbosity: u8) -> bool {
813+
fn show_for(&self, verbosity: i8) -> bool {
800814
if 2 <= verbosity {
801815
return true;
802816
}

src/index.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ struct RemoteIndex {
186186

187187
impl RemoteIndex {
188188
fn open(url: &Url, certs_source: CertsSource) -> CargoResult<Self> {
189+
log::trace!("opening index entry for {url}");
189190
let url = url.to_string();
190191
let url = tame_index::IndexUrl::NonCratesIo(std::borrow::Cow::Owned(url));
191192
let index = tame_index::SparseIndex::new(tame_index::IndexLocation::new(url))?;
@@ -212,6 +213,7 @@ impl RemoteIndex {
212213
}
213214

214215
fn krate(&mut self, name: &str) -> CargoResult<Option<IndexKrate>> {
216+
log::trace!("krate {name}");
215217
let etag = self
216218
.etags
217219
.iter()

0 commit comments

Comments
 (0)