diff --git a/src/omega/omega_connection.rs b/src/omega/omega_connection.rs index 2a8c8e0..5869455 100755 --- a/src/omega/omega_connection.rs +++ b/src/omega/omega_connection.rs @@ -4,6 +4,7 @@ use async_tungstenite::{ tokio::{TokioAdapter, connect_async}, tungstenite::protocol::Message, }; +use crossterm::style::Print; use dashmap::DashMap; use futures::prelude::*; use json::{JsonValue, number::Number}; @@ -17,19 +18,19 @@ use tokio::{ use tokio_native_tls::TlsStream; use uuid::Uuid; -use crate::log_err; use crate::{ data::{ communication::{CommunicationType, CommunicationValue, DataTypes}, user::UserStatus, }, - get_private_key, log, log_in, log_out, + get_private_key, log, log_cv_out, log_in, log_out, rho::rho_manager::{self, RHO_CONNECTIONS, connection_count}, util::{ crypto_helper::{decrypt_b64, secret_key_to_base64}, logger::PrintType, }, }; +use crate::{log_cv_in, log_err}; pub struct WaitingTask { pub task: Box, CommunicationValue) -> bool + Send + Sync>, @@ -349,12 +350,12 @@ impl OmegaConnection { match msg { Some(Ok(Message::Text(msg))) => { let cv = CommunicationValue::from_json(&msg); + log_cv_in!(PrintType::Omega, cv); if cv.is_type(CommunicationType::pong) || cv.is_type(CommunicationType::ping) { self.handle_pong(&cv, true).await; continue; } let msg_id = cv.get_id(); - log_in!(0, PrintType::Omega, "{}", &cv.to_json().to_string()); if let Some(task) = WAITING_TASKS.remove(&msg_id) { if (task.1.task)(self.clone(), cv.clone()) { continue; @@ -371,9 +372,7 @@ impl OmegaConnection { } pub async fn send_message(&self, cv: &CommunicationValue) { - if !cv.is_type(CommunicationType::ping) { - log_out!(0, PrintType::Omega, "{}", &cv.to_json().to_string()); - } + log_cv_out!(PrintType::Omega, cv); let msg = cv.to_json().to_string(); let mut guard = self.write.lock().await; diff --git a/src/rho/client_connection.rs b/src/rho/client_connection.rs index 576ca85..a6793a1 100644 --- a/src/rho/client_connection.rs +++ b/src/rho/client_connection.rs @@ -29,7 +29,7 @@ use crate::{ }, omega::omega_connection::OmegaConnection, }; -use crate::{get_private_key, get_public_key, log_in, log_out}; +use crate::{get_private_key, get_public_key, log_cv_in, log_in, log_out}; pub struct ClientConnection { pub sender: Arc>>>, @@ -131,12 +131,7 @@ impl ClientConnection { self.handle_ping(cv).await; return; } - log_in!( - self.get_user_id().await, - PrintType::Client, - "{}", - &cv.to_json().to_string() - ); + log_cv_in!(PrintType::Client, cv); let identified = *self.identified.read().await; let challenged = *self.challenged.read().await; diff --git a/src/rho/iota_connection.rs b/src/rho/iota_connection.rs index ea208e1..e59a6eb 100755 --- a/src/rho/iota_connection.rs +++ b/src/rho/iota_connection.rs @@ -2,6 +2,7 @@ use crate::calls::call_group::CallGroup; use crate::calls::call_manager; use crate::get_private_key; use crate::get_public_key; +use crate::log_cv_in; use crate::log_err; use crate::log_in; use crate::log_out; @@ -154,12 +155,7 @@ impl IotaConnection { return; } - log_in!( - self.get_iota_id().await, - PrintType::Iota, - "{}", - cv.to_json().to_string() - ); + log_cv_in!(PrintType::Iota, cv); let identified = *self.identified.read().await; let challenged = *self.challenged.read().await; diff --git a/src/util/logger.rs b/src/util/logger.rs index 6fdcacd..edb6f6f 100644 --- a/src/util/logger.rs +++ b/src/util/logger.rs @@ -8,6 +8,9 @@ use std::{ }; use ansi_term::Color; +use json::JsonValue; + +use crate::data::communication::CommunicationValue; static LOGGER: OnceLock> = OnceLock::new(); @@ -138,3 +141,84 @@ macro_rules! log_err { $crate::util::logger::log_internal($sender, $kind, ">>", true, format!($($arg)*)) }; } + +// ******** COMMUNICATION VALUES ******** +pub fn log_cv_internal( + prefix: &'static str, + cv: &CommunicationValue, + print_type: Option, +) { + let formatted = format_cv(cv); + + log_internal( + cv.get_sender(), + print_type.unwrap_or(PrintType::General), + prefix, + false, + formatted, + ); +} + +pub fn format_cv(cv: &CommunicationValue) -> String { + let mut parts = Vec::new(); + + let sender = cv.get_sender(); + let receiver = cv.get_receiver(); + + if sender > 0 && receiver > 0 { + parts.push(format!("{} > {}", sender, receiver)); + } else if sender > 0 { + parts.push(format!("{}", sender)); + } else if receiver > 0 { + parts.push(format!("> {}", receiver)); + } + + let comm_type = cv.get_type().to_string(); + parts.push(format!("{}", comm_type)); + + let mut data_parts = Vec::new(); + if let JsonValue::Object(data) = &cv.clone().to_json()["data"] { + for (key, value) in data.iter() { + let val_string = match value { + JsonValue::String(s) => s.clone(), + _ => value.dump(), + }; + + data_parts.push(format!("{} {}", key, val_string)); + } + } + + if !data_parts.is_empty() { + parts.push(format!("{}", data_parts.join(", "))); + } + + parts.join(": ") +} +#[macro_export] +macro_rules! log_cv { + ($kind:expr, $cv:expr) => { + $crate::util::logger::log_cv_internal("", &$cv, Some($kind)) + }; + ($cv:expr) => { + $crate::util::logger::log_cv_internal("", &$cv, None) + }; +} + +#[macro_export] +macro_rules! log_cv_in { + ($kind:expr, $cv:expr) => { + $crate::util::logger::log_cv_internal("> ", &$cv, Some($kind)) + }; + ($cv:expr) => { + $crate::util::logger::log_cv_internal("> ", &$cv, None) + }; +} +#[macro_export] +macro_rules! log_cv_out { + ($kind:expr, $cv:expr) => { + $crate::util::logger::log_cv_internal("< ", &$cv, Some($kind)) + }; + ($cv:expr) => { + $crate::util::logger::log_cv_internal("< ", &$cv, None) + }; +}