From 3b7d22e67562dfec6e4e962bf88bdb5d93bd038b Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 9 Jan 2020 12:30:35 +0000 Subject: [PATCH] clients: set up a logger to chill out output --- Cargo.lock | 60 +++++++++++++++++++++++++++ clients/mix-client/Cargo.toml | 1 + clients/mix-client/src/lib.rs | 3 +- clients/nym-client/Cargo.toml | 2 + clients/nym-client/src/clients/mod.rs | 28 ++++++++----- clients/nym-client/src/main.rs | 6 ++- clients/provider-client/Cargo.toml | 1 + clients/provider-client/src/lib.rs | 5 ++- 8 files changed, 91 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c82d49cc11..f5919935f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -491,6 +491,18 @@ dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "error-chain" version = "0.12.1" @@ -772,6 +784,14 @@ name = "httparse" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "hyper" version = "0.12.35" @@ -1051,6 +1071,7 @@ dependencies = [ name = "mix-client" version = "0.1.0" dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "sphinx 0.1.0", "tokio 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1136,8 +1157,10 @@ dependencies = [ "curve25519-dalek 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "directory-client 0.1.0", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "mix-client 0.1.0", "pem 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "provider-client 0.1.0", @@ -1336,6 +1359,7 @@ name = "provider-client" version = "0.1.0" dependencies = [ "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "sfw-provider-requests 0.1.0", "sphinx 0.1.0", "tokio 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1353,6 +1377,11 @@ dependencies = [ "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quote" version = "1.0.2" @@ -1847,6 +1876,14 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termcolor" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -2224,11 +2261,28 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wincolor" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winreg" version = "0.6.2" @@ -2304,6 +2358,7 @@ dependencies = [ "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum encoding_rs 0.8.22 (registry+https://github.com/rust-lang/crates.io-index)" = "cd8d03faa7fe0c1431609dfad7bbe827af30f82e1e2ae6f7ee4fca6bd764bc28" +"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" "checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" "checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" "checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08" @@ -2337,6 +2392,7 @@ dependencies = [ "checksum http 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0" "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +"checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" "checksum hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)" = "9dbe6ed1438e1f8ad955a4701e9a944938e9519f6888d12d8558b645e247d5f6" "checksum hyper-tls 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" @@ -2392,6 +2448,7 @@ dependencies = [ "checksum proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "369a6ed065f249a159e06c45752c780bda2fb53c995718f9e484d08daa9eb42e" "checksum proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0319972dcae462681daf4da1adeeaa066e3ebd29c69be96c6abb1259d2ee2bcc" "checksum publicsuffix 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3bbaa49075179162b49acac1c6aa45fb4dafb5f13cf6794276d77bc7fd95757b" +"checksum quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" @@ -2444,6 +2501,7 @@ dependencies = [ "checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +"checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" @@ -2484,6 +2542,8 @@ dependencies = [ "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9" "checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/clients/mix-client/Cargo.toml b/clients/mix-client/Cargo.toml index e60e471da6..bd033b31f9 100644 --- a/clients/mix-client/Cargo.toml +++ b/clients/mix-client/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +log = "0.4.8" tokio = { version = "0.2", features = ["full"] } ## will be moved to proper dependencies once released diff --git a/clients/mix-client/src/lib.rs b/clients/mix-client/src/lib.rs index 4eb486fff2..431623178a 100644 --- a/clients/mix-client/src/lib.rs +++ b/clients/mix-client/src/lib.rs @@ -1,3 +1,4 @@ +use log::*; use sphinx::SphinxPacket; use std::net::SocketAddr; use tokio::prelude::*; @@ -17,7 +18,7 @@ impl MixClient { ) -> Result<(), Box> { let bytes = packet.to_bytes(); - println!("socket addr: {:?}", mix_addr); + info!("socket addr: {:?}", mix_addr); let mut stream = tokio::net::TcpStream::connect(mix_addr).await?; stream.write_all(&bytes[..]).await?; diff --git a/clients/nym-client/Cargo.toml b/clients/nym-client/Cargo.toml index f2d80a5409..3a1f88458e 100644 --- a/clients/nym-client/Cargo.toml +++ b/clients/nym-client/Cargo.toml @@ -16,8 +16,10 @@ base64 = "0.11.0" clap = "2.33.0" curve25519-dalek = "1.2.3" dirs = "2.0.2" +env_logger = "0.7.1" futures = "0.3.1" hex = "0.4.0" +log = "0.4.8" pem = "0.7.0" rand = "0.7.2" rand_distr = "0.2.2" diff --git a/clients/nym-client/src/clients/mod.rs b/clients/nym-client/src/clients/mod.rs index 4737c7a70a..9fe8d5ea6f 100644 --- a/clients/nym-client/src/clients/mod.rs +++ b/clients/nym-client/src/clients/mod.rs @@ -7,9 +7,12 @@ use futures::join; use futures::lock::Mutex as FMutex; use futures::select; use futures::{SinkExt, StreamExt}; +use log::*; use sfw_provider_requests::AuthToken; use sphinx::route::{Destination, DestinationAddressBytes}; use sphinx::SphinxPacket; +use std::io; +use std::io::prelude::*; use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; @@ -39,14 +42,17 @@ impl MixTrafficController { async fn run(mut rx: mpsc::UnboundedReceiver) { let mix_client = mix_client::MixClient::new(); while let Some(mix_message) = rx.next().await { - println!( + info!( "[MIX TRAFFIC CONTROL] - got a mix_message for {:?}", mix_message.0 ); let send_res = mix_client.send(mix_message.1, mix_message.0).await; match send_res { - Ok(_) => println!("We successfully sent the message!"), - Err(e) => eprintln!("We failed to send the message :( - {:?}", e), + Ok(_) => { + print!("."); + io::stdout().flush().ok().expect("Could not flush stdout"); + } + Err(e) => error!("We failed to send the message :( - {:?}", e), }; } } @@ -70,7 +76,7 @@ impl ReceivedMessagesBuffer { } async fn add_new_messages(buf: Arc>, msgs: Vec>) { - println!("Adding new messages to the buffer! {:?}", msgs); + info!("Adding new messages to the buffer! {:?}", msgs); let mut unlocked = buf.lock().await; unlocked.messages.extend(msgs); } @@ -150,7 +156,7 @@ impl NymClient { topology: Topology, ) { loop { - println!("[LOOP COVER TRAFFIC STREAM] - next cover message!"); + info!("[LOOP COVER TRAFFIC STREAM] - next cover message!"); let delay = utils::poisson::sample(LOOP_COVER_AVERAGE_DELAY); let delay_duration = Duration::from_secs_f64(delay); tokio::time::delay_for(delay_duration).await; @@ -169,13 +175,13 @@ impl NymClient { topology: Topology, ) { loop { - println!("[OUT QUEUE] here I will be sending real traffic (or loop cover if nothing is available)"); + info!("[OUT QUEUE] here I will be sending real traffic (or loop cover if nothing is available)"); // TODO: consider replacing select macro with our own proper future definition with polling let traffic_message = select! { real_message = input_rx.next() => { - println!("[OUT QUEUE] - we got a real message!"); + info!("[OUT QUEUE] - we got a real message!"); if real_message.is_none() { - eprintln!("Unexpected 'None' real message!"); + error!("Unexpected 'None' real message!"); std::process::exit(1); } let real_message = real_message.unwrap(); @@ -184,7 +190,7 @@ impl NymClient { }, default => { - println!("[OUT QUEUE] - no real message - going to send extra loop cover"); + info!("[OUT QUEUE] - no real message - going to send extra loop cover"); utils::sphinx::loop_cover_message(our_info.address, our_info.identifier, &topology) } }; @@ -208,7 +214,7 @@ impl NymClient { loop { let delay_duration = Duration::from_secs_f64(FETCH_MESSAGES_DELAY); tokio::time::delay_for(delay_duration).await; - println!("[FETCH MSG] - Polling provider..."); + info!("[FETCH MSG] - Polling provider..."); let messages = provider_client.retrieve_messages().await.unwrap(); let good_messages = messages .into_iter() @@ -243,7 +249,7 @@ impl NymClient { None => { let auth_token = provider_client.register().await.unwrap(); provider_client.update_token(auth_token); - println!("Obtained new token! - {:?}", auth_token); + info!("Obtained new token! - {:?}", auth_token); } Some(token) => println!("Already got the token! - {:?}", token), } diff --git a/clients/nym-client/src/main.rs b/clients/nym-client/src/main.rs index 34b1b63b8a..7edda10274 100644 --- a/clients/nym-client/src/main.rs +++ b/clients/nym-client/src/main.rs @@ -1,6 +1,8 @@ #![recursion_limit = "256"] use clap::{App, Arg, ArgMatches, SubCommand}; +use env_logger; +use log::*; use std::process; pub mod clients; @@ -11,6 +13,8 @@ mod sockets; mod utils; fn main() { + env_logger::init(); + let arg_matches = App::new("Nym Client") .version(built_info::PKG_VERSION) .author("Nymtech") @@ -80,7 +84,7 @@ fn main() { .get_matches(); if let Err(e) = execute(arg_matches) { - println!("{}", e); + error!("{}", e); process::exit(1); } } diff --git a/clients/provider-client/Cargo.toml b/clients/provider-client/Cargo.toml index 876e1ac9c9..d3411463ff 100644 --- a/clients/provider-client/Cargo.toml +++ b/clients/provider-client/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] futures = "0.3.1" +log = "0.4.8" tokio = { version = "0.2", features = ["full"] } ## internal diff --git a/clients/provider-client/src/lib.rs b/clients/provider-client/src/lib.rs index cb273b414c..643770c1dc 100644 --- a/clients/provider-client/src/lib.rs +++ b/clients/provider-client/src/lib.rs @@ -1,4 +1,5 @@ use futures::io::Error; +use log::info; use sfw_provider_requests::requests::{ProviderRequest, PullRequest, RegisterRequest}; use sfw_provider_requests::responses::{ ProviderResponse, ProviderResponseError, PullResponse, RegisterResponse, @@ -64,7 +65,7 @@ impl ProviderClient { pub async fn send_request(&self, bytes: Vec) -> Result, ProviderClientError> { let mut socket = tokio::net::TcpStream::connect(self.provider_network_address).await?; - println!("keep alive: {:?}", socket.keepalive()); + info!("keep alive: {:?}", socket.keepalive()); socket.set_keepalive(Some(Duration::from_secs(2))).unwrap(); socket.write_all(&bytes[..]).await?; if let Err(_e) = socket.shutdown(Shutdown::Write) { @@ -91,7 +92,7 @@ impl ProviderClient { let bytes = pull_request.to_bytes(); let response = self.send_request(bytes).await?; - println!("Received the following response: {:?}", response); + info!("Received the following response: {:?}", response); let parsed_response = PullResponse::from_bytes(&response)?; Ok(parsed_response.messages)