From 9c51a04f693981d8001d838280d040cf0a75840a Mon Sep 17 00:00:00 2001 From: PiyushXCoder Date: Sat, 20 Feb 2021 14:14:46 +0530 Subject: [PATCH] added logging --- Cargo.lock | 43 ++++++++++++++-- Cargo.toml | 2 +- src/main.rs | 6 ++- vecmap/Cargo.lock | 5 -- vecmap/Cargo.toml | 9 ---- vecmap/src/errors.rs | 24 --------- vecmap/src/lib.rs | 116 ------------------------------------------- 7 files changed, 44 insertions(+), 161 deletions(-) delete mode 100644 vecmap/Cargo.lock delete mode 100644 vecmap/Cargo.toml delete mode 100644 vecmap/src/errors.rs delete mode 100644 vecmap/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 30ad0ce..d68f82d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -677,6 +677,19 @@ dependencies = [ "syn", ] +[[package]] +name = "env_logger" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "flate2" version = "1.0.20" @@ -935,6 +948,12 @@ version = "1.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "idna" version = "0.2.0" @@ -1063,11 +1082,11 @@ dependencies = [ "actix-web", "actix-web-actors", "clap", + "env_logger", "futures", "rand 0.8.3", "serde", "serde_json", - "vecmap", ] [[package]] @@ -1668,6 +1687,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -1976,10 +2004,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" -[[package]] -name = "vecmap" -version = "0.1.0" - [[package]] name = "version_check" version = "0.1.5" @@ -2092,6 +2116,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 2dca4fb..0bab179 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,10 @@ actix-web = "3" actix-web-actors = "3" actix-broker = "0.3.1" actix-files = "0.5.0" +env_logger = "0.8.3" clap = "2.33.3" serde = "1.0.123" serde_json = "1.0.62" rand = "0.8.3" futures = "0.3.12" -vecmap = { path = "vecmap" } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 298d0e9..d1e7d35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ //! |--> ws_sansad4 <----/ //! -use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, web}; +use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, middleware::Logger, web}; use actix_files as fs; use actix_web_actors::ws; use ws_sansad::WsSansad; @@ -22,10 +22,14 @@ mod chat_pinnd; mod validator; #[actix_web::main] async fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + let config = config::Config::new(); let static_path = config.static_path; HttpServer::new(move || { App::new() + .wrap(Logger::new("%t [%a] %s %{User-Agent}i %r")) .service(web::resource("/ws/").route(web::get().to(ws_index))) .service(fs::Files::new("/", &static_path).index_file("index.html")) }) diff --git a/vecmap/Cargo.lock b/vecmap/Cargo.lock deleted file mode 100644 index 4f81543..0000000 --- a/vecmap/Cargo.lock +++ /dev/null @@ -1,5 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "vecmap" -version = "0.1.0" diff --git a/vecmap/Cargo.toml b/vecmap/Cargo.toml deleted file mode 100644 index 00af475..0000000 --- a/vecmap/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "vecmap" -version = "0.1.0" -authors = ["piyush"] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/vecmap/src/errors.rs b/vecmap/src/errors.rs deleted file mode 100644 index 81411c8..0000000 --- a/vecmap/src/errors.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::fmt::{Display, Formatter, Result}; -use std::error::Error; - -#[derive(Debug)] -pub struct ElementNotFount; - -#[derive(Debug)] -pub struct KeyAlreadyExist; - -impl Display for ElementNotFount { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!(f, "Element not found") - } -} - -impl Error for ElementNotFount {} - -impl Display for KeyAlreadyExist { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!(f, "Key already do exist") - } -} - -impl Error for KeyAlreadyExist {} \ No newline at end of file diff --git a/vecmap/src/lib.rs b/vecmap/src/lib.rs deleted file mode 100644 index c516f7e..0000000 --- a/vecmap/src/lib.rs +++ /dev/null @@ -1,116 +0,0 @@ -use std::error::Error; - -pub mod errors; - -#[derive(Debug, Clone)] -pub struct VecMap(Vec>); - -#[derive(Debug, Clone)] -pub struct VecMapElement { - key: K, - value: V -} - -impl IntoIterator for VecMap { - type IntoIter = std::vec::IntoIter>; - type Item = VecMapElement; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl VecMap { - pub fn new() -> VecMap { - VecMap(Vec::new()) - } - - pub fn insert(&mut self, key: K, value: V) { - let key_tmp = key.clone(); - match self.0.iter_mut().find(move |a| a.key == key_tmp) { - Some(i) => { - i.value = value.clone(); - } - None => { - self.0.push(VecMapElement { - key, - value - }); - } - } - } - - pub fn get(&self, key: &K) -> Option<&V> { - match self.0.iter().find(|a| &a.key == key) { - Some(v) => Some(&v.value), - None => None - } - } - - pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { - match self.0.iter_mut().find(move |a| &a.key == key) { - Some(v) => Some(&mut v.value), - None => None - } - } - - pub fn remove(&mut self, key: &K) -> Result<(), errors::ElementNotFount> { - match self.0.iter().position(move |a| &a.key == key) { - Some(i) => { - self.0.remove(i); - } - None => { - return Err(errors::ElementNotFount); - } - } - Ok(()) - } - - pub fn change_key(&mut self, key: &K, new_key: &K) -> Result<(), &dyn Error> { - let key_tmp = key.clone(); - if let Some(_) = self.0.iter().position(move |a| a.key == key_tmp) { - return Err(&errors::KeyAlreadyExist); - } - - - match self.0.iter_mut().find(move |a| &a.key == key) { - Some(i) => { - i.key = new_key.to_owned(); - } - None => { - return Err(&errors::ElementNotFount); - } - } - Ok(()) - } - - pub fn key_exist(&self, key: &K) -> bool { - match self.0.iter().position(move |a| &a.key == key) { - Some(_) => true, - None => false - } - } - - pub fn len(&self) -> usize { - self.0.len() - } -} - -#[cfg(test)] -mod tests { - - use crate::VecMap; - - #[test] - fn it_works() { - let mut a: VecMap = super::VecMap::new(); - - a.insert(1, 5); - a.insert(2, 10); - a.remove(&1); - - a.into_iter().enumerate().for_each(|(i,e)| { - println!("{}\t-> {:?}", i,e); - }); - } -}