Improved server-side logs

This commit is contained in:
Phuntsok Drak-pa 2018-03-25 14:54:40 +02:00
parent 5a56afb709
commit 517d66b11b
1 changed files with 74 additions and 4 deletions

View File

@ -167,6 +167,12 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
let name: String = match (|| loop { let name: String = match (|| loop {
match receive!() { match receive!() {
input => { input => {
println!(
"{time} Client {addr} : {message}",
time = get_time(),
addr = client,
message = input
);
let spliced_input: Vec<&str> = input.split_whitespace().collect(); let spliced_input: Vec<&str> = input.split_whitespace().collect();
if spliced_input.len() != 4 && spliced_input.len() != 5 if spliced_input.len() != 4 && spliced_input.len() != 5
|| spliced_input[0] != "PROT" || spliced_input[0] != "PROT"
@ -183,6 +189,12 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
for c in username.chars() { for c in username.chars() {
if !c.is_ascii() { if !c.is_ascii() {
ascii_nick = false; ascii_nick = false;
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME FAILURE"
);
send!("NAME FAILURE"); send!("NAME FAILURE");
break; break;
} }
@ -199,9 +211,21 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
} }
} }
if used == false { if used == false {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME OK"
);
send!("NAME OK"); send!("NAME OK");
return Ok(username); return Ok(username);
} else { } else {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME FAILURE"
);
send!("NAME FAILURE"); send!("NAME FAILURE");
} }
} }
@ -211,9 +235,21 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
} }
loop { loop {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME REQ"
);
send!("NAME REQ"); send!("NAME REQ");
match receive!() { match receive!() {
input => { input => {
println!(
"{time} Client {addr} : {message}",
time = get_time(),
addr = client,
message = input
);
let spliced_input: Vec<&str> = input.split_whitespace().collect(); let spliced_input: Vec<&str> = input.split_whitespace().collect();
if spliced_input.len() != 2 || spliced_input[0] != "NAME" { if spliced_input.len() != 2 || spliced_input[0] != "NAME" {
return Err(Error::new(ErrorKind::Other, "BAD REQ")); return Err(Error::new(ErrorKind::Other, "BAD REQ"));
@ -223,6 +259,12 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
for c in username.chars() { for c in username.chars() {
if !c.is_ascii() { if !c.is_ascii() {
ascii_nick = false; ascii_nick = false;
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME FAILURE"
);
send!("NAME FAILURE"); send!("NAME FAILURE");
break; break;
} }
@ -239,9 +281,21 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
} }
} }
if used == false { if used == false {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME OK"
);
send!("NAME OK"); send!("NAME OK");
return Ok(username); return Ok(username);
} else { } else {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME FAILURE"
);
send!("NAME FAILURE"); send!("NAME FAILURE");
} }
} }
@ -255,7 +309,7 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
Ok(name) => name, Ok(name) => name,
Err(e) => { Err(e) => {
println!( println!(
"{time} Client {addr} encountered an error: {err}", "{time} client {addr} encountered an error: {err}",
time = get_time(), time = get_time(),
addr = client, addr = client,
err = e err = e
@ -290,11 +344,25 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
match input { match input {
"BYE" => { "BYE" => {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "BYE"
);
send!("BYE"); send!("BYE");
return Ok(()); return Ok(());
} }
"PING" => send!("PONG"), "PING" => {
println!(
"{time} to client {addr} : {message}",
time = get_time(),
addr = client,
message = "NAME FAILURE"
);
send!("PONG");
}
"REQ CLIENTS" => { "REQ CLIENTS" => {
let mut lock = clients.lock().unwrap(); let mut lock = clients.lock().unwrap();
@ -320,9 +388,11 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
} }
_ => { _ => {
println!( println!(
"{time} Sending: BAD REQ. Cause: {message}", "{time} to client {addr} : \"{message}\", cause : {inmessage}",
time = get_time(), time = get_time(),
message = input addr = client,
message = "BAD REQ",
inmessage = input
); );
send!("BAD REQ"); send!("BAD REQ");
} }