Better Logging

This commit is contained in:
Alex Emmet 2025-10-26 11:20:00 +01:00
commit ab707c1907
13 changed files with 176 additions and 53 deletions

View file

@ -5,6 +5,8 @@ mod omega;
mod rho;
mod util;
use ansi_term::Color;
use crossterm::style::PrintStyledContent;
use futures::StreamExt;
use std::sync::Arc;
use tokio::net::TcpListener;
@ -14,13 +16,23 @@ use tungstenite::handshake::server::{Request, Response};
use crate::{
omega::omega_connection::OmegaConnection,
rho::{client_connection::ClientConnection, iota_connection::IotaConnection},
util::print::PrintType,
util::print::line,
util::print::line_err,
util::print::print_start_message,
};
#[tokio::main]
async fn main() {
print_start_message();
OmegaConnection::new().connect().await;
let listener = TcpListener::bind("0.0.0.0:959").await.unwrap();
println!("WebSocket server listening on 0.0.0.0:959");
line(
PrintType::OmegaIn,
"WebSocket server listening on 0.0.0.0:959",
);
while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(async move {
@ -32,12 +44,15 @@ async fn main() {
let ws_stream = match accept_hdr_async(stream, callback).await {
Ok(ws) => ws,
Err(e) => {
eprintln!("WebSocket upgrade failed: {}", e);
line_err(
PrintType::General,
&format!("WebSocket upgrade failed: {}", e),
);
return;
}
};
println!("New {} connection", path);
if path == "/ws/client/" {
line(PrintType::ClientIn, "New Client connection");
let client_conn: Arc<ClientConnection> =
Arc::from(ClientConnection::new(ws_stream));
loop {
@ -52,24 +67,25 @@ async fn main() {
let text = msg.into_text().unwrap();
client_conn.clone().handle_message(text).await;
} else if msg.is_close() {
println!("Client disconnected");
line(PrintType::ClientIn, "Client disconnected");
client_conn.handle_close().await;
return;
}
}
Some(Err(e)) => {
eprintln!("WebSocket error: {}", e);
line_err(PrintType::ClientIn, &format!("WebSocket error: {}", e));
client_conn.handle_close().await;
return;
}
None => {
println!("Client stream ended");
line(PrintType::ClientIn, "Client stream ended");
client_conn.handle_close().await;
return;
}
}
}
} else if path == "/ws/iota/" {
line(PrintType::IotaIn, "New Iota connection");
let iota_conn: Arc<IotaConnection> = Arc::from(IotaConnection::new(ws_stream));
loop {
let msg_result = {
@ -83,19 +99,19 @@ async fn main() {
let text = msg.into_text().unwrap();
iota_conn.clone().handle_message(text).await;
} else if msg.is_close() {
println!("Iota disconnected");
line(PrintType::IotaIn, "Iota disconnected");
iota_conn.handle_close().await;
return;
}
}
Some(Err(e)) => {
eprintln!("WebSocket error: {}", e);
line_err(PrintType::IotaIn, &format!("WebSocket error: {}", e));
iota_conn.handle_close().await;
return;
}
None => {
// Stream ended
println!("Iota stream ended");
line(PrintType::IotaIn, "Iota stream ended");
iota_conn.handle_close().await;
return;
}