Skip to content

Commit

Permalink
refactor(logs): improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Jun 15, 2021
1 parent e8a3c35 commit 5333453
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use structopt::StructOpt;
rename_all_env = "screaming-snake"
)]
pub struct Opt {
/// Activates the debug mode
#[structopt(short, long)]
pub debug: bool,
/// Activates the verbose mode
#[structopt(short, long, parse(from_occurrences), alias = "debug")]
pub verbose: u8,
/// Sets the configuration file.
#[structopt(
short,
Expand Down
19 changes: 9 additions & 10 deletions git-cliff/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'a> Changelog<'a> {
/// Processes the commits and omits the ones that doesn't match the
/// criteria set by configuration file.
fn process_commits(&mut self) {
debug!("Processing the commits...");
let config = &self.config;
self.releases.iter_mut().for_each(|release| {
release.commits = release
Expand All @@ -52,7 +53,7 @@ impl<'a> Changelog<'a> {
) {
Ok(commit) => Some(commit),
Err(e) => {
debug!("Cannot process commit: {} ({})", commit.id, e);
trace!("{} ({})", commit.id[..7].to_string(), e);
None
}
}
Expand All @@ -63,6 +64,7 @@ impl<'a> Changelog<'a> {

/// Processes the releases and filters them out based on the configuration.
fn process_releases(&mut self) {
debug!("Processing the releases...");
let skip_regex = self.config.git.skip_tags.as_ref();
self.releases = self
.releases
Expand All @@ -71,21 +73,16 @@ impl<'a> Changelog<'a> {
.rev()
.filter(|release| {
if release.commits.is_empty() {
debug!(
"Release {} doesn't have any commits",
release
.version
.as_ref()
.cloned()
.unwrap_or_else(|| String::from("[?]"))
);
if let Some(version) = release.version.as_ref().cloned() {
trace!("Release doesn't have any commits: {}", version);
}
false
} else if let Some(version) = &release.version {
!skip_regex
.map(|r| {
let skip_tag = r.is_match(version);
if skip_tag {
debug!("Skipping release: {}", version)
trace!("Skipping release: {}", version)
}
skip_tag
})
Expand All @@ -99,6 +96,7 @@ impl<'a> Changelog<'a> {

/// Generates the changelog and writes it to the given output.
pub fn generate<W: Write>(&self, out: &mut W) -> Result<()> {
debug!("Generating changelog...");
if let Some(header) = &self.config.changelog.header {
write!(out, "{}", header)?;
}
Expand All @@ -117,6 +115,7 @@ impl<'a> Changelog<'a> {
mut changelog: String,
out: &mut W,
) -> Result<()> {
debug!("Generating changelog and prepending...");
if let Some(header) = &self.config.changelog.header {
changelog = changelog.replacen(header, "", 1);
}
Expand Down
2 changes: 1 addition & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn run(mut args: Opt) -> Result<()> {
if let Some(commit_id) = commits.first().map(|c| c.id().to_string()) {
match tags.get(&commit_id) {
Some(tag) => {
debug!("There is already a tag ({}) for {}", tag, commit_id)
warn!("There is already a tag ({}) for {}", tag, commit_id)
}
None => {
tags.insert(commit_id, tag);
Expand Down
4 changes: 3 additions & 1 deletion git-cliff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use structopt::StructOpt;

fn main() {
let args = Opt::from_args();
if args.debug {
if args.verbose == 1 {
env::set_var("RUST_LOG", "debug");
} else if args.verbose > 1 {
env::set_var("RUST_LOG", "trace");
} else if env::var_os("RUST_LOG").is_none() {
env::set_var("RUST_LOG", "info");
}
Expand Down

0 comments on commit 5333453

Please sign in to comment.