Improve log

This commit is contained in:
nyyu 2021-03-15 21:15:03 +01:00 committed by Gitea
parent 5b39644df0
commit 5117bbe659
2 changed files with 16 additions and 11 deletions

View file

@ -62,7 +62,7 @@ pub trait DataSource {
path: &PathBuf, path: &PathBuf,
client: &ureq::Agent, client: &ureq::Agent,
) { ) {
info!("Retrieving data for {} at {}", champ.name, pos); info!("{}: Retrieving data for {} at {}", self.get_alias(), champ.name, pos);
let data = self.get_champ_data_with_win_pourcentage(champ, pos, client); let data = self.get_champ_data_with_win_pourcentage(champ, pos, client);
match data { match data {
@ -77,7 +77,7 @@ pub trait DataSource {
blocks: data.0, blocks: data.0,
}; };
info!("Writing item set for {} at {}", champ.name, pos); info!("{}: Writing item set for {} at {}", self.get_alias(), champ.name, pos);
fs::write( fs::write(
path.join(format!("{}_{}_{}.json", self.get_alias(), champ.id, pos)), path.join(format!("{}_{}_{}.json", self.get_alias(), champ.id, pos)),
serde_json::to_string_pretty(&item_set).unwrap(), serde_json::to_string_pretty(&item_set).unwrap(),
@ -85,7 +85,7 @@ pub trait DataSource {
.unwrap(); .unwrap();
} }
None => { None => {
error!("Can't get data for {} at {}", champ.id, pos); error!("{}: Can't get data for {} at {}", self.get_alias(), champ.id, pos);
} }
} }
} }

View file

@ -12,6 +12,7 @@ use std::io::Error;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant;
use std::{fs, thread, time}; use std::{fs, thread, time};
use time::Duration; use time::Duration;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@ -44,7 +45,6 @@ pub struct ChampInfo {
key: String, key: String,
} }
const USER_AGENT_KEY: &str = "User-Agent";
const USER_AGENT_VALUE: &str = const USER_AGENT_VALUE: &str =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"; "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0";
const DEFAULT_LOL_CHAMPS_DIR: &str = ".\\champs"; const DEFAULT_LOL_CHAMPS_DIR: &str = ".\\champs";
@ -64,11 +64,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for s in &args { for s in &args {
if s.eq_ignore_ascii_case("-v") || s.eq_ignore_ascii_case("--verbose") { if s.eq_ignore_ascii_case("-v") || s.eq_ignore_ascii_case("--verbose") {
level = LevelFilter::Debug; level = LevelFilter::Debug;
break;
} }
} }
SimpleLogger::new() SimpleLogger::new()
.with_level(level) .with_level(level)
.with_module_level("ureq", LevelFilter::Error) .with_module_level("ureq", LevelFilter::Error)
.with_module_level("rustls", LevelFilter::Error)
.init()?; .init()?;
info!("CGG Item Sets"); info!("CGG Item Sets");
@ -78,11 +80,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR),
}; };
static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new() static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new()
.user_agent(USER_AGENT_VALUE)
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();
static ref REALM: Realm = CLIENT static ref REALM: Realm = CLIENT
.get("https://ddragon.leagueoflegends.com/realms/euw.json") .get("https://ddragon.leagueoflegends.com/realms/euw.json")
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call() .call()
.unwrap() .unwrap()
.into_json() .into_json()
@ -92,7 +94,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", "https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
REALM.v REALM.v
)) ))
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call() .call()
.unwrap() .unwrap()
.into_json() .into_json()
@ -104,16 +105,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
]; ];
} }
info!( info!("LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap());
"LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap()
);
info!("LoL version: {}", REALM.v); info!("LoL version: {}", REALM.v);
info!("LoL numbers of champs: {}", CHAMPION.data.len()); info!("LoL numbers of champs: {}", CHAMPION.data.len());
let mut threads = vec![]; let mut threads = vec![];
for data_source in DATA_SOURCES.iter() { for data_source in DATA_SOURCES.iter() {
threads.push(thread::spawn(move || { threads.push(thread::spawn(move || {
execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR) let init = Instant::now();
execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR);
info!(
"{}: done in {} ms",
data_source.get_alias(),
init.elapsed().as_millis()
);
})); }));
} }
for child in threads { for child in threads {
@ -157,7 +162,7 @@ fn execute_data_source(
if let Some(champ) = champion.data.get(&champ_id) { if let Some(champ) = champion.data.get(&champ_id) {
if positions.is_empty() { if positions.is_empty() {
error!("{} missing positions", &champ_id); error!("{}: {} empty positions", data_source.get_alias(), &champ_id);
} else { } else {
let path = lol_champs_dir.join(&champ_id).join("Recommended"); let path = lol_champs_dir.join(&champ_id).join("Recommended");
fs::create_dir_all(&path).unwrap(); fs::create_dir_all(&path).unwrap();