From 7cb7249c2b02b702b8dbf3a5290e2a46bee5e645 Mon Sep 17 00:00:00 2001 From: SkyBlockDev <72335827+SkyBlockDev@users.noreply.github.com> Date: Sat, 11 Sep 2021 12:03:48 +0200 Subject: [PATCH] Add an actual logger --- .gitignore | 2 + Cargo.lock | 153 +++++++++++++++++++++----------------------------- Cargo.toml | 9 +-- src/fetch.rs | 15 +++-- src/main.rs | 16 ++++++ src/server.rs | 9 +-- 6 files changed, 100 insertions(+), 104 deletions(-) diff --git a/.gitignore b/.gitignore index f5841b0..9d57d45 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ node_modules .idea/ lowestbins.json +debug.log +info.log diff --git a/Cargo.lock b/Cargo.lock index e9354e8..28690fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,15 +8,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "aho-corasick" -version = "0.7.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" -dependencies = [ - "memchr", -] - [[package]] name = "alloc-no-stdlib" version = "2.0.3" @@ -46,17 +37,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.0.1" @@ -132,6 +112,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + [[package]] name = "core-foundation" version = "0.9.1" @@ -166,19 +159,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "flate2" version = "1.0.21" @@ -353,15 +333,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "hyper" version = "0.14.12" @@ -485,16 +456,15 @@ name = "lowestbins" version = "0.1.0" dependencies = [ "base64", - "flate2", "hematite-nbt", "hyper", "lazy_static", - "nbtrs", - "pretty_env_logger", + "log", "reqwest", "serde", - "serde_derive", "serde_json", + "simplelog", + "substring", "tokio", "tokio-util", ] @@ -567,15 +537,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "nbtrs" -version = "0.1.1" -source = "git+https://github.com/overviewer/nbtrs#abd80daf4ffb821489628274008da213d5964671" -dependencies = [ - "byteorder", - "flate2", -] - [[package]] name = "ntapi" version = "0.3.6" @@ -585,6 +546,25 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -689,16 +669,6 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" -[[package]] -name = "pretty_env_logger" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "proc-macro2" version = "1.0.29" @@ -708,12 +678,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.9" @@ -772,23 +736,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "regex" -version = "1.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" - [[package]] name = "remove_dir_all" version = "0.5.3" @@ -932,6 +879,17 @@ dependencies = [ "libc", ] +[[package]] +name = "simplelog" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59d0fe306a0ced1c88a58042dc22fc2ddd000982c26d75f6aa09a394547c41e0" +dependencies = [ + "chrono", + "log", + "termcolor", +] + [[package]] name = "slab" version = "0.4.4" @@ -954,6 +912,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "substring" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ee6433ecef213b2e72f587ef64a2f5943e7cd16fbd82dbe8bc07486c534c86" +dependencies = [ + "autocfg", +] + [[package]] name = "syn" version = "1.0.76" @@ -988,6 +955,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "tinyvec" version = "1.3.1" diff --git a/Cargo.toml b/Cargo.toml index 792ab20..951a76b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,14 +10,11 @@ serde_json = "1.0" hyper = { version = "0.14", features = ["full"] } serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } -serde_derive = "1.0.130" -pretty_env_logger = "0.4" tokio-util = "0.6.8" reqwest = { version="0.11.4", features = ["json", "gzip", "brotli"] } base64 = {version="0.13.0" } hematite-nbt = { version="0.5.2" } -nbtrs = {git="https://github.com/overviewer/nbtrs"} -flate2 = "1" lazy_static = "1.4.0" - - +log="0.4.14" +simplelog = "^0.10.0" +substring="1.4.5" \ No newline at end of file diff --git a/src/fetch.rs b/src/fetch.rs index 03bff12..d876b36 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,10 +1,12 @@ use crate::bazaar::get as get_bazaar; use crate::util::{get, parse_hypixel}; +use log::{debug, info}; use std::collections::HashMap; use std::fs; use std::time::Instant; pub async fn fetch_auctions() { + info!("fetching auctions"); let started = Instant::now(); let mut auctions: HashMap = HashMap::new(); @@ -12,18 +14,19 @@ pub async fn fetch_auctions() { let r = get(1).await; auctions = parse_hypixel(r.auctions, auctions); for a in 2..r.total_pages { - println!("------------------------ req: {}", a); + debug!("------------------------ req: {}", a); let now = Instant::now(); let r = get(a).await; - println!(": request took {} miliseconds", now.elapsed().as_millis()); + debug!(": request took {} miliseconds", now.elapsed().as_millis()); let nowss = Instant::now(); auctions = parse_hypixel(r.auctions, auctions); - println!("$ parsing took {} miliseconds", nowss.elapsed().as_millis()); - println!( + debug!("$ parsing took {} miliseconds", nowss.elapsed().as_millis()); + debug!( "! request and parsing took {} miliseconds", now.elapsed().as_millis() ); } + info!("fetching bazaar"); let r = get_bazaar().await; let prods = r.products; for (key, val) in prods.iter() { @@ -31,7 +34,7 @@ pub async fn fetch_auctions() { } let xs = serde_json::to_string(&auctions).unwrap(); - println!("writing file"); - println!("!! Total time taken {}", started.elapsed().as_secs()); + debug!("writing file"); + info!("!! Total time taken {}", started.elapsed().as_secs()); fs::write("lowestbins.json", xs).unwrap(); } diff --git a/src/main.rs b/src/main.rs index 94d013e..006798d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,26 @@ use lowestbins::fetch::fetch_auctions; use lowestbins::server::start_server; use lowestbins::util::set_interval; +use simplelog::*; +use std::fs::File; use tokio::time::Duration; #[tokio::main] async fn main() -> Result<(), Box> { + println!("creating logger"); + CombinedLogger::init(vec![ + WriteLogger::new( + LevelFilter::Info, + Config::default(), + File::create("info.log").unwrap(), + ), + WriteLogger::new( + LevelFilter::Debug, + Config::default(), + File::create("debug.log").unwrap(), + ), + ]) + .unwrap(); fetch_auctions().await; set_interval( diff --git a/src/server.rs b/src/server.rs index a1d07f4..b2c7dfe 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,7 @@ use hyper::service::{make_service_fn, service_fn}; use hyper::{header, Body, Method, Request, Response, Result, Server}; +use log::{error, info}; +use substring::Substring; use tokio::fs::File; use tokio_util::codec::{BytesCodec, FramedRead}; @@ -7,8 +9,6 @@ static INDEX: &str = "lowestbins.json"; static NOTFOUND: &[u8] = b"Not Found"; pub async fn start_server() { - pretty_env_logger::init(); - let addr = "127.0.0.1:1337".parse().unwrap(); let make_service = @@ -16,15 +16,16 @@ pub async fn start_server() { let server = Server::bind(&addr).serve(make_service); + info!("Listening on http://{}", addr); println!("Listening on http://{}", addr); if let Err(e) = server.await { - eprintln!("server error: {}", e); + error!("server error: {}", e); } } async fn response_examples(req: Request) -> Result> { - println!("{} {}", req.method(), req.uri().path()); + info!("{} {}", req.method(), req.uri().path().substring(0, 30)); match (req.method(), req.uri().path()) { (&Method::GET, "/lowestbins.json") | (&Method::GET, "/lowestbins") => {