Feat: Logging, prettify chat history
This commit is contained in:
		
							
								
								
									
										27
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -4,21 +4,22 @@ use tokio::{
 | 
			
		||||
    net::TcpListener,
 | 
			
		||||
    sync::broadcast,
 | 
			
		||||
};
 | 
			
		||||
use tracing::info;
 | 
			
		||||
use tracing_subscriber::filter::EnvFilter;
 | 
			
		||||
use log::info;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<(), Report> {
 | 
			
		||||
    env_logger::init();
 | 
			
		||||
    // Setup the Environment
 | 
			
		||||
    setup()?;
 | 
			
		||||
 | 
			
		||||
    // Set up a TCP listener to listen for incoming tcp requests
 | 
			
		||||
    let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
 | 
			
		||||
    info!("Bounded to localhost port 8080");
 | 
			
		||||
 | 
			
		||||
    // Setting up broadcast channel: A channel that will accept messages and broadcast them to everyone connected to the TCP server
 | 
			
		||||
    // the channel accepts an i32 that is the maximum number of messages the channel can retain at any given time
 | 
			
		||||
    let (sender, _receiver) = broadcast::channel(10);
 | 
			
		||||
    let (sender, _receiver) = broadcast::channel(100);
 | 
			
		||||
 | 
			
		||||
    // First loop accept all the tcp client requests
 | 
			
		||||
    loop {
 | 
			
		||||
        // prevent the compiler error when we try to access something in a loop when it was initialized outside of the loop
 | 
			
		||||
        let sender = sender.clone();
 | 
			
		||||
@@ -27,6 +28,7 @@ async fn main() -> Result<(), Report> {
 | 
			
		||||
 | 
			
		||||
        // accept the requests
 | 
			
		||||
        let (mut socket, addr) = listener.accept().await.unwrap();
 | 
			
		||||
        info!("Accepted a websocket request from {}", addr);
 | 
			
		||||
 | 
			
		||||
        tokio::spawn(async move {
 | 
			
		||||
            // Splitting the TCP socket into read/write halves
 | 
			
		||||
@@ -36,22 +38,26 @@ async fn main() -> Result<(), Report> {
 | 
			
		||||
 | 
			
		||||
            let mut line: String = String::new();
 | 
			
		||||
 | 
			
		||||
            writer.write_all(format!("Your address is {addr} \n").as_bytes()).await.unwrap();
 | 
			
		||||
 | 
			
		||||
            // second loop keeps receiving input from that one client going
 | 
			
		||||
            loop {
 | 
			
		||||
                tokio::select! {
 | 
			
		||||
                    // input half
 | 
			
		||||
                    result = buffer_reader.read_line(&mut line) => {
 | 
			
		||||
                        if result.unwrap() == 0{
 | 
			
		||||
                            break;
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        // send items to the broadcast channel
 | 
			
		||||
                        sender.send((line.clone(), addr)).unwrap();
 | 
			
		||||
                        sender.send((format!("{addr} - {line}"), addr)).unwrap();
 | 
			
		||||
                        // clear the input buffer
 | 
			
		||||
                        line.clear();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // print half
 | 
			
		||||
                    result = receiver.recv() => {
 | 
			
		||||
                        let (msg, other_addr) = result.unwrap();
 | 
			
		||||
 | 
			
		||||
                        if addr != other_addr {
 | 
			
		||||
                            writer.write_all(msg.as_bytes()).await.unwrap();
 | 
			
		||||
                        }
 | 
			
		||||
@@ -71,14 +77,5 @@ fn setup() -> Result<(), Report> {
 | 
			
		||||
    }
 | 
			
		||||
    color_eyre::install()?;
 | 
			
		||||
 | 
			
		||||
    if std::env::var("RUST_LOG").is_err() {
 | 
			
		||||
        std::env::set_var("RUST_LOG", "info");
 | 
			
		||||
        info!("RUST_LOG is set. All errors shall be logged !")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    tracing_subscriber::fmt::fmt()
 | 
			
		||||
        .with_env_filter(EnvFilter::from_default_env())
 | 
			
		||||
        .init();
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user