Skip to content

Commit

Permalink
Switch to simplelog
Browse files Browse the repository at this point in the history
`env_logger` does not handle writing to file for the moment, see rust-cli/env_logger#208
  • Loading branch information
kraktus committed Oct 23, 2022
1 parent 00ba11a commit eaaa0de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lintcheck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ publish = false
cargo_metadata = "0.14"
clap = "3.2"
crossbeam-channel = "0.5.6"
env_logger = "0.9"
simplelog = "0.12.0"
flate2 = "1.0"
log = "0.4"
rayon = "1.5.1"
Expand Down
39 changes: 23 additions & 16 deletions lintcheck/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::{Arg, ArgAction, ArgMatches, Command};
use env_logger::Builder;
use log::LevelFilter;
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};
use std::env;
use std::fs::{self, File};
use std::path::PathBuf;

fn get_clap_config() -> ArgMatches {
Expand Down Expand Up @@ -43,8 +44,9 @@ fn get_clap_config() -> ArgMatches {
.conflicts_with("fix"),
Arg::new("verbose")
.short('v')
.long("--verbose")
.action(ArgAction::Count)
.help("Verbosity to use, default to WARN")
.help("Verbosity to use, default to WARN"),
])
.get_matches()
}
Expand All @@ -57,6 +59,8 @@ pub(crate) struct LintcheckConfig {
pub sources_toml_path: PathBuf,
/// we save the clippy lint results here
pub lintcheck_results_path: PathBuf,
// /// we save the clippy logs here
// pub lintcheck_log_path: PathBuf,
/// Check only a specified package
pub only: Option<String>,
/// whether to just run --fix and not collect all the warnings
Expand All @@ -72,19 +76,22 @@ pub(crate) struct LintcheckConfig {
impl LintcheckConfig {
pub fn new() -> Self {
let clap_config = get_clap_config();
let mut builder = Builder::new();
builder
.filter(
None,
match clap_config.get_count("verbose") {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 => LevelFilter::Debug,
_ => LevelFilter::Trace,
},
)
.default_format()
.init();
let level_filter = match clap_config.get_count("verbose") {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 => dbg!(LevelFilter::Debug),
_ => LevelFilter::Trace,
};
// using `create_dir_all` as it does not error when the dir already exists
fs::create_dir_all("lintcheck-logs").expect("Creating the log dir failed");
let _ = CombinedLogger::init(vec![
TermLogger::new(level_filter, Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
WriteLogger::new(
level_filter,
Config::default(),
File::create("lintcheck-logs/lintcheck.log").unwrap(),
),
]);

// first, check if we got anything passed via the LINTCHECK_TOML env var,
// if not, ask clap if we got any value for --crates-toml <foo>
Expand All @@ -103,7 +110,7 @@ impl LintcheckConfig {
// wasd.toml, use "wasd"...)
let filename: PathBuf = sources_toml_path.file_stem().unwrap().into();
let lintcheck_results_path = PathBuf::from(format!(
"lintcheck-logs/{}_logs.{}",
"lintcheck-logs/{}_results.{}",
filename.display(),
if markdown { "md" } else { "txt" }
));
Expand Down
7 changes: 2 additions & 5 deletions lintcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ impl Crate {
let status = &all_output.status;

if !status.success() {
warn!("bad exit status after checking {} {} \n",
self.name, self.version
);
warn!("bad exit status after checking {} {} \n", self.name, self.version);
}

if config.fix {
Expand Down Expand Up @@ -694,8 +692,7 @@ fn main() {
let _ = write!(text, "{cratename}: '{msg}'");
}

println!("Writing logs to {}", config.lintcheck_results_path.display());
fs::create_dir_all(config.lintcheck_results_path.parent().unwrap()).unwrap();
println!("Writing results to {}", config.lintcheck_results_path.display());
fs::write(&config.lintcheck_results_path, text).unwrap();

print_stats(old_stats, new_stats, &config.lint_filter);
Expand Down

0 comments on commit eaaa0de

Please sign in to comment.