From 91015b476b31ffc544aac631dc5413119fa43e6d Mon Sep 17 00:00:00 2001 From: Alex Newton Date: Thu, 1 Sep 2022 15:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BB=A5info=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=E6=98=BE=E7=A4=BAlog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 11 ++++++++++- src/consts.rs | 2 +- src/lib.rs | 2 +- src/parse.rs | 7 ++++--- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eed49bd..fa0f8cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,7 +194,7 @@ dependencies = [ [[package]] name = "etrc12306gen" -version = "0.0.1" +version = "0.1.0" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index b8025cd..906d72e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "etrc12306gen" authors = ["Alex Newton "] -version = "0.0.1" +version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/cli.rs b/src/cli.rs index b646a61..08a8ca9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use std::time; +use std::{env, time}; use clap::{Args, Parser, Subcommand}; use log::debug; @@ -26,7 +26,16 @@ pub struct Cli { } impl Cli { pub fn run(&self) -> Result<(), Box> { + // 初始化log + let env_log = env::var("RUST_LOG"); + let log_level = if let Ok(env_log) = &env_log { + env_log.as_str() + } else { + "info" + }; + env::set_var("RUST_LOG", log_level); pretty_env_logger::init(); + let mut builder = Client::builder() .cookie_store(true) .timeout(time::Duration::from_secs(7)); diff --git a/src/consts.rs b/src/consts.rs index 1581bb9..0f1b454 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -3257,4 +3257,4 @@ pub static ref STATION_CODE: HashMap<&'static str, &'static str> =[ ("漾濞" , "AVM"), ("永平县" , "APM"), ].iter().cloned().collect(); -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index ef7b793..1feec7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,4 @@ pub mod cli; mod consts; pub mod parse; pub mod pyetgr; -mod utils; \ No newline at end of file +mod utils; diff --git a/src/parse.rs b/src/parse.rs index 4a301ce..b1637a3 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -33,7 +33,7 @@ pub fn parse_line( from_value(from_slice::(&data)?["line"]["stations"].clone())?; debug!("开始读取Cookie"); let mut cookie: Vec = vec![]; - match OpenOptions::new() + if OpenOptions::new() .create(true) .write(true) .read(true) @@ -41,9 +41,9 @@ pub fn parse_line( .expect("创建cookie.txt文件失败") .read_to_end(&mut cookie) .expect("读取失败cookie.txt文件失败") + == 0 { - 0 => panic!("读取cookie.txt失败, 文件为空,请填入cookie后再使用"), - _ => (), + panic!("读取cookie.txt失败, 文件为空,请填入cookie后再使用"); } let cookie = String::from_utf8(cookie)?; debug!("加载Cookie成功"); @@ -226,5 +226,6 @@ pub fn parse_train( train_history.push(train_number.0); train_history.push(train_number.1); } + info!("获取时刻表任务完成"); Ok(()) }